Skip to content

Instantly share code, notes, and snippets.

@twr14152
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twr14152/96b3f95155bbaca6fa62 to your computer and use it in GitHub Desktop.
Save twr14152/96b3f95155bbaca6fa62 to your computer and use it in GitHub Desktop.
Python ssh script for network devices
host_commands = []
f = open('show_commands.txt', 'r')
for line in f:
host_commands.append(line.strip())
f.close()
print host_commands
device_list = []
f = open('device_list.txt', 'r')
for line in f:
device_list.append(line.strip())
f.close()
print device_list
#
import paramiko
import time
import getpass
import os
from host_file import device_list
from commands_file import host_commands
UN = raw_input("Username : ")
PW = getpass.getpass("Password : ")
# For loop allows you to specify number of hosts
for ip in device_list:
print ip
twrssh = paramiko.SSHClient()
twrssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
twrssh.connect(ip, port=22, username=UN, password=PW)
remote = twrssh.invoke_shell()
remote.send('term len 0\n')
remote.send('enable\n')
remote.send('%s\n' % PW)
time.sleep(1)
#This for loop allows you to specify number of commands you want to enter
#Dependent on the output of the commands you may want to tweak sleep time.
for commands in host_commands:
remote.send(' %s \n' % commands)
time.sleep(2)
buf = remote.recv(65000)
print buf
f = open('sshlogfile0001.txt', 'a')
f.write(buf)
f.close()
twrssh.close()
@twr14152
Copy link
Author

The benefit the commands_file.py and the host_file.py is that they will create lists out of the host and command text files you create. Appreciate the help Kevin Coming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment