• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

Perl Net::Telnet and multiple devices

I have a nice little Net::Telnet script that we want to use to remotely reboot devices,  works great for a single IP address.  What I need is to somehow make it read the IP addresses from a text file containing a bunch of IP addresses.  My script is:

use Net::Telnet ();
$machine = '192.168.0.50';
$logfile = ".log";
$interval = "20";
$t = new Net::Telnet (Timeout => 10);
$t->input_log($logfile);
$t->open($machine);
$t->waitfor('/Login: $/');
$t->print("admin");
$t->waitfor('/Password: $/');
$t->print("password");
$t->prompt('/>/');
$t->cmd ("reboot");
$count = 0;
while ($count < $interval) {
        sleep(1);
        print "...wait...\n";
        $count++;
}
$t->input_log("");

I know I have to change either the "$machine = '192.168.0.50';" line or the "$t->open($machine);" line to make it look to an outside file, but I don't know how to do that.  I'm sadly very green at this.  It would be nice if somehow it could be made to do them one at a time as well, instead of all at once since we possibly could do this to several hundred devices at a time.  Thank you very much for any and all assistance!
0
NCTC-NET
Asked:
NCTC-NET
1 Solution
 
Pieter JordaanCommented:
Hi

The easiest way to do that, is to run your current script from another script.

Change:
$machine = $1;

Save it, then create your text file with all the ip addresses.

then execute the script from bash.
This example uses:
addresseslist - the name of your file containing a list of ip or hostnames, one on each line.
scriptname.pl - the name of the perl script.

for i in `cat addresseslist`; do perl scriptname.pl $i; done

This will do them one at a time.

You can also make it check for failures, and email you the result, using bash scripting.
0
 
NCTC-NETAuthor Commented:
I had to make 1 small change but this was exactly the solution I was looking for, thanks!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now