How to Connect Cisco Router with Perl Scripts and send config files to devices

From IPDonkey_wiki
Jump to: navigation, search

this is perl script for login to multiple cisco routers and switches and send initial config from files

you  need the falloing perl modules before 
 Net::Telnet http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm
 Net::Telnet::Cisco http://search.cpan.org/~joshua/Net-Telnet-Cisco-1.10/Cisco.pm


installation

user@hostn:~/DIR> tar xvfz Net-Telnet-Cisco-1.10.tar.gz 
Net-Telnet-Cisco-1.10/
Net-Telnet-Cisco-1.10/README
Net-Telnet-Cisco-1.10/Cisco.pm
Net-Telnet-Cisco-1.10/.cvsignore
Net-Telnet-Cisco-1.10/MANIFEST
Net-Telnet-Cisco-1.10/test.pl
Net-Telnet-Cisco-1.10/MANIFEST.SKIP
Net-Telnet-Cisco-1.10/Changes
Net-Telnet-Cisco-1.10/INSTALL
Net-Telnet-Cisco-1.10/Makefile.PL
Net-Telnet-Cisco-1.10/TODO
user@hostn:~/DIR> cd Net-Telnet-Cisco-1.10/
user@hostn:~/DIR/Net-Telnet-Cisco-1.10> perl Makefile.PL 
Checking if your kit is complete...
Looks good
Writing Makefile for Net::Telnet::Cisco
user@hostn:~/DIR/Net-Telnet-Cisco-1.10> make
cp Cisco.pm blib/lib/Net/Telnet/Cisco.pm
AutoSplitting blib/lib/Net/Telnet/Cisco.pm (blib/lib/auto/Net/Telnet/Cisco)
Manifying blib/man3/Net::Telnet::Cisco.3pm
user@hostn:~/DIR/Net-Telnet-Cisco-1.10> make install
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ERROR: Can't create '/usr/lib/perl5/site_perl/5.12.3/Net/Telnet'
Do not have write permissions on '/usr/lib/perl5/site_perl/5.12.3/Net/Telnet'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
at -e line 1
make: *** [pure_site_install] Error 13
user@hostn:~/DIR/Net-Telnet-Cisco-1.10> sudo make install
root's password:
Appending installation info to /usr/lib/perl5/5.12.3/i586-linux-thread-multi/perllocal.pod
user@hostn:~/DIR/Net-Telnet-Cisco-1.10> 


 #!/usr/bin/perl
use Term::ReadKey;
use lib '/Net-Telnet-Cisco-1.10/blib/lib';
use Net::Telnet::Cisco;
sub myloginread
{
 my $session = Net::Telnet::Cisco->new(Host => "10.2.219.101" , port=>$port,Input_log => "input.log");
 $session->print ("\r");
 $session->waitfor("/Username:/") or die('FAILED');
 $session->print($username);
 $session->waitfor("/Password:/") or die('FAILED');
 $session->print($password);
 $session->waitfor('/PROMPT>/');
 open 'config' , $file;
 while (my $line = <config>)
   {
   $session-> cmd ($line);
   }
}
sub readwriteerase
{
if ($result ==1){
   &myloginread;
   print "\n$file done!!\n";
   }
   elsif ($result ==2)
   {
   &myloginwrite;
   }
   elsif ($result ==4)
   {
   &loginrestore;
   print "\n$file done!!\n";
   }
}
sub myports
{
{
   $port = '2001';#port and fle assosiation 
   $file = 'r1.txt';
   &readwriteerase;
   };
{
   $port = '2002';
   $file = 'r2.txt';
   &readwriteerase;
   
   };
{
   $port = '2003';
   $file = 'r3.txt';
   &readwriteerase;
   
   };
};
sub loadfiles
{
print "Directory:";
chomp ($dir = <STDIN>) ;
unless(-d $dir){
print "Invalid directory!!\n" and  goto main ;
}
opendir(DIR,($dir)) ;
   while (my $file = readdir(DIR)) {
       next if ($file =~ m/^\./);
       print "$file\n";
   }
chdir $dir ;
print "Configuration to load:";
chomp ($dir = <STDIN>) ;
unless(-d $dir){
print "Invalid directory!!\n" and  goto main ;
}
chdir $dir ;
&mypassword;
}
sub mypassword
{
print "Username:";
chomp ($username =<STDIN>);
print "Password:";
ReadMode('noecho');
$password =<STDIN>;
ReadMode(0);
&myports;
}
sub saveconfig
{
print "location to save:";
chomp ($dir = <STDIN>) ;
unless(-d $dir){
   mkdir $dir or die;
}
chdir $dir ;
&mypassword;
}
sub myloginwrite
{
my $MB = 1024 * 1024;
my $session = Net::Telnet::Cisco->new(Host => "127.0.0.1" , port=>$port,Input_log => "input.log",Timeout=>30);
 $session->print ("\r");
 $session->waitfor("/Username:/") or die('FAILED');
 $session->print($username);
 $session->waitfor("/Password:/") or die('FAILED');
 $session->print($password);
 $session->cmd("terminal length 0");
 $session->max_buffer_length(5 * $MB);
     @output = $session->cmd("more system:running-config");
     open LOG, "> $file";
     select LOG;
     print @output;
     close LOG;
    $session->waitfor_pause; 
}
sub wrerase
{
print "TBC";
}
sub loginrestore
{
 my $MB = 1024 * 1024;
 my $session = Net::Telnet::Cisco->new(Host => "127.0.0.1" , port=>$port,Input_log => "input.log",Timeout=>25);
 $session->print ("\r");
 $session->waitfor("/Username:/") or die('FAILED');
 $session->print($username);
 $session->waitfor("/Password:/") or die('FAILED');
 $session->print($password);
 $session->waitfor('/PROMPT>/');
 #$session->cmd('^Z');
 $session->max_buffer_length(5 * $MB);
 $session->cmd("conf t");
 open 'config' , $file;
 while (my $line = <config>)
   {
   $session-> cmd ($line);
    }
 }
sub restore
{
print "restore location:";
chomp ($dir = <STDIN>) ;
unless(-d $dir){
print "Invalid directory!!\n" and  goto main ;
}
chdir $dir ;
&mypassword;
}
#######################################################################
main:
print "Choose a task:\n\n";
print "1. Load startup config. 
2. Save config.
3. Write erase.
4. Restore saved config\n\nSelect Option:";
$result = <STDIN>;
if ($result == 1)
{
&loadfiles
}
elsif ($result ==2)
{
&saveconfig
}
elsif ($result == 3)
{
&wrerase
}
elsif ($result == 4)
{
&restore
}
##################################################################### 


ipdonkey.co.uk

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox