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

To anyone that may have the vast knowledge that I am lacking.

I am creating a socket to connect to a telnet server. The problem is that the server requires a username but not a password.

I cannot find a way to pass the username in a socket connection. I know, I know, Use Net::Telnet. I would but the linux box that I am running off of I do not have access to install modules. I would like to figure out if this is possible or I am forced to find a way to install this to my home directory ?

Any help would be greatly appreciated. Thanks in advance!

Edit:Ended up using the Net::Telnet module installed to my home dir on the linux box.

Side note: ended up changing usernames that I was using as it is much easier to use Net::Telnet if you have a user that also has a password.

Replies are listed 'Best First'.
Re: Logging into telnet using a socket
by Tanktalus (Canon) on Oct 27, 2005 at 00:44 UTC

    I'd say your time is better spent learning how to install to your home directory than figuring out the minutae of sockets. Knowing how to locally install is much more transferable and long-term useful knowledge than socket programming because local installs can solve socket programs as well as lots of other problems.

Re: Logging into telnet using a socket
by pg (Canon) on Oct 27, 2005 at 01:27 UTC

    To implement the entire Telnet protocol (or a subset of it), username and password is just the first issue you are facing. So the level of difficulty is higher than you thought. To install Net::Telnet is a much easier way out.

    If you really want to go down the path to implement yourself (for whatever purpose, might be a good learning experience), you first need to get familiar with Telnet protocol, maybe read its specification first (RFC 854).

    For your direct issue: yes, you cannot specify password and username when you create TCP socket, as TCP socket never defines those concept (actually not even Telnet). Username and password are tranfered by the Telnet protocol (which is build on top of TCP) as data.

Re: Logging into telnet using a socket
by sgifford (Prior) on Oct 27, 2005 at 03:16 UTC
    Have you tried just writing the username and a CRLF to the socket? There's a pretty good chance that will work, though I don't have a telnet server around to test with.

    That said, everybody's right you should just figure out how to install a module in your home directory.

Re: Logging into telnet using a socket
by monarch (Priest) on Oct 27, 2005 at 04:33 UTC
    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.

      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.

Re: Logging into telnet using a socket
by mikeock (Hermit) on Oct 27, 2005 at 02:08 UTC
    Ok. That is what I was hearing from my peers at work but I wanted a second opinion before I tromped down that territory.

    I do also in the same program use 2 different socket call to connect to different ports that also have a telnet like interface. Due to the fact that they don't use a username and password it was easy to implement this. Since that was so simple I was thinking that there was a simple way to go about this and it appears that the solution was in front of me all the time.

    Thanks for the input and I will see how it goes.

Re: Logging into telnet using a socket
by castaway (Parson) on Oct 27, 2005 at 09:10 UTC
    Is it really a telnet server if it requires no password? "All" you need to do is connect the socket, read from it, send a username when it sends you a "login" type text, and away you go. What code have you tried?

    I wouldnt advise using Net::Telnet if you want to do actual "Telnet" like stuff, since its options support is quite broken. (Its fine for simple "running commands on telnet servers" though).

    C.