#!/usr/bin/perl
############################################
## Program: rping.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 [/scripts]
#############################################
##
## 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

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];
$Cmds_File = $ARGV[1];
$Enable = "en";
$Cmd1="term len 0";
# &Ping_Test($ARGV[0]); 

if (!open (CMDS, "$Cmds_File")){
print "\n\n\tCould not open the Commands file specified: $Cmds_File\n";
exit;
}
chomp(@cmd_set = <CMDS>);
close (CMDS);
$User = shift (@cmd_set);
$Login_Passwd = shift (@cmd_set);

$|=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";

&Exist_Check($Output_Dir);
$Log_File=$Host."_".$Extension;

# Set the terminal length
print $Proc_pty_handle "$Cmd1\n";
( $match, $err, $before) = &expect( $Proc_pty_handle, 3, "$Cmd1");
# Taking care of the command line supplied commands
foreach $cmd_set(@cmd_set){ # Start of FOR loop
print $Proc_pty_handle "show clock\n";
( $match, $err, $before) = &expect( $Proc_pty_handle, 3, "show clock");

print $Proc_pty_handle "$cmd_set\n";
( $match, $err, $before) = &expect( $Proc_pty_handle, 3, "$cmd_set");

} # End of FOR loop
print $Proc_pty_handle "show clock\n";
( $match, $err, $before) = &expect( $Proc_pty_handle, 3, 'show clock' );
#print LOG "Command sent: $match\n$before\n";
print $Proc_pty_handle "$exit\n";
&close_it( $Proc_pty_handle);
&close_it($Proc_tty_handle);

@tmp = $before;
&Parse_MS (\@tmp);

print " Written by Sai Prasad Kesavamatham, 2001\n\n";

# ***********************************************************
# ****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:-Parse_MS****
# To get the ms values 
sub Parse_MS{
$tmp = $_[0];
@array = @$tmp;
# print "From Array:\n@array\n\nsai\n\n";
foreach $line(@array){
if ($line =~ /round-trip/){
@stats = split (/=/,$line);
@ms = split (/\//,$stats[1]);
@tmp = split (/\s+/,$ms[2]);
# print "$ms[0],$ms[1],$tmp[0]";
print "$ms[0]\n$tmp[0]\n";
print "Uptime Here\n";
print "Host Name\n";
}
}
}
# *** End of subroutine:- Parse_MS ***
# ***********************************************************
# ****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 cmds.txt [Output_Directory] [Extension]
(Do not forget the double quotes and commas)
Router_Name : Router on which to run the commands
Commands : File containing Commands to be run in separateline
in the order, including user-id and password
Output_Directory: Output folder[Optional] location for the results
Extension : File extension if needed [Optional]

example: perl rexec.pl 10.10.1.1 cmds.txt
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