CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl: connect to network devices, run set of commands

 



popeye
Novice

Oct 21, 2013, 10:36 AM

Post #1 of 1 (210 views)
Perl: connect to network devices, run set of commands Can't Post

I am trying to write a script for my own use that will allow me to connect to network devices, then run a set of commands.
I start with a list of ips in a text file. Each ip is on its own line.
I start with a second file of commands. Each command on one line.
for illustration .. the cmd.txt file exists and has the following commands resident

sh ver
sh run



Code
 
#!/usr/bin/perl
use strict;
use warnings;
use Net::OpenSSH;
my $user = 'popeye';
my $password = 'thesailorman';
use vars qw ($cmd $host @HOSTS @CMDS); #
#
open (SEED, 'ip.txt') or die ("Missing list of ips.");
@HOSTS = <SEED>;
close SEED;
#
open (CMD, 'cmds.txt') or die ("Missing list of commands.");
@CMDS = <CMD>;
close CMD;
#
foreach $host (@HOSTS) {
chomp $host;
print "$host\n";
my $ssh = Net::OpenSSH->new(host=>"$host", user=>"$user", port=>22, password=>"$password");
foreach $cmd (@CMDS) {
chomp $cmd;
print "$cmd\n";
$ssh->system($cmd);
}
}


Thanks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives