Home > PerlScripts > Telnet script for switches

Telnet script for switches

September 5th, 2008 Leave a comment Go to comments

.
.
Can you imagine telneting into 700+ switches to update just a single line? Well with this script it can be done in a very easy way, it does require Perl and the Net::Telnet::Cisco library.
In this case I used the script to update Alcatel Switches OminiStack but it should also work for Cisco Switches/Routers.

Download here, or copy and paste to file.pl and run perl file.pl hosts.txt

#!/usr/local/bin/perl -w
# Remote exec commands into a list of switches (Alcatel, OminiStack, Cisco) by telenet
# Author: Felipe Ferreira
# Date 05/09/2008
# Version: 0.3
# tip: open the .logs in gedit, because cat won't display all chars.
use Net::Telnet::Cisco ();

my $username = "admin";
my $passwd = "passnow";

if (!defined($ARGV[0]))
{
print "n Usage :$0 n";
print "Example: $0 hosts.txtn";
print "n Author: Felipe Ferreira nn";
exit 1;
}
$filehosts = $ARGV[0];
chomp($filehosts);

#Get all hosts IPs from the text file and pass to an array
open(DATA, $filehosts);
@swtichlist= ;
close(DATA);
foreach (@swtichlist) {
$host=shift @swtichlist;
$host =~s/t+//;
chomp ($host);

print "Loging to: $host n";
&TELNET;
}

# SUBS
sub TELNET {
$t = Net::Telnet::Cisco -> new (
Timeout => 5,
Binmode => "true",
Prompt => '/#/',
Dump_log => "dump.log",
Input_log => "$host.log");

#Start Connecting
$t->open($host);
$t->waitfor('/Name:/');
$t->print($username);
$t->waitfor('/Password:/');
$t->print($passwd);
$t->autopage;
$t->always_waitfor_prompt;
$t->cmd('config');
$t->waitfor_pause(0.9);
$t->send_wakeup( 'connect' );
$t->cmd('snmp-server community caca rw 10.1.2.12 view DefaultSuper');
$t->send_wakeup( 'connect' );
$t->waitfor_pause(0.9);
$t->cmd("exit");
$t->cmd("copy running-config startup-config");
$t->cmd("Yes");
$t->cmd("exit");
$t->close;
}
print "Done, check .log n";

# ref:
# http://www.ruby-doc.org/stdlib/libdoc/net/telnet/rdoc/classes/Net/Telnet.html
# http://nettelnetcisco.sourceforge.net/docs.html#name

  1. Chris Carson
    August 25th, 2009 at 03:35 | #1

    I cannot get the script to execute. I receive an error.

    perl file.pl hosts.txt
    syntax error at file.pl line 24, near “= ;”
    Execution of file.pl aborted due to compilation errors.

    hosts.txt is a file that includes a list of IP addresses.

    Thanks for any assistance you might have.

  2. alpha
    August 12th, 2014 at 14:45 | #2

    the community caca is the best

  1. No trackbacks yet.

VAMOVE *

*