如何最好地捕获和记录 scp 输出?
我正在尝试捕获安装脚本(使用 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 cdbcdb@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
是的,我最近试图从 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);
scp 使用控制代码将其进度条打印到终端。 它将检测您是否重定向输出,从而忽略进度条。
您可以通过使用大多数发行版默认安装的“script”命令来欺骗 scp 使其认为它在终端中运行来解决这个问题:
test.txt 的内容将是:
...这可能就是您想要的。
我在将交互式脚本的输出重定向到日志文件时偶然发现了这个问题。 日志中没有结果并不是问题,因为您始终可以评估退出代码。 但我真的希望交互式用户能够看到进度条。 这个答案解决了这两个问题。
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:
The content of test.txt will be:
...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.
看来您只是从日志中忽略了 scp 是否成功。
我猜滚动条不会打印到标准输出并使用 ncurses 或其他类型的 TUI?
你可以通过查看 scp 的返回值来判断是否成功。 就像
man scp
所说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
man scp
says这里的答案都不适合我,我需要在很长的地理距离上递归复制包含大量文件的大目录,所以我想记录进度(
&& echo成功!
到目前为止还不够)。我最终设计并以某种方式工作的是:
使用
-v
执行详细日志记录的技巧(-C
允许压缩和-r
递归)。通过 Grepping 日志文件,
我可以看到到目前为止复制的文件数量。
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:
With
-v
doing the trick of verbose logging (-C
allows compression and-r
recursion).Grepping the logfile
allowed me to see the number of files copied so far.
也许您可以使用'脚本' 记录终端会话。
Maybe you can use 'script' to log the terminal session.
非常有帮助,但是要将消息写入日志文件,我像这样更改了它
,这将写入“我的文件已成功复制!” 消息到日志文件。
is very helpful but to write the message to a log file I changed it like this
and this will write "myfile successfully copied!" message to the log file.
尝试:
Try:
我还不能发表评论:(所以我会在这里添加更新...
@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.
我的服务器上的 SCP 没有
-c
选项(-c
是指定一个 cihper) - 我需要速率,所以我使用了下面的:-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:-v
is for verbose on my server.这就是我所做的,
您可能不需要使用 SSH 选项 —o,但我有必要自动化每次在新虚拟机上运行 scp 的操作。
Here is what I did
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.
在上面的命令中,我将输出存储到文件 abc.txt 中。
此
grep
命令用于在文件 xyz.out 中搜索包含 Error 的文本,并将输出存储在 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.