2

I have to run a command on around 1000 network devices and save the output to a file. In a file I have a list of all the network device names that have to be connected to using SSH. I then have to run a command, let's say show interface description for instance, then save the output to a file. The devices are all set up with a generic username and password that is the same on all devices. So basically I need a script to run these commands on all devices in the list file:

ssh user@device1
password: passwordhere
user@device1> show interface description
user@device1> exit

Then save all the output to a file. I am using Red Hat Enterprise Server.

1

3 Answers 3

4

The main thing you need to automate is the typing of the ssh password. There are two ways I know of that you can try, either set up password-less ssh or install sshpass:

  • Use sshpass to enter the password. If you have the DAG repositories set up, you should be able to install with yum:

    yum install sshpass
    

    If not, you can either download the RPM directly or add the repository and then install:

    1. Download the rpmforge-release package. Choose one of the two links below, selecting to match your host's architecture. If you are unsure of which one to use you can check your architecture with the command uname -i

    2. Install DAG's GPG key

      rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
      
    3. Install the downloaded package

      rpm -i rpmforge-release-0.5.2-2.el6.rf.*.rpm
      
    4. Install sshpass

      yum install sshpass
      

    Once you have installed sshpass, given a file called ips.txt, for example, that contains the names or IPs of your devices one per line, you can automate the process like this:

    while read ip; do 
      echo -n "$ip: " >> local_file.log;
      sshpass -p 'your password' ssh user@$ip remote_command >> local_file.log
    done < ips.txt
    
  • Alternatively, you can set up password-less ssh for each of the devices and skip installing sshpass:

    1. Set up password-less ssh

      ssh-keygen -t rsa
      while read ip; do 
        ssh-copy-id -i ~/.ssh/id_rsa.pub user@$ip; 
      done < ips.txt
      
    2. Run your jobs

      while read ip; do 
        echo -n "$ip: " >> local_file.log;
        ssh user@$ip remote_command >>local_file.log
      done < ips.txt
      
3

Some years ago I had a similar problem of automating remote connections. I used expect which is a very flexible tool, but sometimes a bit subtle to use. It will do anything a user can do by typing at the command line, but all possible reactions of the system(s) must be recognizable by the expect script (hence the name of the tool: it does not like the unexpected).
However it may be a sledge-hammer for your problem, and the solution proposed by @erdon may be lighter and more appropriate.

1
  • 1
    @Campino I would recommend expect as well, since it's a more generic tool what you learn of it to solve this problem can be applied to other problems in the future, not just this specific type of problem.
    – Bratchley
    May 24, 2013 at 12:07
0

You can use Python and Paramiko scripting for this. Sorry for the plug but I have already a script for this posted here "http://certifiedgeek.weebly.com/blog/network-scripting-via-ssh-with-python-and-paramiko" where the script is located in "https://github.com/cwgueco/netscript"

This script "netscript.py" has this options:

netscript.py -t <target> -u <username> -p <password> -i <command_file> -v
- the target is the IP address/hostname
- username/password should have allowed privilege to run commands
- command_file is a text file where the commands are listed
- v for verbose wherein you can pipe this to a file

Just create a script which will loop this python script while connecting to each device and running the same set of commands and piping each output to a file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .