acondor has asked for the wisdom of the Perl Monks concerning the following question:
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
etcAnother file has referencing host and commands:
commands.list
HOST1, CommandA
HOST1, CommandB
HOST2, CommandC
HOST3, CommandD
HOST3, CommandE
HOST3, CommandF
etcMy 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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Login to multiple devices using telnet and issue different commands
by stevieb (Canon) on Aug 25, 2015 at 15:16 UTC | |
by acondor (Initiate) on Aug 25, 2015 at 17:37 UTC | |
by poj (Abbot) on Aug 26, 2015 at 07:00 UTC | |
|
Re: Login to multiple devices using telnet and issue different commands
by kcott (Archbishop) on Aug 25, 2015 at 16:58 UTC |