Perl: connect to network devices, run set of commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: connect to network devices, run set of commands
# 1  
Old 10-21-2013
Perl: connect to network devices, run set of commands

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

Using Net::OpenSSH
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);
  }
}

Partial output example from Net::OpenSSH
Quote:
Cisco CISCO829951/K9 (revision 1.1) with 995328K/53248K bytes of memory.
Processor board ID FTX1641ALBP
4 Gigabit Ethernet interfaces
1 Serial interface
2 terminal lines
1 Virtual Private Network (VPN) Module
2 Voice FXO interfaces
24 Voice FXS interfaces
1 Internal Services Module (ISM) with Services Ready Engine (SRE)
Cisco Unity Express 8.6.4 in slot/sub-slot 0/0
DRAM configuration is 72 bits wide with parity enabled.
255K bytes of non-volatile configuration memory.
250880K bytes of ATA System CompactFlash 0 (Read/Write)


License Info:

License UDI:

-------------------------------------------------
Device# PID SN
-------------------------------------------------
*0 CISCO2951/K9 FTX1641ALBP



Technology Package License Information for Module:'c37851'

-----------------------------------------------------------------
Technology Technology-package Technology-package
Current Type Next reboot
------------------------------------------------------------------
ipbase ipbasek9 Permanent ipbasek9
security securityk9 Permanent securityk9
uc uck9 Permanent uck9
data None None None

Configuration register is 0x2102
Connection to 10.1.1.3 closed by remote host







I also tried to get Net::SSH2 to work
Code:
#!/usr/bin/perl
use strict;
use warnings;
use strict;
use Net::SSH2;
my $user = 'popeye';
my $password = 'thesailor';
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;
 my $ssh = Net::SSH2->new();
#
  if(!$ssh->connect($host)){
   print("Connection Failed\n");
   exit(1);
   next;
  }
  if(!$ssh->auth_password($user,$password)){
   print("Authentication Failed");
   exit(1);
   next;
  }
#
 my $channel = $ssh->channel();

  foreach $cmd (@CMDS) {
   #chomp $cmd;
   my $command = $channel->exec("$cmd") or die $_;
   my $output;
   my $len = $channel->read($output,2048);
   print "output is $output\n";
   } #End command foreach 

} #End Host for each

Partial output example from Net::SSH2
Quote:

This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
Export Compliance Product Report Application

If you require further assistance please contact us by sending email to
export@cisco.com.

Cisco CISCO37951/K9 (revision 1.1) with 995328K/53248K bytes of memory.
Processor board ID FTX1645AL9S
4 Gigabit Ethernet interfaces
1 Serial interface
2 terminal lines
1 Virtual Private Network (VPN) Module
2 Voice FXO interfaces
24 Voice FXS interfaces
1 Internal Services Module (ISM) with Services Ready Engine (SRE)
Cis
Use of uninitialized value $_ in die at m2 line 37.
Died at m2 line 37.



The output for both are very similar. I get the first command response but not the second.

Thanks alot in advance.
# 2  
Old 10-21-2013
You might try the expect language, it's meant for automating picky interactive things.
# 3  
Old 10-21-2013
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Configuring central logging server for network devices

Hi I am very well aware of configuring central logging (syslog)server on solaris to capture logs of other solaris servers. But don't know how to capture the logs of network devices like Juniper , cisco etc on solaris server. Is this possible through syslog server of solaris. Is there any way we... (1 Reply)
Discussion started by: amity
1 Replies

2. Linux

Ports,number of slots,network devices - UNIX

Hi All, Can someone help me in finding out below information from ReHat linux,Solaris,AIX, hp-ux machine : 1. How to find total number of ports in supported by each slot. 2. name of ports that are used to transmit data to the printer 3. total number of slots on a network device (2 Replies)
Discussion started by: omkar.jadhav
2 Replies

3. Shell Programming and Scripting

Perl telnet to router run commands from file

I have a perl script that is called with a router name command list file and output file. The command file can be very large, up to 3k of commands. At first I dumped the command list file to an array and ran the entire file on the router and captured the output. It worked ok for a command list in... (1 Reply)
Discussion started by: numele
1 Replies

4. UNIX for Dummies Questions & Answers

How to set server's ip address, router, network mask and set if it is an internal or external ip?

Hello, I need to write a program which sets server's ip address, router, network mask. Program also should set if it is an internal or external ip. Maybe someone can help me ? Any information from u is very useful :b: I stopped at .. :( #!/bin/sh A=`hostname -i` echo "server ip address is $A"... (4 Replies)
Discussion started by: zagaruika
4 Replies

5. Shell Programming and Scripting

Perl - run sub routine for list of devices

Hi, Can anyone please correct the script such that it run on all the devices in the devicelist.txt file. The problem is when the script runs it only reads the first device in the list, configures the device and exists. Script: The devicelist.txt: device.crs... (1 Reply)
Discussion started by: sureshcisco
1 Replies

6. Shell Programming and Scripting

Run SQL command for a list of devices

Hi Please help me to resolve. Question: I can run this command to change the mode of a device with id=500 as below dbc "select device mode=3 where id=500;" How can i run the same query with a file contaning n number of ids ? file1.txt 12 234 34 500 34 45 Thanks in... (3 Replies)
Discussion started by: sureshcisco
3 Replies

7. Shell Programming and Scripting

How to use Perl's DBI connect when no mysql passwd is set?

I am getting the error "Can't call method "prepare" on an undefined value at..." with the following code: <code>my %dbh; my $dbh = DBI->connect("DBI:mysql:Customer_Data", $username, $password ) or die "Database connection... (1 Reply)
Discussion started by: vincaStar
1 Replies

8. Linux

Can USB Devices Be Exported Over a Network?

While I know that it's possible to use something like SANE to share a USB scanner over a network, or use NBD or iSCSI to share a USB flash or external HD over the network, I've been wondering about a raw USB <--> TCP/IP transport. Back in the late 90s, I swear I remember hearing about a project... (2 Replies)
Discussion started by: deckard
2 Replies

9. Shell Programming and Scripting

How to run a set of commands through ssh

I need to run a set of commands on a remote machine using ssh. it should also collect output and return status of each command. Can someone help me how to do this? (1 Reply)
Discussion started by: vickylife
1 Replies

10. UNIX for Advanced & Expert Users

Connect failed: No Devices Available

After installing an internal fax modem I get the message above when using the cu -l /dev/tty1A command. Any help would be most appreciated to the cu command to work again. Thanks. (3 Replies)
Discussion started by: cfaiman
3 Replies
Login or Register to Ask a Question