Click here to get back home

Using Net::Telnet to connect to Perl server

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Using Net::Telnet to connect to Perl server strongcarl 01-22-2005
Posted by strongcarl on January 22, 2005, 12:57 am
Please log in for more thread options
Hello all!

I'm a newbie in this forum (and Perl), and would be grateful with any help
and comments you can provide with this problem that's been driving me
mad!!....

I'm trying to use the Net::Telnet module to connect to a Perl server and
issue a command. However, the results of the command are not returned to
Telnet. I'm not certain if its because the line separator is not a 'n'
but '.' or if its becuase the server is writing to a handle and not
STDOUT. Manually connecting to the server and running the command
displays the output to screen.

I have checked for errors at every stage and there are none - except that
the output is not returned.

******Perl server code:*******

#!/usr/bin/perl -w
use IO::Socket;
use Net::hostent; # for OO version of gethostbyaddr


$PORT = 9000; # pick something not in use

$server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1);

die "can't setup server" unless $server;
print "[Server $0 accepting clients]n";

while ($client = $server->accept()) {
$client->autoflush(1);
print $client "Command ";                        
while ( <$client>) {
next unless /S/; # blank line
if (/quit|exit/i) { last;
}
elsif (/who/i ) { print $client `who 2>&1`;
}
elsif (/disk/i ) { print $client `df -vh 2>&1`; }
elsif (/ps/i ) { print $client `ps -ef 2>&1`; }
elsif (/tl/i ) { print $client `tail log 2>&1` ;}
elsif (/motd/i ) { print $client `cat /etc/motd 2>&1`;
}
else {
print $client "Commands: quit, who, disk, motd, ps, tl,
n";
}
} continue {
print $client "Command? ";
}
close $client;
}



**********connecting telnet sub***********

sub connect_server {
$telnet = new Net::Telnet (Timeout=>10, #allows time for
login and find command to run etc
#Errmode=>'return',
dump_Log=> 'logfile',
Port=>'9000', #port matches that
of server running on NIP
Prompt=>'/Command/');
$telnet->open("$ip");
@result=$telnet->cmd('ps');
$telnet->cmd('exit');
print "result is: @result";
}


************sample debug log*********

cat logfile
> 0x00000: 70 73 0d 0a ps..

< 0x00000: 43 6f 6d 6d 61 6e 64 20 Command

< 0x00000: 70 73 20 2d 65 66 20 63 6f 6d 6d 61 6e 64 55 49 ps -ef
commandUI
< 0x00010: 44 20 20 20 20 20 20 20 20 50 49 44 20 20 50 50 D
PID PP
< 0x00020: 49 44 20 20 43 20 53 54 49 4d 45 20 54 54 59 20 ID C STIME
TTY
< 0x00030: 20 20 20 20 20 20 20 20 20 54 49 4d 45 20 43 4d
TIME CM
< 0x00040: 44 0a 72 6f 6f 74 20 20 20 20 20 20 20 20 20 31 D.root
1
< 0x00050: 20 20 20 20 20 30 20 20 30 20 4a 61 6e 32 31 20 0 0
Jan21
< 0x00060: 3f 20 20 20 20 20 20 20 20 30 30 3a 30 30 3a 30 ?
00:00:0
< 0x00070: 33 20 69 6e 69 74 0a 72 6f 6f 74 20 20 20 20 20 3 init.root



Similar ThreadsPosted
Net::SSH::Perl to connect to a remote server via a web page August 30, 2004, 10:02 am
net::telnet to ms telnet server May 19, 2005, 12:28 am
installing LIBNET and NET::TELNET on a windows 200 server June 16, 2005, 12:11 pm
Error In Using Perl To Connect To MySQL May 27, 2006, 2:59 am
Starting a perl program when I connect to the internet. September 22, 2004, 3:34 pm
unable to connect to SuSE using net::ssh::perl module March 9, 2006, 10:23 pm
Finding out the time on an FTP server using perl August 30, 2005, 5:27 am
MS SQL server and SQL DATETIME manipulation in perl April 29, 2005, 2:23 am
Net:SSH::Perl oddity when logging in to SSH V2 Server January 16, 2006, 7:03 am
net::ssh::Perl connecting to f-secure ssh server October 10, 2007, 4:07 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap