getting telnet banner

  • Thread starter Zebee Johnstone
  • Start date
Z

Zebee Johnstone

I want to connect to a machine's telnet server, read its banner, and
disconnect.

Net::Telnet doesn't appear to read banners, it doesn't return info till
you log on.


A simple: perl -e 'print `telnet clone`;'
will give the info back, but not till the telnet session has timed out.

The following code doesn't time out and doesn't return anything either.

I've tried printing \n to the socket before getting $answer, makes no
difference.

Anyone know what I can do to get the banner?


#!/usr/bin/perl -w
use strict;
use IO::Socket;

my $remote_host = shift;
my $remote_port = 23;
my $socket = IO::Socket::INET->new(PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto => "tcp",
Type => SOCK_STREAM)
or die "Couldn't connect to $remote_host:$remote_port : $@\n";

my $answer = join("\n",<$socket>);
close($socket);
print "answer is $answer\n";
 
D

David Efflandt

I want to connect to a machine's telnet server, read its banner, and
disconnect.

Net::Telnet doesn't appear to read banners, it doesn't return info till
you log on.

I don't know if a telnet banner typically has more than one line (I use
ssh instead), but following is a simple example that can read/print a line
(works as well for polling smtp servers, etc.):

use IO::Socket;
$remote = IO::Socket::INET->new (
Proto => "tcp",
PeerAddr => "localhost",
PeerPort => "telnet(21)",
)
or die "can't connect";
$_ = <$remote>;
print;
 
Z

Zebee Johnstone

In comp.lang.perl.misc on Sat, 9 Oct 2004 04:56:51 +0000 (UTC)
David Efflandt said:
I don't know if a telnet banner typically has more than one line (I use
ssh instead), but following is a simple example that can read/print a line
(works as well for polling smtp servers, etc.):

Mine does... it's one of the reasons I want to read it, as it changes
depending on certain events.
use IO::Socket;
$remote = IO::Socket::INET->new (
Proto => "tcp",
PeerAddr => "localhost",
PeerPort => "telnet(21)",
)
or die "can't connect";
$_ = <$remote>;
print;


I can't find any of those items in the IO::Socket perldoc, where do they
come from? What's the "telnet(21)" thing, telnet's port 23?

I tested it on my system and tcpdump tells me it is connecting, but I
see no output and it doesn't exit, same symptoms as my original program.

If I turn off telnet, then it correctly dies.

Zebee
 
Z

Zebee Johnstone

In comp.lang.perl.misc on Fri, 08 Oct 2004 22:43:28 -0500
l v said:
Try using Net::Telnet's getlines ...... untested snippet follows ...

$t = new Net::Telnet (Timeout => $secs,
Prompt => "/$serverName:.+/");
$mode = $t->errmode('return');

$t->open("$serverName.xxx.com");
@lines = $t->getlines;
$t->close;

As I said, Net::Telnet doesn't appear to return things till you log on.

This snippet times out and produces nothing.

Zebee
 
B

Ben Morrow

Quoth (e-mail address removed):
In comp.lang.perl.misc on Sat, 9 Oct 2004 04:56:51 +0000 (UTC)


Mine does... it's one of the reasons I want to read it, as it changes
depending on certain events.

Then read lines until you get a login prompt, and parse.
I can't find any of those items in the IO::Socket perldoc, where do they
come from?

Try the docs for the class you are using, IO::Socket::INET.
What's the "telnet(21)" thing, telnet's port 23?

A typo :). 'telnet(21)' as a port specification tells IO::Socket::INET
to look 'telnet' up with getservbyname and if that fails to use port 23
instead. Specifying 21 would mysteriously fail when you ran the script
on a machine with a broken /etc/services.
I tested it on my system and tcpdump tells me it is connecting, but I
see no output and it doesn't exit, same symptoms as my original program.

Try reading more lines; perhaps the first line from your telnet server
is blank.

Ben
 
D

David Efflandt

In comp.lang.perl.misc on Sat, 9 Oct 2004 04:56:51 +0000 (UTC)


Mine does... it's one of the reasons I want to read it, as it changes
depending on certain events.



I can't find any of those items in the IO::Socket perldoc, where do they
come from? What's the "telnet(21)" thing, telnet's port 23?

My mistake about the telnet port (21 is ftp). The snippet is modified
from something in "perldoc perlipc".
I tested it on my system and tcpdump tells me it is connecting, but I
see no output and it doesn't exit, same symptoms as my original program.

If I turn off telnet, then it correctly dies.

Maybe I was mistaken about it working for telnet. I use something similar
to read status of servers that return a 1 line status (smtp, ftp, etc.),
or a loop to send a request and read http reply headers headers (and/or
content). I don't have a local telnetd to test it on and when I tried it
to my Unix ISP (which should have returned a banner with SunOS version) it
hung with no output..
 
K

krakle

l v said:
Well, well, well, a bit snippy aren't we. I'm not sure then why I
wasted more time on *your* problem. But I did....

Well it did time out and produce nothing... So... :)
 
J

Jay Rogers

Zebee Johnstone said:
I want to connect to a machine's telnet server, read its banner, and
disconnect.

Net::Telnet doesn't appear to read banners, it doesn't return info till
you log on.

Not true. If you don't want to login, then just don't call
Net::Telnet::login().

This will do what you ask for:

use Net::Telnet;
$t = new Net::Telnet;
$t->open($host);
($banner) = $t->waitfor('/login: ?$/');
$t->close;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top