Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I agree with the others regarding using the Net::telnet module if you can afford to. I know that the implementation may be a bit more complicated, but you have less of a chance of having unexpected things happen in your script. For your case you would need to do something like this
use Net::Telnet; # initialize the telnet module my $username = 'user'; my $password = 'some_password'; my $command = 'ls -la'; print "enter the full domain name or IP of the machine that you want t +o telnet to\n"; chomp(my $host=<STDIN>); my prompt_regex = '/ .* ( \-?\@? \w*?\s? [\$#\%>~] \]? | \\\[\\e\[0m\\\] \[0m ) \s? /x'; $shell = new Net::Telnet ('Timeout'=>'7', 'Errmode'=> sub { report_error("login failure"); }, 'Prompt' => $prompt_regex ); $shell->open(Host=>$host); # opens the object $shell->login($username,$password); # login # then we issue some command with $shell->cmd("$command"); # or get the output of some command my $cmd_output = $shell->cmd("$command"); # and close the telnet object sleep 1; $shell->close; sub report_error { .... some code here to handle errors.... }
To give due credit, the above code is from a project that a friend and I work on for fun known as the Temerity Project. Though we both contributed to this script, he refined its implementation and added that awesome regular expression to check for a user's prompt type.
We originally tried using the expect.pm for interacting with telnet. This works too but is a lot more messy.
I hope that this is at least a bit of a help. The Net::Telnet module is a very nice module should you decide to use it. Take care, Joe

In reply to Re: Telnet via Perl fails by JoeJaz
in thread Telnet via Perl fails by shamala

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-29 11:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found