Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: A little demo for Net::SSH2

by Anonymous Monk
on Oct 29, 2010 at 18:14 UTC ( #868377=note: print w/replies, xml ) Need Help??


in reply to Re^3: A little demo for Net::SSH2
in thread A little demo for Net::SSH2

Hi, I want to run program in background in remote host. I tried this but does not work.
#!/usr/bin/perl -w use strict; use warnings; use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect("myhost.com") or die ("SSH2 Connect Error: $!"); $ssh2->auth_password($user, $pass) or die; my $chan1 = $ssh2->channel(); $chan1->blocking(0); $chan1->exec("./a.out &"); $chan1->close; $ssh2->disconnect();
Any idea will be appreciated. Thanks.

Replies are listed 'Best First'.
Re^5: A little demo for Net::SSH2
by zentara (Archbishop) on Oct 30, 2010 at 10:33 UTC
Re^5: A little demo for Net::SSH2
by afoken (Canon) on Oct 30, 2010 at 11:37 UTC

    Explain "does not work". What do you expect, what happens instead? Any messages?

    Add error checks to your code.

    Alexander

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

      zentara: No still does not work.

      afoken: Does not work means as follow: I run the perl script (Net::SSH2) in my machine host1, which is supposed to run a command "a.out" in machine host2. If I do

      $chan1->exec("./a.out");
      I can see in host2 this program running. (by command ps or top) But if I do
      $chan1->exec("./a.out &"); or $chan1->exec("nohup ./a.out"); (as zentara suggested)
      I do not see any program running in host2.

      The machines are NFS mounted, so all machines can see same home directory.

        Hi, you need both nohup and &, AND some minimal daemonizing. :-)

        I just tested this, and I see what the problem is. To run the program in the background, you need to close up/redirect it's stdin,stdout,stderr filehandles, like in the daemonization process. Anyways, this syntax works.

        #!/usr/bin/perl use warnings; use strict; use Net::SSH2; my $ssh2 = Net::SSH2->new(); #using keys authorization my $pass = 'ztester'; $ssh2->connect('localhost') or die "Unable to connect Host $@ \n"; # works when run from z's homedir because you need # permission to read the keys $ssh2->auth_publickey('zentara', '/home/zentara/.ssh/id_dsa.pub', '/home/zentara/.ssh/id_dsa', $pass ); my $chan = $ssh2->channel(); $chan->blocking(1); #close up the filehandles... rename foo to whatever you want $chan->exec("nohup /home/zentara/perlplay/net/zzsleep > foo.out 2> foo +.err < /dev/null &"); $chan->send_eof; exit;
        The zzsleep program is simply
        #!/usr/bin/perl $|= 1; while(1){ print time, "\n"; sleep 1; }

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2022-09-24 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (114 votes). Check out past polls.

    Notices?