Scenario
You manage large amount of devices and you want to execute commands and gather output from them in an automated way.
Solution
Use the following Python script. The script uses a CSV file with the following device information:
1 |
IP address, username, password |
The script can be executed from shell against a CSV file:
1 |
python ssh_script.py devices.csv |
This example script will display an output of ‘show interfaces’ command from all devices listed in the CSV file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import paramiko import time import csv import sys def disable_paging(remote_conn): '''Disable paging on a Cisco router''' # remote_conn.send("terminal pager 0\n") time.sleep(1) # Clear the buffer on the screen output = remote_conn.recv(1000) return output if __name__ == '__main__': if len(sys.argv) < 2: print 'usage: ' + sys.argv[0] + ' csvfile' sys.exit(1) filename = sys.argv[1] f = open(filename, 'r') data = csv.reader(f) for item in data: # DEVICE CREDENTIALS ip = item[0] username = item[1] password = item[2] print ip, username, password # Create instance of SSHClient object remote_conn_pre = paramiko.SSHClient() # Add untrusted hosts remote_conn_pre.set_missing_host_key_policy( paramiko.AutoAddPolicy()) # Initiate SSH connection remote_conn_pre.connect(ip, username=username, password=password) print "SSH connection established to %s" % ip # Use invoke_shell to establish an 'interactive session' remote_conn = remote_conn_pre.invoke_shell() #print "Interactive SSH session established" # Strip the initial router prompt output = remote_conn.recv(1000) # Turn off paging disable_paging(remote_conn) # Send the router a command #remote_conn.send("\n") remote_conn.send("enable\n") # Wait for the command to complete time.sleep(1) output = remote_conn.recv(5000) print output # Send the router a command remote_conn.send(password) # Wait for the command to complete time.sleep(1) output = remote_conn.recv(5000) print output # Send the router a command remote_conn.send("\n") remote_conn.send("show interfaces\n") # Wait for the command to complete time.sleep(1) output = remote_conn.recv(5000) print output print '*'*100 f.close() |
Result
You will receive command output from all devices listed in the CSV file. Let me know in the comments if there are any issues!
Refererence:
i have an expert mode in my case and i need to send a password and run commands in expert mode. can you help me with this code ?
How do I open 50 simultaneous SSH sessions for the same device and send commands to it at the same time ?
You’ll need to use multi threading to achieve this: https://c0deman.wordpress.com/2014/05/22/multiple-ssh-login-python-paramiko-with-threading/
Hi,I have use case where i need to connect to multiple server and execute certain commands how to achieve this