Perl Net::Telnet and multiple devices

Posted on 2011-05-09
Last Modified: 2012-05-11
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 = '';
$logfile = ".log";
$interval = "20";
$t = new Net::Telnet (Timeout => 10);
$t->waitfor('/Login: $/');
$t->waitfor('/Password: $/');
$t->cmd ("reboot");
$count = 0;
while ($count < $interval) {
        print "...wait...\n";

I know I have to change either the "$machine = '';" 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!
Question by:NCTC-NET
    LVL 11

    Accepted Solution


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

    $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. - the name of the perl script.

    for i in `cat addresseslist`; do perl $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.

    Author Closing Comment

    I had to make 1 small change but this was exactly the solution I was looking for, thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now