如何最好地捕获和记录 scp 输出?

发布于 2024-07-07 02:54:03 字数 596 浏览 7 评论 0原文

我正在尝试捕获安装脚本(使用 scp)的输出并记录它。 但是,我没有得到 scp 打印出来的所有内容,即进度条。

屏幕输出:

复制 /user2/cdb/builds/tmp/uat/myfiles/* 到 服务器 /users/myfiles 作为 cdb

cdb@server的密码: 我的文件 100% |********************************| 2503 00:00

日志输出:

复制 /user2/cdb/builds/tmp/uat/myfiles/* 到 服务器 /users/myfiles 作为 cdb

我真的很想知道我的文件到达那里。 这是我现在尝试的但无济于事:

myscript.sh 2>&1 | tee mylogfile.log

有人有捕获 scp 输出并记录它的好方法吗?

谢谢。

I am trying to capture output from an install script (that uses scp) and log it. However, I am not getting everything that scp is printing out, namely, the progress bar.

screen output:

Copying
/user2/cdb/builds/tmp/uat/myfiles/* to
server /users/myfiles as cdb

cdb@server's password:
myfile 100% |*****************************| 2503 00:00

log output:

Copying
/user2/cdb/builds/tmp/uat/myfiles/* to
server /users/myfiles as cdb

I'd really like to know that my file got there. Here's what I am trying now to no avail:

myscript.sh 2>&1 | tee mylogfile.log

Does anyone have a good way to capture scp output and log it?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

青丝拂面 2024-07-14 02:54:04

是的,我最近试图从 proc_open() 获取 php 脚本中的输出
我失去了一段安静的时间试图获得输出:-)
但这里有点晚了,然后我在这里阅读这篇文章让我意识到我真的不需要这个垃圾输出到我的脚本中,

只需退出代码就可以完成这项工作:-)

$exit_code = proc_close($process);

yes i recently was trying to get output within a php script from proc_open()
i lost a quiet a time trying to get output :-)
but its a bit late here and i then reading this post here made me realize that i dont really need this junky output to my script

just the exit code will do the job :-)

$exit_code = proc_close($process);

你在我安 2024-07-14 02:54:03

scp 使用控制代码将其进度条打印到终端。 它将检测您是否重定向输出,从而忽略进度条。

您可以通过使用大多数发行版默认安装的“script”命令来欺骗 scp 使其认为它在终端中运行来解决这个问题:

script -q -c "scp server:/file /tmp/" > /tmp/test.txt

test.txt 的内容将是:

file    0%    0     0.0KB/s   --:-- ETA
file   18%   11MB  11.2MB/s   00:04 ETA
file   36%   22MB  11.2MB/s   00:03 ETA
file   54%   34MB  11.2MB/s   00:02 ETA
file   73%   45MB  11.2MB/s   00:01 ETA
file   91%   56MB  11.2MB/s   00:00 ETA
file  100%   61MB  10.2MB/s   00:06

...这可能就是您想要的。

我在将交互式脚本的输出重定向到日志文件时偶然发现了这个问题。 日志中没有结果并不是问题,因为您始终可以评估退出代码。 但我真的希望交互式用户能够看到进度条。 这个答案解决了这两个问题。

scp prints its progress bar to the terminal using control codes. It will detect if you redirect output and thus omit the progress bar.

You can get around that by tricking scp into thinking it runs in a terminal using the "script" command which is installed on most distros by default:

script -q -c "scp server:/file /tmp/" > /tmp/test.txt

The content of test.txt will be:

file    0%    0     0.0KB/s   --:-- ETA
file   18%   11MB  11.2MB/s   00:04 ETA
file   36%   22MB  11.2MB/s   00:03 ETA
file   54%   34MB  11.2MB/s   00:02 ETA
file   73%   45MB  11.2MB/s   00:01 ETA
file   91%   56MB  11.2MB/s   00:00 ETA
file  100%   61MB  10.2MB/s   00:06

...which is probably what you want.

I stumbled over this problem while redirecting the output of an interactive script into a log file. Not having the results in the log wasn't a problem as you can always evaluate exit codes. But I really wanted the interactive user to see the progress bar. This answer solves both problems.

清泪尽 2024-07-14 02:54:03

看来您只是从日志中忽略了 scp 是否成功。

我猜滚动条不会打印到标准输出并使用 ncurses 或其他类型的 TUI?

你可以通过查看 scp 的返回值来判断是否成功。 就像

scp myfile [email protected]:. && echo success!

man scp所说

scp exits with 0 on success or >0 if an error occurred.

It looks like your just missing whether the scp was succesful or not from the log.

I'm guessing the scroll bar doesn't print to stdout and uses ncurses or some other kind of TUI?

You could just look at the return value of scp to see whether it was successful. Like

scp myfile [email protected]:. && echo success!

man scp says

scp exits with 0 on success or >0 if an error occurred.
别挽留 2024-07-14 02:54:03

这里的答案都不适合我,我需要在很长的地理距离上递归复制包含大量文件的大目录,所以我想记录进度&& echo成功! 到目前为止还不够)。

我最终设计并以某种方式工作的是:

scp -vrC root@host:/path/to/directory . 2> copy.log &

使用 -v 执行详细日志记录的技巧(-C 允许压缩和 -r 递归)。

通过 Grepping 日志文件,

grep file copy.log | wc -l

我可以看到到目前为止复制的文件数量。

None of the answers here worked for me, I needed to recursively copy large directory with lot of files over long geo distance, so I wanted to log the progress (&& echo success! was by far not enough).

What I finally engineered and somehow worked was:

scp -vrC root@host:/path/to/directory . 2> copy.log &

With -v doing the trick of verbose logging (-C allows compression and -r recursion).

Grepping the logfile

grep file copy.log | wc -l

allowed me to see the number of files copied so far.

郁金香雨 2024-07-14 02:54:03

也许您可以使用'脚本' 记录终端会话。

Maybe you can use 'script' to log the terminal session.

失退 2024-07-14 02:54:03
scp myfile [email protected]:. && echo success! 

非常有帮助,但是要将消息写入日志文件,我像这样更改了它

scp myfile [email protected]:. && echo myfile successfully copied! >> logfile 2>&1

,这将写入“我的文件已成功复制!” 消息到日志文件。

scp myfile [email protected]:. && echo success! 

is very helpful but to write the message to a log file I changed it like this

scp myfile [email protected]:. && echo myfile successfully copied! >> logfile 2>&1

and this will write "myfile successfully copied!" message to the log file.

━╋う一瞬間旳綻放 2024-07-14 02:54:03

尝试:

scp server:/file /tmp/ > /dev/tty

Try:

scp server:/file /tmp/ > /dev/tty
平定天下 2024-07-14 02:54:03

我还不能发表评论:(所以我会在这里添加更新...

@Martin 为我提供了最好的解决方案,尽管如果您的 scp 命令位于脚本的中间,那么它的输出可能出现在之后 。

我认为这是因为脚本必须在子 shell 中运行该命令,但我尚未进行

编辑:它确实会生成一个 shell,因此如果您需要按顺序运行(并且确实失败) (就像在构建脚本中一样)那么您必须在脚本命令的使用中添加一些逻辑,

script -q -c "your command" && sleep 1

或类似的内容,以便您的父 shell 等待。 child shell 在继续之前要完成。

I can't comment yet :( so I'll add an update here...

@Martin had the best solution for me although if your scp command is midway through your script then it's output may appear after commands that actually ran afterwards.

I think that is because script must run the command in a subshell but I am yet to test.

EDIT: it does indeed spawn a shell so if you need things to run (and indeed fail) in a sequential manner (like in a build script) then you would have to add some logic around the use of the script command.

i.e.

script -q -c "your command" && sleep 1

or something similar so that your parent shell waits for the child shell to finish before moving on.

海的爱人是光 2024-07-14 02:54:03

我的服务器上的 SCP 没有 -c 选项(-c 是指定一个 cihper) - 我需要速率,所以我使用了下面的:

$ scp -q -v nb3510@servername:sftp.dummy . 2>&1 | grep 'Bytes per second'
Bytes per second: sent 52945.7, received 188047087.0

-v 用于我的服务器上的详细信息。

SCP on my server did not have -c option (well -c was to specify a cihper) - I needed the rate, so I used below:

$ scp -q -v nb3510@servername:sftp.dummy . 2>&1 | grep 'Bytes per second'
Bytes per second: sent 52945.7, received 188047087.0

-v is for verbose on my server.

人间不值得 2024-07-14 02:54:03

这就是我所做的,

script -q /dev/stdout -c 'scp -P 3303 -o StrictHostKeyChecking=no -r -i $(sshKey.secureFilePath) LocalFolder myadminadmin@$(HOSTIP):/RemoteFolder/' | tee scp.log

您可能不需要使用 SSH 选项 —o,但我有必要自动化每次在新虚拟机上运行 scp 的操作。

Here is what I did

script -q /dev/stdout -c 'scp -P 3303 -o StrictHostKeyChecking=no -r -i $(sshKey.secureFilePath) LocalFolder myadminadmin@$(HOSTIP):/RemoteFolder/' | tee scp.log

You may not need —o with SSH options, but it was necessary for me to automate something that runs scp on a new VM every time.

街道布景 2024-07-14 02:54:03
$ grep -r "Error" xyz.out > abc.txt

在上面的命令中,我将输出存储到文件 abc.txt 中。

grep 命令用于在文件 xyz.out 中搜索包含 Error 的文本,并将输出存储在 abc.txt 中而不显示在控制台上。

$ grep -r "Error" xyz.out > abc.txt

Here in the above command I am storing output into file abc.txt.

This grep command is for searching text containg Error in file xyz.out and storing the output in abc.txt without displaying on console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文