当前位置: 动力学知识库 > 问答 > 编程问答 >

perl - How to answer the rsync password prompt using IPC::Run?

问题描述:

I do not have Expect and I don't get it to work on my machine.

I do not use an ssh key pair for rsync, but will supply a password (no, the password is not stored in the script or somewhere else - it's just a program that gets the password from somewhere and has to insert it).

I'm using cygwin on Windows x64

I tried to handle it using IPC::Run but I failed. Somehow, the password prompt seems to behave other than a normal prompt.

This works:

Reproduction script ask.pl to generate a prompt:

#!perl

use strict;

use warnings;

print "enter something:\n";

my $answer = <STDIN>;

print "your answer was: $answer\n";

exit(0);

Reproduction script to answer ask.pl's prmot:

#!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', '[email protected]:/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 the password to rsync?

I looked at other threads (provide password to SCP / SSH etc.) but the solution is either using Expcect or a key pair. But I cannot use Expect or the key pair.

网友答案:

You have 3 changes you need to make.

  1. You have to use a pseudo tty to capture the password prompt.
  2. To make it work in IPC::Run you have to run bash first then the command you want.
  3. You need to match $out with /password:/ not $in.

Here's the script, hope it helps:

#!perl
use strict;
use warnings;
use IPC::Run qw( start pump finish timeout );

my @cmd = ('bash', '-c', 'rsync -avr [email protected]:/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";
分享给朋友:
您可能感兴趣的文章:
随机阅读: