Net::Telnet hangs after initiating reboot command | Newbie | | Join Date: Feb 2007
Posts: 1
| | |
Hi,
I am facing a problem with the Perl Telnet Object. My Telnet Object hangs just after issueing reboot command on the remote host.
Please help me who faced this problem !!!
Thanks in advance
Siju Maliakkal
|  | Moderator | | Join Date: Oct 2006 Location: San Francisco, CA
Posts: 830
| | | re: Net::Telnet hangs after initiating reboot command
Well, of course it will hang, as the telnet session will be dropped immediately after you tell the remote server to reboot. I don't know the best method to solve this, but one alternative would be to setup an alarm. http://perldoc.perl.org/functions/alarm.html
I would also enclose that particular telnet command in an eval statement to trap the alarm call as well. Then do the appropriate cleanup associated with the fact that the telnet session is defunct. Something like this: -
eval {
-
alarm 5;
-
$t->cmd("reboot");
-
alarm 0;
-
};
-
# Proceed with cleanup
-
| | Newbie | | Join Date: May 2009
Posts: 1
| | | re: Net::Telnet hangs after initiating reboot command - ## I have tested this code on all Unix platforms (AIX, HP, Linux - All, Solaris)
-
## This is absolutely flawless
-
## Abhishek A. Kulkarni
-
-
use Net::Telnet;
-
use Net::Ping;
-
-
sub Reboot
-
{
-
my ($hostname, $username, $password) = @_;
-
-
print "\n Telnet for Reboot\n";
-
my $telnetHandle = getTelnetHandle($hostname, $username, $password);
-
-
print "\n Rebooting the machine \n";
-
-
@output = remoteCommand("which reboot", $hostname, $username, $password);
-
-
$command = $output[0];
-
chomp($command);
-
print "\n$command\n";
-
-
$telnetHandle->cmd("$command");
-
$telnetHandle->close;
-
-
print "\n telnet handle closed \n";
-
-
## Verify if host is pinging for some time even after reboot command
-
for($count=1;$count<=300;$count++)
-
{
-
if($ping->ping($hostname))
-
{
-
print "\n host is still pinging \n";
-
sleep(1);
-
}
-
else
-
{
-
print "\n Host went DOWN \n";
-
last;
-
}
-
}
-
## Verify if host has come up after reboot
-
$Reboot = 0;
-
for($count=1;$count<=180;$count++)
-
{
-
if($ping->ping($hostname))
-
{
-
print "\n Host came UP\n";
-
$Reboot = 1;
-
last;
-
}
-
else
-
{
-
print "#";
-
sleep(5);
-
}
-
}
-
-
if($Reboot == 0)
-
{
-
print "\n Host did not come UP even after waiting for 15 mins \n";
-
}
-
## Get telnet handle after reboot and return it to the calling subroutine
-
$telnetHandle = getTelnetHandle($hostname,$username,$password);
-
return $telnetHandle;
-
}
-
-
sub remoteCommand
-
{
-
my ($command, $hostname, $username, $password) = @_;
-
my $telnetHandle = getTelnetHandle($hostname, $username, $password);
-
-
@output = $telnetHandle->cmd(String => $command,Timeout => 4000,Prompt => ' /[#] $/');
-
-
return @output;
-
}
-
-
sub getTelnetHandle
-
{
-
my ($hostname, $username, $password) = @_;
-
my $prompt = '/#|$ $/';
-
my $timeout = 10;
-
my $errmode = "return";
-
my $port = 23;
-
-
$ping = Net::Ping->new();
-
-
if ($ping->ping($hostname))
-
{
-
print "INFO: Host is reachable\n";
-
-
$telnetHandle = new Net::Telnet (Errmode=>$errmode,Host=>$hostname,Prompt=>$prompt,Port=>$port,Timeout=>$timeout);
-
-
$telnetHandle->open("$hostname");
-
-
print "\n Telnet Handle Created \n";
-
-
$telnetHandle->login(Name=>$username,Password=>$password,Errmode => $errmode,Timeout =>$timeout);
-
-
$STDError = $telnetHandle->errmsg();
-
-
if ($STDError)
-
{
-
print "ERROR : $STDError";
-
}
-
}
-
else
-
{
-
print "ERROR: Host is not reachable";
-
}
-
return $telnetHandle;
-
}
|  | Site Moderator | | Join Date: May 2007 Location: New Hampshire
Posts: 2,565
| | | re: Net::Telnet hangs after initiating reboot command
You need to please keep in mind the age of the question(s) that you are reading. This post is about 2 years old and I doubt the user is even monitoring it.
To that end, I am closing this thread.
Regards,
Jeff
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|