use IO::Socket; 
print " -----------------------------------\n";
print " Cisco switch config backup utility\n";
print " Written by Peter Van Eeckhoutte\n";
print " Version 1.1\n";
print " (c) 2009\n";
print " http://www.corelan.be:8800\n";
print " -----------------------------------\n\n";

if ($#ARGV < 2)   { 
print "  usage: $0 <hostsfile> <ip of tftp> <enable password> [<telnet password> <telnet username>]\n\n"; 
exit(0); 
} 
my $inputfile = $ARGV[0];
my $tftpserver = $ARGV[1];
my $enablepw = $ARGV[2];
my $telnetpw = $ARGV[2];
if ($#ARGV eq 3)
{
  $telnetpw = $ARGV[3];
}
my $telnetuser="";
if ($#ARGV eq 4)
{
  $telnetpw = $ARGV[3];
  $telnetuser = $ARGV[4];
}

open FILE, $inputfile or die "Unable to open file $inputfile\n";
while (<FILE>)
{
    $targethost=$_;
	chomp($targethost);
	print " [+] Connecting to switch $targethost \n";
	$sock = IO::Socket::INET->new(PeerAddr => $targethost, 
								  PeerPort => 23, 
								  Proto    => 'tcp'); 
	$output = <$sock> || die " [!] *** Unable to connect ***\n"; 
	sleep 1;
	print "     Logging in\n";
	if ($telnetuser ne "")
	{
	  print $sock $telnetuser."\n";
	  $output = <$sock>;
	  sleep 1;
	}
	print $sock $telnetpw."\n";
	$output = <$sock>;
	sleep 1;
	print "     Going into enable mode\n";
	print $sock "enable\n";
	$output = <$sock>;
	sleep 1;
	print $sock $enablepw."\n";
	$output = <$sock>;
	sleep 1;
	print "     Sending configuration to tftp $tftpserver\n";
	print $sock "copy running-config tftp\n";
	$output = <$sock>;
	sleep 1;
	print $sock $tftpserver."\n";
	$output = <$sock>;
	print $sock "\n";
	sleep 1;
	print "     Sending vlan.dat to tftp $tftpserver\n";
	print $sock "copy vlan.dat tftp\n";
	$output = <$sock>;
	sleep 1;
	print $sock $tftpserver."\n";
	$output = <$sock>;
	print $sock "\n";
	sleep 1;
	print "     Closing connection\n\n";
	print $sock "exit\n";
	close $sock;
}
close FILE or die "Unable to close file\n";

