Saturday, April 15, 2006

Running commands using script


#!/usr/bin/perl
#Written by Vaibhav.
#guptav[at]cse[dot]iitb[dot]ac[dot]in on Mon Mar 7

use Expect;
#List of IPs in file 'IPS'
open(IP,"<IPS");
@IPS=<IP>;
close IP;
my $newpassword = "yourpassword";

foreach $ip(@IPS) {
chomp $ip;
print "-------$ip-------\n";
my $timeout = 50;
my $aft = new Expect;
$aft->spawn("ssh $ip")
or die "Cannot ssh to the machine \n";
$aft->log_file("/tmp/expect_log");
$aft->expect($timeout,'-re','\? $');
$aft->send("yes\n");
$aft->expect($timeout,'-re','password: $');
$aft->send("$newpassword\n");
$aft->expect($timeout,'-re','# $');
$aft->send("ls\n"); # OR LIST OF COMMANDS
$aft->expect($timeout,'-re','# $');
$aft->send("exit\n");
}