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

I am seeing an issue when using Net-SSH-Expect to SSH a linux Fedora-18 system. Specifically, I am seeing issue when I send the command “reboot” over SSH to the linux host to reboot the system.

Below is the sequence of commands I send.

1.Create a new Net::SSH::Expect connection.

2.Send a sample command to the remote linux host to make sure the connection is OK.

3.Send ‘reboot’ command over SSH. The command “reboot” also goes through perfectly fine.

4.After sending any command, I typically do ‘waitfor’ in Expect.pm & this is where the issue is observed.

5.‘waitfor’ calls ‘_sec_expect’ function again in Expect.pm, which throws down the below error.

3:Child PID 3328 exited with status 65280

The $before variable shows this. Connection to 192.168.200.1 closed by remote host. Connection to 192.168.200.1 closed.

Could anyone please advice on why i am seeing this error ? Shouldn't we expect the connection to close down after reboot is issued ?

To me looks like Expect.pm is expecting that the connection should be already there even after the reboot command is send.

Note that the issue was not observed with Fedora-14.

Replies are listed 'Best First'.
Re: Net-SSH-Expect & Fedora-18
by roboticus (Chancellor) on Jul 02, 2014 at 00:12 UTC

    hardy004:

    Perhaps Fedora 14 didn't terminate the TCP connections on reboot while Fedora 18 does? Or maybe there's a difference in the shutdown script for ssh.

    In any event, I'd either:

    • Use shutdown instead of reboot, providing enough time delay to process the next prompt,
    • Catching the error when you run reboot and discard it,
    • Make a small shell script that performs the reboot in the background after a time delay.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks for the reply. I believe so. After looking at some of the blogs, it seems like Fedora 18 behaves in the correct manner.

      What i am looking is for the Net::SSH to not throw out errors when we perform reboot over ssh

      Shutdown would not be an option

      Make a small shell script that performs the reboot in the background after a time delay.

      Can you elaborate more on this point ? How can this be useful ? I did run the reboot command in the background & didn't observe any benefit

        Shutdown would not be an option

        The reboot command has no option for a delay, but there is a reboot option for the shutdown command, which also has a delay option.

        shutdown -t 10 -r

        will start a reboot after a 10 second delay. That should be plenty of time for the SSH session to end and the connection to close.

        hardy004:

        Sure, if you're using bash, you can do it like this:

        (sleep 10; ls -al) &

        The parenthesis tell bash to execute the stuff inside the parenthesis in another process, the & at the end tells it to make that process run in the background (i.e., immediately releasing the console so it can issue the next prompt). So then you'll get the next expected prompt, but the inner command will sleep for 10 seconds, and then list the directory contents. Just replace the ls command with the appropriate reboot command and you should be good to go.

        roboticus@sparky:~/tmp$ date Wed Jul 2 17:04:29 EDT 2014 roboticus@sparky:~/tmp$ (sleep 10; ls -al) & [1] 13508 roboticus@sparky:~/tmp$ date Wed Jul 2 17:04:32 EDT 2014 roboticus@sparky:~/tmp$ date Wed Jul 2 17:04:39 EDT 2014 roboticus@sparky:~/tmp$ total 144 drwxr-xr-x 2 roboticus roboticus 4096 Jun 19 11:18 . drwxr-xr-x 39 roboticus roboticus 4096 Jul 2 17:00 .. -rw-r----- 1 roboticus root 138227 Jun 12 15:30 ttt [1]+ Done ( sleep 10; ls --color=auto -al ) roboticus@sparky:~/tmp$ date Wed Jul 2 17:04:45 EDT 2014

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Net-SSH-Expect & Fedora-18
by oiskuu (Hermit) on Jul 02, 2014 at 16:37 UTC

    It serves no purpose to wait for the command shell to be killed. Exit the shell after "reboot", or better yet, issue an "exec reboot".