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

I've got two scripts. One that will back up crontabs on remote machines, one that will backup directories. The crontab one simply ssh'es to the target, lists the crontab, and prints out. I use a simple
open ( CRON, "ssh host crontab -l" ) || die $!; while ( <CRON> ) { print $_; }
It works just fine. Doing something similar for rsync:
open ( RSYNC, "rsync -az -e \"ssh -o BatchMode=yes\" --delete-after ho +st:/path/to/dir /path/to/backup |" ); while ( <RSYNC> ) { print "output: $_"; }
The rsync command will only print out something when it doesn't work properly. In this case, I actually store it and do something w/ it later. What I've found though, is that when I run the script, it is spitting the output out on the terminal... not being captured in the file handle. I assume ssh is printing to stdout and rsync to something else, but I thought Perl would capture any output, if I open a command as a file handle.

Before anyone asks, I'm not using the Perl modules as I've had a very hard time w/ them, due to different SSH versions on my network. That is beyond my control, so I'm adapting. The above works much better, sans the output issue.

Replies are listed 'Best First'.
Re: Filehandle not reading
by graff (Chancellor) on Mar 05, 2007 at 03:09 UTC
    The file handle is only capturing STDOUT, and the stuff that is showing up on the terminal is most likely being printed to STDERR. Adjusting the command line being passed to open might fix it:
    open( RSYNC, "rsync -az -e \"ssh -o Batchmode=yes\" --delete-after hos +t:/path/to/dir /path/to/backup 2>&1 |" );
    I think the  2>&1 redirection should work on most unix-style shells, but I'm not sure -- test that first.
      The file handle is only capturing STDOUT, and the stuff that is showing up on the terminal is most likely being printed to STDERR. Adjusting the command line being passed to open might fix it:

      Alternatively, one can use IPC::Open3 to distinguish what's printed to STDOUT and what to STDERR.

      Cheers, graff. That worked like a champ.