Woot woot. It's working as expected now. Everything is working as I want it to. Thanks for everyone's help. It creates a child process to SSH to the device, then kills the process when it's finished while the "Parent" keeps running. Here's what the final code looks like:

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,$clie +nt,$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', R +euse => 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 <Switch IP> : <Patch pa +nel mapping>, SSH to it and implement the macro stored on the switch + if ($message=~ /(.*)\:(\d*)/) { #Make sure the port mapping exists, If it does, connect SS +H session my $host=$1; my $portnumber=$2; #Pull in port mappings. Format is <Patch Panel> , <Switch +Port name> 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=<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: ".$h +ost.".\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::Applia +nce::Session print $@->errmsg, "\n"; # message from Net::Telnet print $@->lastline, "\n"; # last line of output from your appl +iance # perform any other cleanup as necessary $client->send('FAILED--'); }else { print "SUCCESS! \n"; $client->send('Successful'); } }

In reply to Re: TCP Server Exits Loop and Crashes with error by rojmab
in thread TCP Server Exits Loop and Crashes with error by rojmab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.