in reply to Logging into telnet using a socket

Have seen this done by using IO::Socket to connect to many Cisco devices for management purposes.

First, create the connection to the socket:

use IO::Socket; my $sock = IO::Socket::INET->new( PeerAddr => $host, PeerPort => 23, Proto => 'tcp', Timeout => 3, ); if ( ! $sock ) { print( STDERR "$host: could not connect: $!" ); exit( 1 ); }

It's up to you whether you use blocking or non-blocking, or indeed if you want to wait for a prompt first or not. Here's an example of blocking with waiting for a prompt:

my ( $r, $resp ); while (sysread($sock, $r, 1024) >= 1) { $resp .= $r; $resp =~ /prompt>/ and last; } print $sock "$username\n";

Then I expect you'd continue using the socket from that point onwards.

Update: as an aside this is a technique that is used in a company I have been working at. There was so much information to be gathered that significant optimisation of the parsing routines had to be done to minimise CPU utilisation given the number of devices being polled using telnet. The above comments allude to Net::Telnet being required.. but the technique of just using IO::Socket has proved rather worthwhile for the purposes I've seen it used for.

Replies are listed 'Best First'.
Re^2: Logging into telnet using a socket
by mikeock (Hermit) on Oct 27, 2005 at 16:13 UTC
    Monarch Your code appears to be the solution that I am looking for. When I attach that snippet to my current program it hangs at the command prompt.

    I then login to the linux box that telnet is running on and run a who and the user that I am connecting with is not listed...

    If this is it then I will follow my Camel and do a local install of Net::Telnet.

    Thanks for the input

    EDIT: Castway, It is a limited functionality telnet server. The user that I am using does not require a password but all other users on the server do require a password

      Have you tried littering the above code with tracing commands to see what is being received from the box you're connecting to? Sometimes you need to initiate a telnet connection with a "\n" just to kick it into life and send a prompt.

      e.g.

      my ( $r, $resp ); # kick the connection into life with a carriage return print $sock "\n"; # read in from the connection while (sysread($sock, $r, 1024) >= 1) { print( STDERR "At " . scalar(localtime(time())) . " received '$r'\n" ); $resp .= $r; $resp =~ /prompt>/ and last; } # print username to socket print( STDERR "Sending username..\n" ); print $sock "$username\n"; # debugging loop just to see what we're getting while (sysread($sock, $r, 1024) >= 1) { print( STDERR "At " . scalar(localtime(time())) . " received '$r'\n" ); $resp .= $r; }

      Note you can use the strace command as the root user on a linux box to attach tracing to your script. It will give you an idea of what text is coming and going from your socket. Very useful for debugging.

        Monarch,

        Hmmm... I am not going to have a chance to try this out until tomorrow. I will definitely try this out as soon as I get into work and see what we receive.

        I do not have access to the root user on the linux box so I am unsure of how to run the strace.