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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |