#!/usr/bin/perl
############################################
## Program: rexec.pl
## Author: Sai Prasad Kesavamatham
## Create Date: 10/09/2001
##
## This script needs the source router NAME and
## commands to execute as command line arguments
##
## Works under Unix; Tested on Solaris 2.6
## Requires Comm.pl module in the path
## as shown in the first line
#############################################
##
## Modified by:
## Date Modified:
## Description:
##
#############################################

## Usage: perl rexec.pl router_name "router commands with commas"
## router_name is the name or ip address of the router
## Keep the commands with in double quotes separated by commas
## example: perl rexec.pl 10.10.1.1 "show clock,show version"
## example: $perl rexec.pl 10.10.1.1 show clock,show version, , ,"
## Prompts for user-id, password 
## To get into enable mode, uncomment the enable password line down somewhere

unshift(@INC,"/export/home/mrtg");
require "Comm.pl"; # Need this library module in the lib path
&Comm'init( 1.8 ); # Related to the above module
# Test the proper command line arguments
if ($ARGV[0] eq "?" || $#ARGV lt 1 || $ARGV gt 3) {
&Error_Message;
}
if (($Output_Dir=$ARGV[2]) eq ""){
$Output_Dir=$ENV{PWD};
}
if (($Extension=$ARGV[3]) eq ""){
open (TMP,"date '+%m%d%y-%H:%M:%S'|");
chop($Extension=<TMP>);
close (TMP);
}

$Program = "/usr/bin/telnet";
$Host = $ARGV[0];
$User = "";
$Enable = "en";
$Cmd1="term len 0";
&Ping_Test($ARGV[0]); 

print ("Enter User name for this router: ");
# Login id (or default if ENTER) from key board
while (($User=<STDIN>) eq "\n"){ 
print "Please type in a User name to continue! :";
}
chop($User);
print "\nEnter the login password; This will not be displayed on the screen:\n";
# Echo-blind for password protection
system ("stty -echo");
chop($Login_Passwd=<STDIN>);
print "\n";
$Login_Passwd=$Login_Passwd;
print "Enter the enable password; This will not be displayed on the screen:\n"; 
chop($Enable_Passwd=<STDIN>);
print "\n";
$Enable_Passwd=$Enable_Passwd;
system ("stty echo");

$|=1; # To flush all the output to the process 

# Process handler for the Telnet session

( $Proc_pty_handle, $Proc_tty_handle, $pid ) = &open_proc( "$Program $Host");
die "open_proc failed" unless $Proc_pty_handle;
# Catching the return results and searching for the required pattern
( $match, $err, $before ) = &expect( $Proc_pty_handle, 3, 'Username:' );
# Sending the commands to the process handler

if ( $match eq 'Username:' )
{
print $Proc_pty_handle "$User\n";
}


print $Proc_pty_handle "$Login_Passwd\n";
print $Proc_pty_handle "$Enable\n";
print $Proc_pty_handle "$Enable_Passwd\n";
print "Your request is being processed\n";
print " Please wait...";
&Exist_Check($Output_Dir);
$Log_File=$Host."_".$Extension;
open (LOG, ">$Output_Dir/$Log_File.txt"); # For output results

# TAking care of the command line supplied commands
print $Proc_pty_handle "$Cmd1\n";
( $match, $err, $before) = &expect( $Proc_pty_handle, 3, "$Cmd1");
@cmd_set=split(/,/,$ARGV[1]);
foreach $cmd_set(@cmd_set){ # Start of FOR loop
print $Proc_pty_handle "$cmd_set\n";
( $match, $err, $before) = &expect( $Proc_pty_handle, 3, "$cmd_set");
print LOG "$before\n"; 
print LOG "Command sent: $match\n";
print "..";
} # End of FOR loop
print $Proc_pty_handle "$exit\n";

( $match, $err, $before) = &expect( $Proc_pty_handle, 3, 'exit' );
print LOG "Command sent: $match\n$before\n";

&close_it( $Proc_pty_handle);
&close_it($Proc_tty_handle);
close (LOG);
system ("/usr/bin/dos2unix $Output_Dir/$Log_File.txt $Output_Dir/$Log_File.txt");
print "\n\nThe results are now available in the '$Output_Dir/$Log_File' file\n\n";
print " Written by Sai Prasad Kesavamatham, 2001\n\n";

# ***********************************************************
# Start of subroutine:- Ping_Test
# To test the availability of the router specified 

sub Ping_Test{
print "\n\n\n Testing the network connectivity ...\n";
open (TMP, "/usr/sbin/ping $_[0] |");
$line = <TMP>;
close (TMP);

if ($line=~/alive/)
{
print " \nFound the router $_[0] !!\n\n";
}
else {
print ("I am not able to ping the Router: $_[0], Please check the connectivity\n\n");
exit(1);
}
}

# ***********************************************************
# ****Start of subroutine:-Exist_Check****
# Call this before generating the log files
sub Exist_Check{
$my_out_dir=$_[0];

if (!-d $my_out_dir){
mkdir ($my_out_dir,0755);
}
}
# *** End of subroutine:- Exist_Check ***
# ***********************************************************
# ****Start of subroutine:-Error_Message****
sub Error_Message{
system ("/usr/bin/clear");
print <<EOM;

Program execution aborted
-------------------------------
This program remotely executes the commands specified on a router.
Term Len is set to 0 with in the program
Insufficient number of parameters entered; Please check the Syntax below

Usage: 
perl rexec.pl router_name "cmd1,cmd2,..." [Output_Directory] [Extension]
(Do not forget the double quotes and commas)
Router_Name : Router on which to run the commands
Commands : Commands to be run [Separated by commas
Output_Directory: Output folder[Optional] location for the results
Extension : File extension if needed [Optional]

example: perl rexec.pl 10.10.1.1 "term len 0,show ip route"
To display this help message: perl rexec.pl ?
example: perl rexec.pl lab01 "show version,show clock"

To display this help message: perl rexec.pl ?
------------------------------------------------------------
Written by Sai Prasad Kesavamatham, 2001

EOM

exit(1);

}
# ***********************************************************

Make your own free website on Tripod.com