Monday, August 12, 2013

scp - Capture the output?

Since scp calls the isatty() function on it's output, it doesn't print the statistics unless it's being run from a terminal, so if you redirect scp output it will omit the progress bar.

You can get around by tricking scp into thinking it runs in a terminal using the "script" command:
# script -q -c "scp file tony@server1:/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

or you can disable the progress bar with -q but enable verosity with -v, then all the same information that is in -q can be read on STDERR. Incidently, scp exits with 0 on success and >0 on error.
# scp -q -c file tony@server1:/tmp &> test.txt

No comments: