use IO::Socket; use Net::Appliance::Session; use strict; use warnings; my ($sock,$LOCALPORT,$MAXLEN,$newmsg,$USER,$PASSWORD,$FILENAME,%PORTHASH,$client,$message); $LOCALPORT=2500; $MAXLEN=5151; $USER="REMOVED"; $PASSWORD="REMOVED"; $FILENAME="ports.txt"; #Pull in port mappings. Format is , I.E. 13,FastEthernet1/0/20 print "Opening Ports and Reading\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"; #Open a file to print things to (implemented later on), reusing the same variable. $FILENAME=">data.txt"; #open STDOUT, '>>data.txt' or die $!; #Open Listener on TCP 2500 #$sock= IO::Socket::INET->new(LocalPort => $LOCALPORT,Proto => 'udp') or die "Socket: $@"; $sock= IO::Socket::INET->new(LocalPort => $LOCALPORT,Proto => 'tcp', Reuse => 1, Listen => SOMAXCONN) or die "Socket: $@"; print "Starting... running on port: $LOCALPORT\n"; while(1) { $client=""; $client = $sock->accept(); $client->recv($message,1024); print "Here AGAIN!6\n"; while (1) { print "In here:\n"; print $message."\n"; #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; if (exists $PORTHASH{$portnumber}) { print "Exists!\n"; doSSH($host,$PORTHASH{$portnumber}); print "Here AGAIN!5\n"; last; }else { print"Port: ".$portnumber." is not defined. Host: ".$host.".\n"; $client->send('Not Defined!!'); last; print "Here AGAIN!3\n"; } } else { last; close $client; } last; } } #Clean up 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 { $client->send('Successful'); } print "Here AGAIN!4\n"; } #### use IO::Socket; use strict; my $LOCALPORT=2000; my ($newmsg,$MAXLEN,$TIMEOUT); $MAXLEN=5151; $TIMEOUT=5; while (1) { my $counter=10; while ($counter>0) { print "Sending in: ".$counter."\n"; $counter--; sleep 1; } print "Sending!\n\n"; my $sock= IO::Socket::INET->new(Proto => 'tcp', PeerPort=>'2500', PeerAddr=>'192.168.110.253') or warn "Socket: $@\n"; if ($sock) { print $sock "192.168.250.13:13"; close ($sock); } }