Perl expect example

open(FH,”+>>brazil_raw.txt”);

my $count=1;
my $key ;
foreach $key ( keys %brazil)
{
print FH ” \n$count)\n”;
print FH “Firewall Name:$brazil{$key}\n”;
print FH “Management Interface X1: $key\n”;
my $expect = Expect->new(); # New object is made as Expect does not allow to reuse already Spawned object
$expect->spawn(“ssh -p 22 $key”) or die “Cannot spawn to $key please check”;
$expect->expect($timeout, $login_prompt);
$expect->send(“admin\n”);
$expect->expect($timeout, $password_prompt);
$expect->send(“password\n”);
$expect->expect($timeout, $command_prompt);
$expect->send(“show status \n”);
$expect->log_file(“brazil_raw.txt”,”+>>”);#This Command writes the data obtained from “show status” to the File
$expect->expect($timeout, $command_prompt);
$expect->hard_close();#Using Hard close as many Connections and wastage of Time
$count++;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s