kumarnaveen has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a list of around 150 servers to access. I need to write a telnet script to get the information from the nodes. I wrote a script which gets the IP address and tries to telnet to machine and gets the information. This script exits if suppose any of the server is un-accessible. Please have a look into this script and let me know how can I solve this.

use Net::Telnet(); print "count is $i\n"; open (OUT,">D:/PERL/TELNET_OUTPUT.txt") or die "could not open new +.txt\n"; open(FILE, "D:/PERL/ip.txt") || die "Failed\n"; foreach $x (<FILE>) { $telnet = Net::Telnet-> new(Host =>"$x",timeout =>13)or $telnet_er +r=1 ; if ($telnet_err){ print "Unable to telnet to $host machine\n";} $telnet -> login(root,siemens) or $login_err=1; if ($telnet->cmd("uname") == "Linux") { print OUT "\n<===========================>\n"; print OUT "\nWELCOME. You have Logged in to $x\n"; print OUT "\n----------------\n"; print OUT "Current Date: "; print OUT $telnet->cmd("date| tail -1"); print OUT "\n----------------\n"; print OUT "Serial Number of the Machine is: "; print OUT $telnet->cmd("dmidecode -t system | grep Serial"); print OUT "\n----------------\n"; print OUT "Operating System of the Machine is: "; print OUT $telnet->cmd("uname | tail -1"); print OUT "\n----------------\n"; print OUT "Host Name of the Machine is"; print OUT $telnet->cmd("hostname| tail -1"); print OUT "\n----------------\n"; print OUT " Disk Information of the Machine is"; print OUT $telnet->cmd("df -kh"); print OUT "\n-----------\n"; print OUT "IP Address of the Interfaces are : \n"; print OUT $telnet->cmd("/sbin/ifconfig -a | grep inet| cut -d\ +" \" -f2"); print OUT "\n-----------\n"; print OUT "MAC Address of the INterfaces are:\n"; print OUT $telnet->cmd("/sbin/ifconfig -a | grep ether"); }

Replies are listed 'Best First'.
Re: Telnet Script exits if any of the machine is un-accessible
by dasgar (Priest) on Jul 18, 2011 at 16:01 UTC

    I think you'll want to change the errmode to be return. By default, the errmode is set to die.

    Basically, change you connection line to be something like:

    $telnet = Net::Telnet->new(Host =>"$x",timeout=>13,errmode=>return);

    Then you'll need to check $telnet to determine if it was able to successfully connect or not. Check the documentation of Net::Telnet for more details on this.

      You can user nmap instead of all these things.It would be better for you. nmap -p9090 172.16.177.103 | grep 9090 | awk '{print $2}' .This is an Example for that
Re: Telnet Script exits if any of the machine is un-accessible
by kennethk (Abbot) on Jul 18, 2011 at 15:54 UTC
    Does your script give you any debugging information when is exits? I suspect it is as simple as you are not handling the error conditions properly, as described in Net::Telnet (look for errmode). You could probably fix this simply with a next:

    if ($telnet_err){ print "Unable to telnet to $host machine\n"; next; }
Re: Telnet Script exits if any of the machine is un-accessible
by flexvault (Monsignor) on Jul 18, 2011 at 15:55 UTC

         $telnet -> login($account,$password) or $login_err=1;

    I hope your login is not a real account and password. Please change it to something else immediately!

    You're in a windows environment, so others on this list are better prepared to answer your real problem.

    But from a security perspective, just the hint that this account/password is real, will start a run on servers around the world!

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin