scottdware has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I recently configured up Perl on Cygwin for my XP machine. I wrote a script using Net::Appliance::Session to log into Cisco switches and give me a count of how many disabled ports there are. Everything works fine, but I noticed that my SSH processes are not closing. For example, If I run the script against 6 switches, I have 6 SSH processes still open. Has anyone else ever seen this. And do you possibly know what I could do to try and fix it? Thanks. P.S. Here's the script code:
#!/usr/bin/perl use Net::Appliance::Session; $switch_file = "/etc/scripts/switches.txt"; $user = "xxxxx"; $pass = "xxxxx"; $out_file = ">> /etc/scripts/portcount.txt"; if (-e "/etc/scripts/portcount.txt") { system("rm -f /etc/scripts/portcount.txt"); } open(OUT, $out_file); foreach $store_number (@ARGV) { chomp($store_number); open(STSWITCH, $switch_file); foreach $switch (<STSWITCH>) { chomp($switch); next if $switch =~ /^#/; next if $switch !~ /\.st${store_number}\.meijer\.com/; $s = Net::Appliance::Session->new( Host => $switch, Transport => 'SSH'); $s->do_privileged_mode(0); eval { $s->connect( Name => $user, Password => $pass, SHKC => 0); $count = 0; @output = $s->cmd("show ip int brie"); $s->close(1); foreach $line (@output) { next if $line !~ /FastEthernet/; next if $line !~ /administratively down/; $count++; } $total += $count; #print $switch ." unused ports: ". $count ."\n"; }; if ($@) { print "Error while accessing the device: $switch\n"; print "The error was: $@\n\n"; } } print OUT "Total for store #". $store_number .": ". $total ."\n"; $total = 0; close(STSWITCH); } close(OUT);

Replies are listed 'Best First'.
Re: Net::Appliance::Session - using SSH will keep process open
by quester (Vicar) on Jan 29, 2008 at 06:35 UTC
    In the examples in Net::Appliance::Session the session variable is lexical, e.g.,
    my $s = Net::Appliance::Session->new( Host => $switch, Transport => 'SSH');
    This causes $s to go out of scope and be destroyed at the end of each iteration of the enclosing foreach loop, which MAY close ssh and fix your problem. I'm not able to test it at the moment, though.

    If that doesn't work, the next thing to try would be to log out of the switch explicitly, so the switch will close the session for you.

    $s->cmd("exit");
    Again, untested.

      I realize this is an old post, but since there was not a solution here, I figured I would post mine.

      I was having the same issue- Net::Appliance::Session (actually, Net::Appliance::Session::Transport::SSH) spawns a child proc for the ssh session, but even after I do $s->close() AND $s goes out of scope, I still see that child proc running when I do a ps. In my case, the ssh connection did not even succeed. Also, my program redirected SIG DIE to my own handler, which I thought might have been messing up Net::Appliance::Session's own signal handling (though this does not seem to be the case).

      Anyway, it turns out that Net::Appliance::Session keeps track of its own children. You can use the method childpid() to get the child's pid. Then, you can check if it is still running and kill it yourself if need be. I have pasted a snippet of my own code below, which seems to work. Note, this only handles the case when the the connection/login fails- you would need some similar code elsewhere in your script to handle the lingering process if you log in successfully and close out the session later.

      sub login_ssh { my $ip = shift; my $uname = shift; my $pword = shift; # I remapped the SIG handler for DIE elsewhere in my code. # If the SSH connection fails, it will be caught by # that handler, which I do not want. So remap it temporarily. my $old_sigdie = $SIG{__DIE__}; $SIG{__DIE__} = sub { mylog(MSG_NOTICE, "Caught SIG to DIE: $_[0]. Ignoring"); }; my $conn = Net::Appliance::Session->new( Host => $ip, Transport => 'SSH' ); eval { # eval {} probably not necessary in this particular case, # since we intercept SIG DIE $conn->connect(Name => $uname, Password => $pword); }; # The ssh procs are sticking around even after we fail to log in. # Get the child pid spawned by # Net::Appliance::Session::Transport::SSH so we can kill it explic +itly my $sshpid = $conn->childpid(); if ($conn->logged_in()) { # return an open Net::Appliance::Session session return $conn; } else { if ($@) { process_error($@); } $SIG{__DIE__} = $old_sigdie; $conn->close(); if (kill(0, $sshpid) > 0) { mylog("Child SSH proc $sshpid is still running. Killing i +t"); my $rc = kill TERM => $sshpid; } if (kill(0, $sshpid) > 0) { mylog("Child SSH proc $sshpid is still running. Kill -9'i +ng it"); my $rc = kill 9 => $sshpid; } return; } }
        Just noticed that the original poster was using Cygwin. The version (1.36) of Net::Appliance::Session that I am using actually implements this for Cygwin (see the DESTROY method in Net/Appliance/Session.pm). However, I was having this issue on Redhat on VMWare (using Tectia).