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