in reply to Login to multiple devices using telnet and issue different commands
G'day acondor,
Welcome to the Monastery.
Here's the basic logic I'd use to tackle this problem:
#!/usr/bin/env perl -l use strict; use warnings; use constant { IP => 0, CMD => 1 }; use Inline::Files; my %data; while (<IPLIST>) { chomp; my ($host, $ip) = split /,\s+/, $_, 2; $data{$host} = [$ip, []]; } while (<CMDLIST>) { chomp; my ($host, $cmd) = split /,\s+/, $_, 2; push @{$data{$host}[CMD]}, $cmd; } for my $host (sort keys %data) { print "Connect to '$host' at '$data{$host}[IP]'"; print "\tRun command '$_'" for @{$data{$host}[CMD]}; print "Disconnect from '$host' at '$data{$host}[IP]'"; } __IPLIST__ HOST1, 10.10.10.101 HOST2, 10.10.10.102 HOST3, 10.10.10.103 __CMDLIST__ HOST1, CommandA HOST1, CommandB HOST2, CommandC HOST3, CommandD HOST3, CommandE HOST3, CommandF
Output:
Connect to 'HOST1' at '10.10.10.101' Run command 'CommandA' Run command 'CommandB' Disconnect from 'HOST1' at '10.10.10.101' Connect to 'HOST2' at '10.10.10.102' Run command 'CommandC' Disconnect from 'HOST2' at '10.10.10.102' Connect to 'HOST3' at '10.10.10.103' Run command 'CommandD' Run command 'CommandE' Run command 'CommandF' Disconnect from 'HOST3' at '10.10.10.103'
Note that I have shown the logic related to your question but I have totally ignored the I/O: input dummied up using Inline::Files; output to STDOUT rather than appending to a logfile.
You'll need to write the code for the I/O yourself; however, the way you're currently doing this if far from optimal. You should use lexical filehandles, the 3-argument form of open and error checking (possibly using the autodie pragma).
— Ken
|
|---|