package CodeExecutor; use strict; use Net::Telnet; use threads; use threads::shared; #require "fileio.pl"; my $finished : shared=0; # Thread control var run("192.168.0.1","23"); sub run{ my $IP=shift; my $Port=shift; my $sock=getConnection($IP,$Port); # Get a telnet socket my $var:shared; # Traffic storage var require 'IO/String.pm'; my $io = IO::String->new($var); $sock->input_log($io); # Connect socketstream to $var my $th=threads->new(\&Update,\$var); # Start update thread program1($sock); # Run the program $th->join(); # Collect the thread if(defined $sock){ print "connection successful:".$sock; $sock->close(); } } sub program1{ my $sock=shift; eval{ print "Start"; $sock->waitfor('/Password:/'); $sock->print("cisco"); $sock->waitfor('/Router>/'); $sock->print("en"); $sock->waitfor('/Password:/'); $sock->print("cisco2"); $sock->waitfor('/Router#/'); sleep(2); $sock->print("show aliases"); $sock->waitfor('/Router#/'); $sock->print("show file systems"); $sock->waitfor('/Router#/'); }; if ($@){ print "Match Timeout"; } {lock $finished; $finished=1;} # Tells threads to stop #FileIO::save_log("traffic_log.txt",$var); # Save log to file } sub getConnection{ my $destIP=shift; my $destPort=shift; my $sock; eval{ $sock=new Net::Telnet(Host=>$destIP, Port=>$destPort, Timeout=>"20"); }; if ($@){ print "Could not get a connection"; exit(0); } return $sock; } # Update the screen with new Traffic # contained in $var sub Update{ my $var=shift; my $linenr=0; while(1){ my @bufflines=split(/\n/,$$var); # Print new lines only while($linenr<$#bufflines){ print $bufflines[$linenr++]."\n"; } {lock $finished; return if $finished;} sleep(1); # Update intervall } } 1; # Returning a true value #### package CodeExecutor; use strict; use Net::Telnet; use threads; use threads::shared; #require "fileio.pl"; my $finished : shared=0; # Thread control var my $var:shared; # Traffic storage var run("192.168.0.1","23"); sub run{ my $IP=shift; my $Port=shift; my $con=getConnection($IP,$Port); # Get a telnet socket my $th=threads->new(\&Update); # Start update thread program1($con); # Run the program $th->join(); # Collect the thread if(defined $con){ print "connection successful:".$con; $con->close(); } } sub program1{ my $sock=shift; require 'IO/String.pm'; my $io = IO::String->new($var); $sock->input_log($io); # Connect socketstream to $var eval{ print "Start"; $sock->waitfor('/Password:/'); $sock->print("cisco"); $sock->waitfor('/Router>/'); $sock->print("en"); $sock->waitfor('/Password:/'); $sock->print("cisco2"); $sock->waitfor('/Router#/'); sleep(2); $sock->print("show aliases"); $sock->waitfor('/Router#/'); $sock->print("show file systems"); $sock->waitfor('/Router#/'); }; if ($@){ print "Match Timeout"; } {lock $finished; $finished=1;} # Tells threads to stop #FileIO::save_log("traffic_log.txt",$var); # Save log to file } sub getConnection{ my $destIP=shift; my $destPort=shift; my $con; eval{ $con=new Net::Telnet(Host=>$destIP, Port=>$destPort, Timeout=>"20"); }; if ($@){ print "Could not get a connection"; exit(0); } return $con; } # Update the screen with new Traffic # contained in $var sub Update{ #my $var=shift; my $linenr=0; while(1){ my @bufflines=split(/\n/,$var); while($linenr<$#bufflines){ print $bufflines[$linenr++]."\n"; } {lock $finished; return if $finished;} sleep(1); } } 1; # Returning a true value