How to best capture and log scp output?

前端 未结 9 1181
栀梦
栀梦 2020-12-31 01:55

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. <

相关标签:
9条回答
  • 2020-12-31 02:48
    scp myfile user@host.com:. && echo success! 
    

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

    scp myfile user@host.com:. && echo myfile successfully copied! >> logfile 2>&1
    

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

    0 讨论(0)
  • 2020-12-31 02:48

    Try:

    scp server:/file /tmp/ > /dev/tty
    
    0 讨论(0)
  • 2020-12-31 02:55

    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.

    0 讨论(0)
提交回复
热议问题