tomfoolry has asked for the wisdom of the Perl Monks concerning the following question:

I have been attempting to capture the transfer speed and/or time from an scp transfer without any luck.

I have tried this: $scp_output = `/usr/bin/scp test_file.tar.gz usr@host:test_file.tar.gz`

But $scp_output remains empty. I have also tried piping the scp output into the perl script's stdin, but again without success.

# scp test_file.tar.gz usr@host:test_file.tar.gz | scp_capture.pl #!/usr/bin/perl while ($line = <STDIN>) { print $line; }

I even tried redirecting the output of scp to a file, so I could later read it into the script.
scp test_file.tar.gz usr@host:test_file.tar.gz > scp_output.txt

My guess is that my troubles are with the way scp generates its output to dynamically update the transfer speed/time. Is there any way I can capture this info?

Example scp output: test_file.tar.gz 100% 3884KB  3.8MB/s   00:00

Replies are listed 'Best First'.
Re: capture scp output
by Limbic~Region (Chancellor) on Apr 20, 2005 at 17:27 UTC
    tomfoolry,
    Just a hunch, but is the output of scp being written to STDERR and not STDOUT?

    Cheers - L~R

    Update: Apparently not. I tried:
    scp user@host:/path/to/file . >foo 2>bar
    Without capturing the progress bar in either file. Incidently, you can turn the progress bar off with the -q option. I wasn't expecting this to produce a nice plain text output given the nature of how the progress bar works, but I would have at least expected something - odd.

    Update 2: You might still be able to do what you want without the progress bar. If you 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.

      Thanks, it looks like the -v and -q options will get me what I want. I appreciate the help!
Re: capture scp output
by Fletch (Bishop) on Apr 20, 2005 at 17:37 UTC

    I believe that scp does an isatty() on it's output and doesn't write the statistics unless it's being run from a terminal, and casual poking seems to confirm that's the case. You could use Expect or IPC::Run to run it with a pty and that should fool it into producing the statistitcs.

    Of course it might be simpler to just save time before you start and again after the system( scp => ... ) returns, subtract and divide that into the file size (obtained with -s or stat.

      Heh, that was an obvious solution that didn't occur to me because I was too focused on trying to capture the output. I think I'm going to give L~R's suggestion a try first, though. Thanks!
Re: capture scp output
by Anonymous Monk on Jul 18, 2013 at 16:27 UTC

    screen will allow you to make sure there is a tty available during unattended execution

    screen -L myscript_with_scp_commands

    Then look for screenlog.0 in the current working directory