Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Any way to make it quicker?

by zendran (Initiate)
on Jun 12, 2014 at 03:21 UTC ( [id://1089639]=perlquestion: print w/replies, xml ) Need Help??

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

Ok, so I have a perl script that basically logs into servers on a text document, or what every you choose the script to use, and executes a set command. Now this script takes to long to go through a list of like 70, and sometimes it will also just randomly stop, it will say it's still executeing the script, but it wont send the commands any more, it will just sit there stuck. I need a way to stop this, and also make the script execute wayy quicker. The script can be found at http://pastebin.com/g9ts3ZKN

Replies are listed 'Best First'.
Re: Any way to make it quicker?
by atcroft (Abbot) on Jun 12, 2014 at 04:19 UTC

    As the Anonymous Monk suggested, you may be able to speed this up by processing the hosts in parallel. Another option, in addition to those suggested, may be Parallel::ForkManager.

    I did not look at your code (thus, the "may be" above). For future posts, please post your code in the post itself (surrounded by <code></code> tags, and possibly <readmore></readmore> tags if very lengthy)-besides being more convenient to those who may have only a limited time to consider your post when they first see it, it also helps to preserve the context for answers that may be posted (as, if memory serves, most PasteBin sites expire the code after some time, thus removing the context for readers who may find this post months or years from now if it relates to an issue they are having).

    Hope that helps, and that you find the information you seek.

Re: Any way to make it quicker?
by salva (Canon) on Jun 12, 2014 at 07:33 UTC
    use Net::OpenSSH::Parallel:
    my $pssh = Net::OpenSSH::Parallel->new; open(my $fh, '<', 'servers.txt'); while (<$fh>){ chomp; my ($user, $password, $host) = split(':', $_); $pssh->add_host($host, user => $user, password => $password); } $pssh->push('*', cmd => 'cd /tmp && perl p.pl'); $pssh->run;
Re: Any way to make it quicker?
by Anonymous Monk on Jun 12, 2014 at 03:35 UTC
      How do I do that? I'm a noob sorry.
Re: Any way to make it quicker?
by sundialsvc4 (Abbot) on Jun 12, 2014 at 13:45 UTC

    Two pure-guesses:

    1. Do you need to instantiate a new SSH2 object every time?   Can you re-use a single one?   Could resources be consumed, and not freed, say within the underlying library, when you create one object after another?   It is well worth exploring that possibility.
    2. “Always be tidy.”   If you connect() successfully, then explicitly disconnect().   You are telling each host, as well as the library on your side, “Say goodnight, Gracie.”   Expressly telling it to bring that connection to an orderly conclusion, and presumably to clean-up any internal resources associated with it.

    Also, I have sometimes found it to be “strangely useful” to add explicit pauses into such programs.   “Tell it to wait a second.”   After all, you are doing something that involves a remote computer system and a communications line, so there is something to be said maybe for giving it just a little extra time to take a deep breath.   Those “once in a blue moon” problems that cause your pager to go-off at 2:30 AM just might be avoided that way.

    It’s a good guess that this script is somehow stalling, and one common cause of stalls is simply that some resource or another runs-out.   Because there is a lot of asynchronous activity going on here, the effects might not be very consistent or reproducible.  

    I would also suggest that you add explicit calls to the error() method, perhaps in a list context so that you get everything it has to offer.   Don’t assume that everything worked, and also don’t assume that, if something did seem to work, there might not also be a useful message to be found.   Sometimes there are “warning” or “informational” messages to be had from a library that are extremely important, but you have to go digging for them.   Look for these messages at several strategic places and at least print them to STDERR.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-20 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found