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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.