#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use IO::Handle;
use Net::Telnet;
my ($host, $username, $passwd) = ("localhost", "issac", "YYYYY");
autoflush STDOUT 1;
my $t = Net::Telnet->new();
# Log into unix box
print localtime().": Connect to $host...";
my $ok = $t->open(Host => $host,
Timeout => 20,
Errmode => "return");
die ("Cannot connect to $host telnet server") if !$ok;
print "OK\n";
print localtime().": Login to $host...";
$ok = $t->login(Name => $username,
Password => $passwd,
Timeout => 20,
Errmode => "return");
die ("Cannot login to $host telnet server") if !$ok;
print "OK\n";
# Read from server continually.
# Periodically send text to server.
while (1)
{
my $output = $t->get(Timeout => 10,
Errmode => "return");
if (defined $output)
{
# Recieved text from server
print localtime().": get() returned definite output=\"$output\
+"\n";
}
elsif ($t->eof())
{
# End of file - server has hung up.
print localtime().": get() returned EOF\n";
last;
}
else
{
# Timeout occured
print localtime().": get() returned TIMEOUT\n";
print localtime().": put() sending string\n";
# Send some crap to the server
my $ok = $t->put(String => "echo HELLO\n",
Timeout => 10,
Errmode => "return");
if (!$ok)
{
if ($t->eof())
{
print localtime().": put() returned EOF\n";
last;
}
else
{
print localtime().": put() returned TIMEOUT\n";
}
}
}
}
Here is some sample output:
gmargo@tesla 1753$ perl telnet_test.pl
Thu Nov 5 19:41:00 2009: Connect to localhost...OK
Thu Nov 5 19:41:00 2009: Login to localhost...OK
Thu Nov 5 19:41:11 2009: get() returned TIMEOUT
Thu Nov 5 19:41:11 2009: put() sending string
Thu Nov 5 19:41:11 2009: get() returned definite output="echo HELLO
"
Thu Nov 5 19:41:11 2009: get() returned definite output="HELLO
issac@tesla 79$ "
Thu Nov 5 19:41:22 2009: get() returned TIMEOUT
Thu Nov 5 19:41:22 2009: put() sending string
Thu Nov 5 19:41:22 2009: get() returned definite output="echo HELLO
"
Thu Nov 5 19:41:22 2009: get() returned definite output="HELLO
issac@tesla 80$ "
Thu Nov 5 19:41:33 2009: get() returned TIMEOUT
Thu Nov 5 19:41:33 2009: put() sending string
Thu Nov 5 19:41:33 2009: get() returned definite output="echo HELLO
"
Thu Nov 5 19:41:33 2009: get() returned definite output="HELLO
issac@tesla 81$ "
Thu Nov 5 19:41:44 2009: get() returned TIMEOUT
Thu Nov 5 19:41:44 2009: put() sending string
Thu Nov 5 19:41:44 2009: get() returned definite output="echo HELLO
"
Thu Nov 5 19:41:44 2009: get() returned definite output="HELLO
issac@tesla 82$ "
|