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

Perl SSH2

by PilotinControl (Monk)
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

Replies are listed 'Best First'.
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?
[Corion]: Do you want to wait for a key press before closing the window?
[LanX]: I want the command line in the history
[tye]: -Mouse
[Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file.
[LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy
[Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history
LanX damns the cult of CB ;-)
LanX WTF WTF WTF
[LanX]: please forget my last 3 posts
[LanX]: Yeah option a doesn't go into history

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2017-03-27 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.