Hello, I'm new to Perl, found sample telnet code that would like to modify.

I have the following:

ip.list (list of host name and related IP addresses), example:

HOST1, 10.10.10.101

HOST2, 10.10.10.102

etc

Another file has referencing host and commands:

commands.list

HOST1, CommandA

HOST1, CommandB

HOST2, CommandC

HOST3, CommandD

HOST3, CommandE

HOST3, CommandF

etc

My 1st question is, how to match host name in command file to ip.list file and use the IP address to process?

Second, if connection is made to HOST1, issue first command, if second command is for the same HOST, then issue second command. If all commands are done for the connected host, close connection, then establish new connection to the next host and repeat.

#!/usr/bin/perl -w use strict; use Net::Telnet; my $log = './log.txt'; open LOG, ">>$log"; #open log file - append mode open IPS, "<ip.list"; #open ip.list file - read mode while(<IPS>){ my $ip = $_; #current iteration (line) of IPs file $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die' Prompt => '/# $/i'); $telnet->open($ip); $telnet->login('USERNAME', 'PASSWORD'); if($telnet->cmd('CommandA')){ print LOG "$ip CommandA successful"; }else{ print LOG "$ip Unable to Connect"; } } close LOG; #close files close IPS;

In reply to 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.