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

Hi Monks,

Hopefully someone out there can tell me what is going on with my Telnet code. First here is the code

my $prompt = "someprompt"; my $t = new Net::Telnet (Errmode => "return", Prompt => '/\$ $/i'); my $ok = $t->open("10.1.46.16"); Error() if $ok != 1; # print "open $ok\n"; $ok = $t->login($username, $passwd); Error() if $ok != 1; # print "login $ok\n"; $ok = $t->print("PS1=$prompt"); Error() if $ok != 1; # print "set prompt $ok\n"; $ok = $t->waitfor("/$prompt/"); Error() if $ok != 1; # print "wait for set prompt $ok\n"; $ok = $t->print('CT'); Error() if $ok != 1; # print "CT $ok\n"; $ok = $t->waitfor("/$prompt/"); Error() if $ok != 1; # print "wait for CT $ok\n"; $ok = $t->print("$cmd"); Error() if $ok != 1; # print "run_ct $ok\n"; $ok = $t->waitfor("/$prompt/"); Error() if $ok != 1; # print "wait for run_ct $ok\n"; $ok = $t->print("ps -ef | grep run_ct | grep -v grep"); Error() if $ok != 1; # print "ps -ef $ok\n"; $ok = $t->waitfor("/$prompt/"); Error() if $ok != 1; # print "wait for ps -ef $ok\n"; my $ps = $t->get(); print "get = $ps\n"; my $process_id = (split /\s+/,$ps)[2]; print "\$process_id = $process_id\n"; $t->close; # Check if process has finished INF: for(;;) { print "loop\n"; my $t = new Net::Telnet (Errmode => "return", Prompt => '/\$ $/i'); my $ok = $t->open("10.1.46.16"); Error() if $ok != 1; print "open $ok\n"; $ok = $t->login($username, $passwd); Error() if $ok != 1; print "login $ok\n"; $ok = $t->print("PS1=$prompt"); Error() if $ok != 1; print "prompt $ok\n"; $ok = $t->waitfor("/$prompt/"); Error() if $ok != 1; print "wait for prompt $ok\n"; $ok = $t->print("ps -ef | grep run_ct | grep -v grep"); Error() if $ok != 1; print "ps -ef $ok\n"; $ok = $t->waitfor("/$prompt/"); # Error() if $ok != 1; print "\$ok = $ok\n"; print "wait for ps -ef $ok\n"; $ps = $t->get(); print "get = $ps\n"; my $process_id2 = (split /\s+/,$ps)[2]; print "process_id = $process_id\nprocess_id2 = $process_id2\n"; unless(defined $process_id2 && $process_id == $process_id2) { $ok = $t->print("CT"); Error() if $ok != 1; # print "CT $ok\n"; $ok = $t->waitfor("/$prompt/"); Error() if $ok != 1; # print "wait for CT $ok\n"; $ok = $t->print("cat $logfile"); Error() if $ok != 1; # print "cat $ok\n"; $ok = $t->waitfor("/$prompt/"); Error() if $ok != 1; # print "wait for cat $ok\n"; my $log_contents = $t->get(); print "$log_contents\n"; last INF; } $t->close; sleep 10; }

Now when i perform the first 'get()' i get the output from my 'ps' command. Now here is the strange thing.....when i do the second 'get()' i get my 'ps' command echoed in the output which then stuffs up my split. Why am i getting two different answers from exactly the same code. Any thoughts would be most welcome as this is driving me crazy!

TeraMarv.

Replies are listed 'Best First'.
Re: Strange behaviour - Net::Telnet
by PhilHibbs (Hermit) on Apr 15, 2005 at 10:31 UTC
    Firstly, running ps -ef | grep [r]un_ct (or any other regex that does not match itself) instead of ps -ef | grep run_ct | grep -v grep will save you a process.

    Secondly, maybe the waitfor is eating the first ps echo. castaway points out in the CB that cmd eats the echo, but print does not, so you should expect the echo, not be surprised by it.

    Finally, if you use a Perl regex in your script to remove the ps command, then it will also remove the ps echo.