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


In reply to Re: Login to multiple devices using telnet and issue different commands by kcott
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.