#!/usr/bin/perl -w use strict; use diagnostics; # cliserver.pl - UDP based CLI server: Sends to and receives from the CLI prompt use POSIX; use IO::Socket; use Net::Telnet; #my $time_to_die = 0; #sub signal_handler { # $time_to_die = 1; #} #If we get INT,TERM,or HUP signals we will call signal handler and die; #$SIG{HUP} = $SIG{INT} = $SIG{TERM} = \&signal_handler; #Fork a new process #my $pid = fork; #Let the parent exit #if ($pid) { # exit; #} #die "Couldn't fork: $!" unless defined($pid); #Dissociate from the controlling terminal that started us and stop #being part of whatever process group we had been a member of #POSIX::setsid() # or die "Can't start a new session: $!"; #Loop until the signal handler changes the value of $time_to_die to 1 #until ($time_to_die) { my($sock, $oldmsg, $newmsg, $hisaddr, $hishost, $MAXLEN, $PORTNO); $MAXLEN = 1024; $PORTNO = shift or die "usage: cliserver.pl "; $sock = IO::Socket::INET->new(LocalPort => $PORTNO, Proto => 'udp') or die "socket: $@"; my $debug = 1; my $TIMEOUT = 9999; my $t; my $prematch; my $CLISERVER = '10.89.224.199'; my @results = (); &TelnetToCli(\$t,'optiuser','optiuser',$CLISERVER); print "Awaiting UDP messages on port $PORTNO\n" if ($debug); #$oldmsg = "This is the starting message."; $hishost = 'unknown_host'; DATALOOP: while ($sock->recv($newmsg, $MAXLEN)) { my($port, $ipaddr) = sockaddr_in($sock->peername); $hishost = gethostbyaddr($ipaddr, AF_INET); print "Client $hishost said ``$newmsg''\n" if ($debug); if ($newmsg) { #print "newmsg:$newmsg\n" if ($debug); my $rc = &PerformCli(\$t,$newmsg); my $errormsg = $t->errmsg; if ($errormsg) { if ($errormsg !~ /pattern match timed-out/i) { #We had some kind of error message so lets try to reinit the object #and try the cli command one more time print "\n\ntelnet object is being reinitialized\n\n"; print "errormsg:$errormsg\n"; $t->close(); &TelnetToCli(\$t,'optiuser','optiuser',$CLISERVER); my $rc = &PerformCli(\$t,$newmsg); $errormsg = $t->errmsg; } } if ($rc) { #A syntax error was encountered. Tell the client $sock->send("Unsupported command ''$newmsg''\n"); } elsif ($errormsg) { #there was no match so send the log back #Send the response my $line = $t->lastline; $sock->send("$errormsg\n$line"); } else { my $line; my $append; foreach $line (@results) { $append .= $line; } $sock->send($append); } } #Kind of a catch all... if we have an error message from the $t object we will try to reinitialize it my $errormsg = $t->errmsg; if ($errormsg) { print "Unknown error with telnet object...will try to reinitialize it\n" if ($debug); $t->close(); &TelnetToCli(\$t,'optiuser','optiuser',$CLISERVER); } } print "recv: $!\n" if ($debug); #If we have not exhasted our retries then we go back to the while loop above if ($TIMEOUT > 0) { $TIMEOUT--; print "Retrying MAINLOOP:$TIMEOUT\n" if ($debug); goto DATALOOP; } else { print "Number of retries exceeded TIMEOUT value\nExiting now" if ($debug); } #close telnet and exit the program $t->close; exit; sub TelnetToCli { my ($t,$username,$password,$destination) = @_; $username = 'optiuser' if ($username eq ''); $password = 'optiuser' if ($password eq ''); $destination = 'priems05' if ($destination eq ''); $$t = Net::Telnet->new( Timeout => 30, Prompt => '/[\$%#>]/', Host => $destination); $$t->errmode("return"); $$t->login($username, $password); print "Telnet connection established with $destination\n" if ($debug); #set the errormode for CLI Timeout #$$t->errmode(\&CliTimeout); print "CLI session started\n" if ($debug); $$t->errmode("return"); } #End of TelnetToCli sub PerformCli { my ($t,$msgtoprocess) = @_; @results = (); #set the errormode $$t->errmode("return"); #my $newfile = 'inputlog.txt'; #my $fh = $$t->input_log($newfile); #Let's send our command to CLI and see if we get a match print "command:$msgtoprocess\n" if ($debug); #$$t->print($cmd); my $prompt = 'CLI>'; eval { @results = $$t->cmd(String => $msgtoprocess, Prompt => '/CLI>/'); }; return 0; } #End of PerlformCli sub Log { @results = (); $results[0] = shift; } sub CliTimeout { print "Problem starting CLI session\n"; } #End of CliTimeout #}