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;