#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; use IO::Select; use Net::Telnet::Options; ## Settings: my $username = 'foo'; my $password = 'foobar'; ## Create an internet socket to connect to our server my $telnet = IO::Socket::INET->new( PeerAddr => 'my.host.name', PeerPort => 23, Proto => 'tcp', Timeout => 60 ) or die "Can't connect to my.host.name ($!)"; ## Create a Net::Telnet::Options object my $telnetopts = Net::Telnet::Options->new(); ## Write a sub that will be called when the server asks us about our t +erminal ## type. sub ttype_sb_callback { ## This module will be called with the suboption command, the cont +ent of ## the option, the data in this line, and the position of the subo +ption ## in the data. ## ## The command can have one of three values: ## "SEND" - Please send me an answer for this option ## "IS" - Here are the values you asked for ## "SB" - Neither SEND or IS were specified, some buggy options +do this! ## ## The option content is an empty string for SEND (since it is a c +ommand), ## contains the passed values for IS, and the entire command for S +B ## ## Both the data and the pos arguments are there "just in case" an +d are ## usually not needed. my ($cmd, $content, $data, $pos) = @_; if($cmd eq 'SEND') { ## Server is asking us for our terminal type, we need to send ## a suboption IS reply, to our telnet socket. $telnetopts->sendOpt($telnet, 'SB', 'TTYPE', 'IS', 'VT100'); + } } ## acceptDoOption is called to define callbacks to call when the serve +r ## asks us to DO something. ## The corresponding calls are: acceptWillOption, activeDoOption, ## activeWillOption ## Either the name of the option, eg 'TTYPE', or its number (24), can +be used. ## Each option is associated with a hashref of callbacks for the vario +us ## types of telnet option commands. ## Commands: ## WILL - Called when the server accepts to do an option ## WONT - Called when the server says no to an option ## DO - Called when the server says do an option ## DONT - Called when the server says dont do an option ## SB - Called when the server sends a suboption reply or request ## RAW - Called for commands that arent options, like EOR $telnetopts->acceptDoOption('TTYPE' => {'SB' => \&ttype_sb_callback} ) +; ## Send any option requests we have set up $telnetopts->doActiveOptions($telnet); ## Create an IO::Select object to tell us when we can read from the so +cket. ## And add our telnet socket. my $select = IO::Select->new(); $select->add($telnet); ## Start a neverending loop to use the socket while(1) { ## Check which handles (we have only one) have data ready for us. my ($handle) = $select->can_read(); ## Get data from the socket, in 1024 byte chunks my $incoming; $handle->recv($incoming, 1024, 0); ## An empty string means our socket is closed. if(!$incoming) { print "Socket closed!\n"; last; } ## Pass our data through the telnet options object, to let it ## read and respond to any options. $telnetopts->answerTelnetOpts($telnet); ## Use the rest of the data: if($incoming =~ /^login:/) { $telnet->print("$username\015\012"); } elsif($incoming =~ /^Password:/) { $telnet->print("$password\015\012"); } # elsif( .. ) # { # } ## Loop around and repeat until done .. }

In reply to Net::Telnet::Options example code by castaway

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.