How do I answer a rsync password prompt using IPC :: Run?
I don't have Expect and I am not making it work on my machine. I am not using an ssh key pair for rsync, but will provide a password (no, the password is not stored in the script or anywhere else - it is just a program, where the password is entered from somewhere, and needs to be pasted). I am using cygwin on Windows x64
I tried to process it using IPC::Run
but I failed. Either way, the password hint seems to behave differently than the regular prompt.
It works:
Playing script ask.pl to generate the prompt:
#!perl
use strict;
use warnings;
print "enter something:\n";
my $answer = <STDIN>;
print "your answer was: $answer\n";
exit(0);
Reproducing the script for ask.pl prmot's answer:
#!perl
use strict;
use warnings;
use IPC::Run qw( start pump finish timeout );
my @cmd = ( "perl", "ask.pl" );
my $in = "ask something:\n";
my $out;
my $h = start( \@cmd, \$in, \$out, timeout(5) );
pump $h until $in =~ /something:/;
$in = "1234\n";
finish $h or die "app returned $?";
print "done";
This does not work:
#!perl
use strict;
use warnings;
use IPC::Run qw( start pump finish timeout );
my @cmd = ( 'rsync', '-avr', 'user@host:/som/path/in/filesystem', '/target/folder' );
my $in = "password:\n";
my $out;
my $h = start( \@cmd, \$in, \$out, timeout(5) );
pump $h until $in =~ /password:/;
$in = "secret-password-here\n";
finish $h or die "app returned $?";
print "$out";
print "done";
So how do I provide a password for rsync?
I looked at other streams (provide password for SCP / SSH, etc.), but the solution either uses Expcect or a key pair. But I cannot use Expect or key pair.
source to share
You have 3 changes you need to make.
- You must use a pseudo tty to get the password prompt.
- To make it work in IPC :: Run, you have to start bash first and then the command you want.
- You need to match
$out
against/password:/
not$in
.
Here's a script, hope it helps:
#!perl
use strict;
use warnings;
use IPC::Run qw( start pump finish timeout );
my @cmd = ('bash', '-c', 'rsync -avr user@host:/som/path/in/filesystem /target/folder');
my $in;
my $out;
my $h = start \@cmd, '<pty<', \$in, '>pty>', \$out, timeout(5);
pump $h until $out =~ /password:/;
$in = "secret-password-here\n";
finish $h or die "app returned $?";
print "$out";
print "done";
source to share