Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Perl - running remote script doesn't return back the control to perl.

by bshah (Novice)
on Mar 20, 2014 at 18:23 UTC ( [id://1079129]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm pretty new to perl. I've written a small sample code which restarts my smtp service. The issue with the below code is, it after it runs /root/scripts/restart.sh on smailhostqa, the control doesn't comes back to Perl. I have to do ctrl-c to exit the script. Since I will be running this from the cron job, I would like to proper exit the script instead of hanging after the command. I thought $ssh->close(); should have closed the ssh session but it didn't.

My question is:

  • Is this the most efficient way ?
  • Is there any better way to do this?
  • How to resolve this issue ?

Your help is greatly appreciated.

#! /usr/bin/perl use strict; use Net::SSH::Expect; # Restarting the service.... my $ssh = Net::SSH::Expect->new ( host => "smailhostqa", user => 'root', raw_pty => 1 ); $ssh->run_ssh() or die "SSH process couldn't start: $!" +; $ssh->send("/root/scripts/restart.sh"); print "foo"; $ssh->waitfor('[root\@\w+ ~]#'); $ssh->close();

Janitorial edit: Code tags fixed: Use '</code>', not '<\code>' to close a code tag. Also repaired markup per Writeup Formatting Tips.

Replies are listed 'Best First'.
Re: Perl - running remote script doesn't return back the control to perl.
by McA (Priest) on Mar 20, 2014 at 18:43 UTC

    Hi,

    I'm not sure why you want a expect session for that. If it's not due to entering a password (which is not recommended anyway because you have to store it somewhere in your program) you could just simply call a subshell this way:

    my $rc = system("ssh -i private.key myhost.mydomain.tld '/root/scripts +/restart.sh'");

    McA

      Thank you for the great suggestion. I'll try it.

Re: Perl - running remote script doesn't return back the control to perl.
by salva (Canon) on Mar 21, 2014 at 09:03 UTC
    Net::SSH::Expect is not completely reliable because of the way it sends commands to the remote machine talking to a shell. Try using Net::OpenSSH instead.

    In any case, a likely cause for your problem is that the restart.sh script may leave behind processes with some of their stdio streams attached to the SSH channel. In that case the ssh process will wait for those processes to close their streams before returning control.

Re: Perl - running remote script doesn't return back the control to perl.
by zentara (Archbishop) on Mar 21, 2014 at 09:52 UTC
    A shell trick from Net::SSH2 might help. This will start execution, then detach completely and allow the SSH2 connection to close. Possibly the syntax for the exec of the shell command can be used in the modules you are using to completely detach, or used in Cron.
    #!/usr/bin/perl use Net::SSH2; # logon and object setup code omitted for clarity my $chan = $ssh2->channel(); $chan->blocking(1); $chan->exec("nohup /home/zentara/perlplay/net/zzsleep > foo.out 2> foo +.err < /dev/null &"); $chan->send_eof; exit;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thank you all for the great suggestion. I'll try it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-03-19 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found