Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Net::Telnet: remote job completion

by maskull (Novice)
on Feb 28, 2012 at 08:34 UTC ( #956614=perlquestion: print w/replies, xml ) Need Help??
maskull has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I'm using my Perl script to Net::Telnet to an AIX box and execute several consecutive scripts. Each one has to wait for the predecessor to finish before it can start.

Before Perl, I would log in using telnet, fire up the first script and then do "tail -f" on the job's log as each one writes to a separate one. When the job completed I'd get the last line saying "FINISHED". Then I'd know I can get on with the next one.

So how do I emulate that "tail -f" with Perl? Or do I take another approach to check whether the job has finished, e.g. grab the job's PID and periodically check if it's still there?

I'm afraid my code isn't really useful at this point. It is only the beginning. I'd appreciate any input. Thank you!

use Net::Telnet; my $telnet = new Net::Telnet(Timeout => 35,Errmode => 'die'); $telnet->open("") or die "Connection failed $telnet->errmsg + "; $telnet->waitfor('/login: $/i'); $telnet->print("user") or die $telnet->errmsg; $telnet->waitfor('/password: $/i'); $telnet->print("pass") or die $telnet->errmsg; $telnet->waitfor('/prompt> $/i'); @lines = $telnet->cmd("script_01") or die $telnet->errmsg;

Replies are listed 'Best First'.
Re: Net::Telnet: remote job completion
by Eliya (Vicar) on Feb 28, 2012 at 10:37 UTC

    It might be easier to modify the scripts such that they don't run in the background (though that of course depends on what exactly they're doing...).  In this case, you'd only have to wait for the prompt before starting the next script.

    The other option (untested — as I have telnet service disabled here) would be to run tail -f (like you're doing interactively) and use waitfor() to scan for the "FINISHED" line in its output.  Some tails also support a --pid=<PID> option (in combination with -f) to have it automatically terminate when some other process (PID) dies or completes.

      Assuming I go with the waitfor() option, do I still have to pass $telnet->cmd("\cC") to force Ctrl-C on my tail command after each job? Have no way of testing it right now but I would assume so if I'm to get the prompt again.

        Yes.  In order to try, I installed telnetd in a sandbox, and it seems to work fine (i.e. ->cmd("\cC") actually does get a SIGINT being delivered to the tail -f).

        ... $telnet->print("/path/to/script") or die $telnet->errmsg; $telnet->print("tail -f /tmp/testxxx") or die $telnet->errmsg; $telnet->waitfor('/FINISHED/'); $telnet->cmd("\cC") or die $telnet->errmsg; ... (run next script)

        where script goes in the background and writes a few lines to /tmp/testxxx, with the last line being "FINISHED".

Re: Net::Telnet: remote job completion
by wwe (Friar) on Feb 28, 2012 at 12:20 UTC
    Depending on your script you can try to integrate them into perl natively.

    If this is not an option I would fire the script and periodicaly check the log file ends with a string indicating successfull processing this not very effective if your files are long, maybe it's worth to check if the script is still running using ps.

Re: Net::Telnet: remote job completion
by sundialsvc4 (Abbot) on Feb 28, 2012 at 20:21 UTC

    I am not certain that I fully understand your request.   It seems to me that what you are saying is that you would like to write a Perl script, running on the AIX host, which would invoke the several consecutive scripts in turn.   You would then invoke that Perl script by telnet’ing to the host and invoking it from the command line.   Is this what you have in mind?

    If this is the case, it could be just as simple as using Perl’s qx// operator within that script.   (See: perldoc -f qx, then perldoc perlop.)   If all you want to do, truly, is to execute one subprocess after another in sequence, waiting for each one after the other to finish in its turn, then, “it’s just that simple.”

    To summarize, my notion ... and it may or may not, on second glance, be what you are looking for so far ... presupposes that a single script be placed and executed upon that AIX box, which invokes the various parts in sequence.   There is no “puppet master” on the remote side.   All in all, this seems to me to be considerably simpler.

      I'm running the Perl script from MS Win 2003 server. It telnets to AIX box on which I have no admin/root privileges - all I can do is run the scripts. It's pretty safe to assume the admin wouldn't like my putting my own scripts next to his toys.

        Perhaps it would be entirely appropriate to ask.   There are no special privileges required to run such a program on that box, although of course you might not yourself have the privileges needed to put them there.   (On the other hand, setting up an ordinary user-account there for you to put your scripts in might be “sure, no problem.”   There are, after all, no elevated privileges required to do any of this.)   It might well be argued that the impact on his system would be significantly less, and so he might well think that it’s a great idea.   (It would also be less troublesome and therefore would require less technical-support effort on the part of himself or his staff, versus any sort of Rube Goldberg contraption.)   Perhaps he will come back with a totally different idea (Tivoli?) that you had never even thought of.   Maybe his department isn’t aware that your department’s requirement exists.   Many a time a group who runs an internal computer service does not know everything about how other departments have contrived to use it ... especially if they are seen by others as being technically or politically unapproachable.

        In short, I wouldn’t avoid asking (or, as the case may be, suggesting that your manager do so), merely on the assumption that you know what the answer would be ... just sayin’.

        And of course, heck, yeah... well do I know how strange and squirrely corporate politics can sometimes be, and about the strangest most irrelevant things.

Re: Net::Telnet: remote job completion
by salva (Abbot) on Feb 29, 2012 at 14:11 UTC
    We are in 2012, how are you still using telnet to connect to an Unix box?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://956614]
Approved by moritz
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2017-01-24 03:17 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (201 votes). Check out past polls.