Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

ssh connection lost during script running when box rebooted and script falls out...

by sqspat (Acolyte)
on Jul 21, 2009 at 11:20 UTC ( #781929=perlquestion: print w/replies, xml ) Need Help??

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

Hello I have a script that uses NET::SSH::Expect to log into a box, issue some commands and then reboot the box and then log back in and issue some more commands, unfortunately when I issue the reboot command the script stops as the ssh connection is lost .... any idea how to get the script not to fall out just because the box has gone down?? I want it just to sleep u ntil box comes back up and can log back in again .... see code below thanks
my $ssh = Net::SSH::Expect->new ( host => "obe140", user => 'root', password => 'admin', raw_pty => 1, timeout => 1 ); #eval { my $login_output = $ssh->login(); if ($login_output !~ /blah/) { die "Login has failed. Login output was $login_output"; } #}; my ($command_file, $commands)= undef; #Open the file with the commands to be issued open(FILE, "input/CMsettings") or die("Unable to open file"); my @commands = <FILE>; close (FILE); $ssh->exec("stty raw -echo"); my $command_run =undef; my @commands_run =(); sleep(1); #insert sleep here so command can be returned. my @issued_commands; #Issue the commands from the comand file foreach my $command1 (@commands){ chomp $command1; $command_run = $ssh->exec($command1); sleep(1); print $command_run; sleep(1); push (@commands_run, $command_run); print "\n\n-----------\n"; } print "\n\n ---- reboot ----\n"; # Issue reboot command my $reboot_command = $ssh->exec('reboot'); -- script drops out here w +hen would like it to hang around until the box is back up and running + so it can issue more commands ????? $ssh->close(); sleep(2); # ---- Login post reboot ----- my $ssh2 = Net::SSH::Expect->new ( host => "obe140", user => 'root', password => 'admin', raw_pty => 1, timeout => 1
  • Comment on ssh connection lost during script running when box rebooted and script falls out...
  • Download Code

Replies are listed 'Best First'.
Re: ssh connection lost during script running when box rebooted and script falls out...
by quester (Vicar) on Jul 21, 2009 at 12:34 UTC
    The easiest way to handle this would be to log off before the box shuts down, so it doesn't hang the ssh session. You could try replacing the shutdown command with this (untested):
    (sleep 3; shutdown) & exit
    You will want to change the sleep 2; call in your script to be considerably longer to give the remote system a chance to shut down, reboot and start up its ssh daemon. Or, better, enclose the last call to Net::SSH::Expect->new in a loop and retry it a reasonable number of times, sleeping a while between tries. This will let you connect reasonably quickly without dying if the remote system unexpected takes longer than usual to reboot (for example, if you are running SELinux and the /.autorelabel file exists.)
Re: ssh connection lost during script running when box rebooted and script falls out...
by afoken (Canon) on Jul 22, 2009 at 09:56 UTC

    The shutdown command usually allows you to schedule the shutdown. Read the man page. On a typical Linux system, you can get away with "shutdown -r +5" to shut down in 5 minutes. During that five minutes, you can cancel the shutdown command with "shutdown -c".

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://781929]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2022-06-26 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (86 votes). Check out past polls.

    Notices?