use IO::Socket; use Net::Appliance::Session; use strict; use warnings; use Sys::Hostname; use POSIX qw(:sys_wait_h); sub REAP { 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = \&REAP; } $SIG{CHLD} = \&REAP; my ($sock,$LOCALPORT,$MAXLEN,$USER,$PASSWORD,$FILENAME,%PORTHASH,$client,$message,$con_handle); $LOCALPORT=2500; $MAXLEN=5151; $USER="REMOVED"; $PASSWORD="REMOVED"; #open STDOUT, '>>data.txt' or die $!; #Open Listener on TCP port identified by $LOCALPORT $sock= IO::Socket::INET->new(LocalPort => $LOCALPORT,Proto => 'tcp', Reuse => 1, Listen => SOMAXCONN) or die "Socket: $@"; $sock->autoflush(1); print "Starting... running on port: $LOCALPORT\n"; while($client = $sock->accept()) { next if my $con_handle = fork; die "fork fail: $!" unless defined($con_handle); $client->recv($message,1024); print "In here:\n"; print $message."\n"; $|=1; #If the packet has the right format of : , SSH to it and implement the macro stored on the switch if ($message=~ /(.*)\:(\d*)/) { #Make sure the port mapping exists, If it does, connect SSH session my $host=$1; my $portnumber=$2; #Pull in port mappings. Format is , I.E. 13,FastEthernet1/0/20 $FILENAME="ports.".$host.".txt"; print "Opening Ports and Reading File:".$FILENAME."\n"; open PORTS, $FILENAME or die $!; my @ports=; close PORTS; foreach(@ports) { chomp($_); my @t=split(/,/,$_); $PORTHASH{$t[0]}=$t[1]; } print "Port reading complete\n"; if (exists $PORTHASH{$portnumber}) { print "Exists!\n"; doSSH($host,$PORTHASH{$portnumber}); print "Here AGAIN!5\n"; exit(fork); }else { print"Port: ".$portnumber." is not defined. Host: ".$host.".\n"; $client->send('Not Defined!!'); last; } } else { print "In ELSE \n"; } } continue { close $client; kill CHLD => -$$; } sub doSSH { my $host=$_[0]; my $interface=$_[1]; my $s = Net::Appliance::Session->new($host); eval { $s->do_paging(0); $s->do_privileged_mode(0); #Send all SSH output to STDOUT so i can watch it $s->input_log(*STDOUT); print "Connecting to $host\n"; $s->connect(Name => $USER, Password =>$PASSWORD); $s->begin_configure(); $s->cmd("interface ".$interface." "); $s->cmd("macro apply 8021x"); $s->end_configure(); $s->close; }; if ( UNIVERSAL::isa($@,'Net::Appliance::Session::Exception') ) { print $@->message, "\n"; # fault description from Net::Appliance::Session print $@->errmsg, "\n"; # message from Net::Telnet print $@->lastline, "\n"; # last line of output from your appliance # perform any other cleanup as necessary $client->send('FAILED--'); }else { print "SUCCESS! \n"; $client->send('Successful'); } }