Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best capture and log scp output?

Tags:

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.

like image 776
caseyboardman Avatar asked Oct 14 '08 19:10

caseyboardman


4 Answers

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.

like image 175
Martin Avatar answered Sep 22 '22 23:09

Martin


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.
like image 20
Tarski Avatar answered Sep 23 '22 23:09

Tarski


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.

like image 2
Tregoreg Avatar answered Sep 20 '22 23:09

Tregoreg


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

like image 1
ayaz Avatar answered Sep 21 '22 23:09

ayaz