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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.