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

Hey monks,

I'm still working on this ssh script. I can get my script to work, but when the firewall dameon is not running and I give the command to bring down the interface ($cmd2)on the other computer, perl just hangs there. Is there a way to break out of the ssh shell after bringing the interfaces down? Also, once I break out of the ssh shell, I want to run some commands on the local machine such as: ifconfig eth0 up and fwstart. How should I incorporate these commands into my script? I would rather not use Net::SSH or Net::SSH::Perl

Thanks for the help,
Dru

#!/usr/bin/perl -w my $result; my $sshprog="/usr/bin/ssh"; my $sshhost="192.168.2.3"; my $sshuser="root"; my $cmd1="ps -ef | grep -v grep |grep fwd"; my $cmd2="ifconfig eth0 down"; $result=`$sshprog $sshuser\@$sshhost $cmd1 2>&1`; if ($? == 0) { print "Firewall Dameon is running\n"; exit 0 } # end if ($? != 0) if ($? != 0) { print "Firewall Dameon is not running. Bringing down the interfaces\ +n"; $result=`$sshprog $sshuser\@$sshhost\ $cmd2 2>&1`; exit 0 } # end if ($result != 0)

 

Replies are listed 'Best First'.
Re: Can't Break Out of SSH Shell
by dws (Chancellor) on Jun 08, 2001 at 08:22 UTC
    You have a socket open to a remote machine, and on the far side you take down the interface, before the program you're running on the far side exits. I'm not at all sure what that does with existing connections, and whether ssh will eventually time out. It certainly means that the packets to cleanly close the connection won't propogate from the remote sshd to the local ssh.

    What happens when drop the remote interface from the (local) command line? Does ssh exit, or does it hang?

      It hangs from the command line also. How about running something like: ps -ef | grep -v grep |grep "ssh 192.168.2.3" saving the results to a hash and then perform a kill on the pid? But can you spawn a new shell while waiting for the ssh connection to close? I let it site for about 5 minutes and it still isn't timing out.

        If ssh also hangs from the shell, chances are good that the local TCP stack isn't seeing that the socket is closed (because it isn't, as far as the TCP stack is concerned, since no FIN packet ever arrives).

        Try rigging script on the far side that forks and exits in the parent process, while the child sleeps a few seconds and then drops the interface. When the parent process exits, sshd should close the socket.

Re: Can't Break Out of SSH Shell
by the_slycer (Chaplain) on Jun 08, 2001 at 08:37 UTC
    There is also a Net::SSH::Perl module which may make things a little easier
Re: Can't Break Out of SSH Shell
by Anonymous Monk on Jun 08, 2001 at 14:15 UTC
    do something like ifconfig eth0 down; ifconfig eth0 up! All commands in one line are to be designed to end with an open network connection.