Viki@Stag has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I wanted to run commands on remote computer & wanted this automated. So decided to use Perl's IPC::Run module alongwith rlogin.
Here is the script :
#!/usr/local/bin/perl use strict; if (@ARGV < 2) { usage(); } open (OUT, ">out.log") or die "out.log: $!\n"; select((select(OUT), $|=1)[0]); open (ERR, ">err.log") or die "err.log: $!\n"; select((select(ERR), $|=1)[0]); my $username = 'abc'; # username my $password = 'abc'; # password my $host = shift @ARGV; my @commands_to_run = @ARGV; use IPC::Run qw( start pump finish timeout ); my @startup = ('rlogin', $host, '-l', $username,); my ($in, $out, $err, $h) = (); my $h = start \@startup, \$in, \$out, \$err; $h->pump until ($out =~ /password:/i); $in = "$password\n"; $h->pump until ($out =~ /\[.+\]/); print "Logged into $host as $username\n"; print "Running commands:\n"; foreach my $cmd (@commands_to_run) { print "\t$cmd\n"; print OUT "STDOUT of $cmd ->\n"; print ERR "STDERR of $cmd ->\n"; $in = "$cmd\n"; do { $h->pump; print OUT "$out\n"; print ERR "$err\n"; } until ($out =~ m!\[.+\]!); print OUT "\n\n"; print ERR "\n\n"; } $in = "exit\n"; close OUT; close ERR; print "Loged Out\nDone!\n"; sub usage { print "perl $0 <hostname> <list of commands to run>\n"; print "Example:\n"; die "\tperl $0 host-name "cat /etc/crontab" "cat /path/to/file"\n +"; }
When I execute above script it does not come past the first
until($out =~ /\[.+\]/)
statement.
The reason I use this until statement is to detect that I have a shell prompt before I execute other commands.

Am I doing anything wrong ? Do suggest if there is any alternative to automate my requirement.
Thanks

Replies are listed 'Best First'.
Re: Question on IPC::Run
by zentara (Cardinal) on Mar 09, 2009 at 11:14 UTC
    Here is an older ssh script to show the idea. You might want to move up to Net::SSH2
    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; # If error is: # Can't locate object method "blocking" via package #"IO::Handle" at /usr/lib/perl5/site_perl/5.8.6/ # Net/SSH/Perl.pm line 212, <GEN0> line 1. # then use "protocol => 2"; my %hostdata = ( 'localhost' => { user => "z", password => "qumquat", cmdtorun => "ls -la", misc_data => [], }, 'zentara.zentara.net' => { user => "z", password => "aardvark", cmdtorun => "/usr/bin/uptime", misc_data => [], }, ); foreach my $host (keys %hostdata) { my $ssh = Net::SSH::Perl->new($host, port => 22, debug => 1, protocol => 2,1 ); $ssh->login($hostdata{$host}{user},$hostdata{$host}{password} ); my ($out) = $ssh->cmd($hostdata{$host}{cmdtorun}); print "$out\n"; }

    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: Question on IPC::Run
by Anonymous Monk on Mar 09, 2009 at 07:40 UTC
    Dont use rlogin, its insecure, All information, including passwords, is transmitted unencrypted (making it vulnerable to interception).