Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Perl SSH2

by PilotinControl (Scribe)
on Jul 22, 2013 at 02:32 UTC ( #1045563=perlquestion: print w/ replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

Good Evening Fellow Monks. I have a perplexing question I am using Net::SSH2 and I am issuing a Kill command as a stand alone script it works just fine once put into a bigger program it does not execute the command...it logs in just fine it just does not execute the command any ideas? Code below:

#!/usr/bin/perl -w # STAND ALONE SCRIPT use strict; use NET::SSH2; my $host = "22.333.44.555"; my $user = "user"; my $pass = "passwd"; my ($len, $buf); my $ssh2 = Net::SSH2->new(); $ssh2->debug(1); if ($ssh2->connect($host)) { if ($ssh2->auth_password($user,$pass)) { my $chan = $ssh2->channel(); $chan->blocking(0); $chan->exec("kill -9 `ps aux | grep ./services | grep -v grep | awk '{ +print $2}' | xargs`;\n"); select(undef,undef,undef,0.2); print $buf while ($len = $chan->read($buf,512)) > 0; print $buf, "\n"; $chan->close; } else { warn "auth failed.\n"; } } else { warn "Unable to connect Host $@ \n"; } sleep 5; # SCRIPT IN SUB FORMAT IN PROGRAM sub stopservices { my $host = "22.333.44.555"; my $user = "user"; my $pass = "passwd"; my ($len, $buf); my $ssh2 = Net::SSH2->new(); if ($ssh2->connect($host)) { if ($ssh2->auth_password($user,$pass)) { my $chan = $ssh2->channel(); $chan->blocking(0); $chan->exec("kill -9 `ps aux | grep ./services | grep -v grep | awk '{ +print $2}'`;\n"); select(undef,undef,undef,0.2); print $buf while ($len = $chan->read($buf,512)) > 0; print $buf, "\n"; $chan->close; } else { warn "auth failed.\n"; } } else { warn "Unable to connect Host $@ \n"; } } # END STOP SERVICES

Comment on Perl SSH2
Download Code
Re: Perl SSH2
by sparkyichi (Deacon) on Jul 22, 2013 at 17:54 UTC
    The script looks okay to me. If "stopservices" is not running you need to look at how you are calling it in the larger script.

    I would add a print line just after "sub stopservices {" to make sure that it is not running. Then look into how you are calling it.

    Sparky
    FMTEYEWTK

      In further review the stand alone script performs the function and the script windows exits after the " sleep 5; " function in the larger program the sub routine does not close down as the main program stays on to perform other functions. I need the " sub stopservices " to exit. In that case a while loop would be needed? Or should I head in a different direction? Thoughts?

        Can you provide more detail about the main script?

        How do you know stopservices is running and hasn't been skipped?

        If you need to get out of a sub you can use "return($var)" to return to the calling routine.

        Sparky
        FMTEYEWTK

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045563]
Approved by patcat88
Front-paged by sparkyichi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-08-29 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls