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.


In reply to Re: Logging into telnet using a socket by monarch
in thread Logging into telnet using a socket by mikeock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.