#!\usr\bin\perl
# Written by Richard Graves
#
#
# ARGV[0] = Device
# ARGV[1] = Command list file
# ARGV[2] = Username
# ARGV[3] = Password
# ARGV[4] = Enable Password
# ARGV[5] = Output Filename
#
#--------------------------------------------------------------------------
use strict;

my ($session, $host, $file, $TLNTUSER, $TLNTPWD, $ENBPWD, $OutputFile);

$host=$ARGV[0];
$file=$ARGV[1];
$TLNTUSER=$ARGV[2];
$TLNTPWD=$ARGV[3];
$ENBPWD=$ARGV[4];
$OutputFile=$ARGV[5];

print ("\nDevice: $host");	#let me know what device I am working on

#--------------------------------------------------------------------------------------------
my (@CONFIGVAR, $CONFIGVAR);
my ($cntr, $line);

if (-e $file && -r $file)						#if file exists and is readable
{
	open(IN, "$file");							#open the file using the IN filehandle
}


while($line=<IN>)								#while data is coming in from file
{
		
	unless($line=~ m/^\!+/)						#unless the line starts with # do the following..
	{											
		$cntr++;
		if ($cntr > 20) 
		{
			die "Please limit number of commands to 20!";
		}
		else
		{
			push (@CONFIGVAR, $line);
		}
			
	}
	
	
}
close(IN);										#close the filehandle
												#close the filehandle
#--------------------------------------------------------------------------------------------


my ($Site, $Region);

open(OUTPUT, ">>$OutputFile");

use Net::Telnet::Cisco;						#use the telnet::cisco module

if ($host =~ m/\d+\.(\d+)\.(\d+)\.\d+/ )  #break out region and site subnets
	{
	 $Region = $1;
	 $Site = $2;
	}
 		
print (OUTPUT "\n$host");

$session = Net::Telnet::Cisco->new(Timeout => '300',
                                   Host => $host,);	#set host and error mode

								
	$session->login($TLNTUSER, $TLNTPWD);		#set user/password
		  
		    # Enable mode
	  if ($session->enable($ENBPWD) ) {
	  } 
	  else 
	  {
	      warn "Can't enable: " . $session->errmsg;
	  }
	  
	
	$session->cmd('config t');	
	
	
	foreach $CONFIGVAR(@CONFIGVAR)
	{
		$session->cmd($CONFIGVAR);
		$session->waitfor_pause(1);
	}
	
	
	$session->cmd('exit');
	
	
	$session->cmd('wr mem');
	$session->waitfor_pause(10);
	
	
	#my @out;
	#@out = $session->cmd("copy system:/running-config "
	#		. "tftp://10.64.80.7/\n\n\n\n");
	
	#print (OUTPUT "@out\n\n");
	
	print (OUTPUT "\tSuccessful");			#print to file
	print ("\tSuccessful\n");					#print to screen

$session->close;								#close the telnet session
close(OUTPUT);






=head1 NAME

Combined Cisco Script

=head1 DESCRIPTION

This script is used to issue config commands to a cisco device using either telnet or SSHv1.

This is the main script, the other two scripts CCS_main.pl and CCS_sub_SSH.pl are also required

=head1 README


=head1 PREREQUISITES

This script requires:

Perl 5.8 or higher
Net::SSH::Perl 
Net::SSH::Perl::Constants
Net::Telnet
Net::Telnet::Cisco

=head1 COREQUISITES

CCS_main.pl
CCS_sub_SSH.pl


=pod OSNAMES

any

=pod SCRIPT CATEGORIES

Networking

=cut