#!/usr/bin/perl use Getopt::Std; # So we can do the processing of the command line options use IO::Socket; # For the connection $port = '23'; #telnet port $timeout=20; #connection timeout # Process the command line options die "Usage: $0 -r -u -p -e \n" if (@ARGV < 6); exit if (!getopts('r:u:p:e:')); $username=$opt_u; $password=$opt_p; $enpassword=$opt_e; $router=$opt_r; main(); sub main(){ $i=0; # create a tcp connection to the specified host and port $handle = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $router, PeerPort => $port, Timeout => $timeout) or return (print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\nCan't connect to port $port on $router\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); # split the program into two processes, identical twins die "can't fork: $!" unless defined($childpid = fork()); # the if{} block runs only in the parent process if ($childpid) { # copy the socket to array while (defined ($line = <$handle>)) { @strings[$i] = $line; $i++; } kill("TERM", $childpid); } # the else{} block runs only in the child process else { print $handle $username."\n" if $username; # Use a username only if there is one; print $handle $password."\n"; print $handle "enable\n"; print $handle $enpassword."\n"; print $handle "sh uauth\n"; print $handle "exit\n"; close ($handle); exit; } #calculate the connected users $i=0; { foreach (@strings) {$i++ if /authenticated/; } print $i; } }