I would shove the information from the two files into a hash, then iterate over the details in a for loop. I've commented inline. Note the telnet portions are untested:

use warnings; use strict; use Net::Telnet; my $ip_list = 'ip.list'; my $cmd_list = 'commands.list'; # use the three-arg form of open, # and *always* check for problems open my $ip_fh, '<', $ip_list or die "Can't open IP list $ip_list: $!"; my %hosts; while (my $line = <$ip_fh>){ chomp $line; # split the line into host and ip, # then put this information into the %hosts hash my ($host, $ip) = split /,\s*/, $line; $hosts{$host}{ip} = $ip } close $ip_fh; open my $cmd_fh, '<', $cmd_list or die "Can't open CMD list $cmd_list: $!"; while (my $line = <$cmd_fh>){ # append each command to an array reference # under each host in the hash chomp $line; my ($host, $cmd) = split /,\s*/, $line; push @{$hosts{$host}{commands}}, $cmd; } close $cmd_fh; my $log = './log.txt'; open my $log_fh, '>>', $log or die $!; for my $host (keys %hosts){ # I think the instantiation of the object could # be done outside of the for() loop as long as each # session is closed, but I'm unsure my $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die', # you were missing a comma here Prompt => '/# $/i', ); # extract the ip for the current host my $ip = $hosts{$host}{ip}; $telnet->open($ip); $telnet->login('USERNAME', 'PASSWORD'); # loop over the command array, # and do something for each one for my $cmd (@{$hosts{$host}{commands}}){ if($telnet->cmd($cmd)){ print $log_fh "$ip $cmd successful"; }else{ print $log_fh "$ip Unable to Connect"; } } } close $log_fh;

-stevieb


In reply to Re: Login to multiple devices using telnet and issue different commands by stevieb
in thread Login to multiple devices using telnet and issue different commands by acondor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.