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

Is there any way to buffer a session with Net::Telnet? I am trying to establish 2 Telnet sessions with a host. The first session to issue commands and the second session to grab "tail -f /var/log/messages" output. The code below works great for my first session.
$t = new Net::Telnet (Timeout => 10); $t->open($host); $t->login($Username, $Password); my @lines = $t->cmd($CMD); print @lines;
Is there a way for the second session to 1) dump it's buffer to something 2) then clear the buffer until I issues the next command via the first telnet session? Thanks!

Replies are listed 'Best First'.
Re: Buffering Session in Net::Telnet
by roboticus (Chancellor) on Mar 26, 2009 at 12:37 UTC
    ewhitt:

    I've never used Net::Telnet, but reading the docs shows that you can directly access the input buffer, so you should be able to do something as simple as starting two different sessions and alternating your attention between the two, something like (the completely untested, and probably wrong):

    # Establish the two telnet sessions $t1 & $t2 with a one-second # timeout to allow reasonably quick interaction with them. $t1 = new Net::Telnet (Timeout => 1, Errmode => 'return'); $t1->open($host); $t1->login($Username, $Password); $t2 = new Net::Telnet (Timeout => 1, Errmode => 'return'); $t2->open($host); $t2->login($Username, $Password); # Now start a long-running task in session 1 and a tail -f in session +2 $t1->print("touch foo_log; long_running_job arg1 arg2 arg3 >>foo_log & +"); $t2->print("tail -f foo_log"); # Now print the tail -f session, and monitor the long-running task # for the "JOB COMPLETE" message while (1) { my @tail = $t2->getlines; print join("\n", @tail), "\n"; my @jobCpl = $t1->getlines; last if grep m/^JOB COMPLETE/ $t1->getlines; last if $t1->eof; } $t1->close; $t2->close;

    Be sure to review the docs, as there's plenty of interesting-looking information in there. (If we had any telnet servers around here, I think I'd spend an hour playing with this module, perhaps I'll use it to chat with an EMail server or something for fun/education.)

    ...roboticus
Re: Buffering Session in Net::Telnet
by VinsWorldcom (Prior) on Mar 26, 2009 at 13:00 UTC