Last active
August 29, 2015 14:27
-
-
Save twr14152/96b3f95155bbaca6fa62 to your computer and use it in GitHub Desktop.
Python ssh script for network devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
host_commands = [] | |
f = open('show_commands.txt', 'r') | |
for line in f: | |
host_commands.append(line.strip()) | |
f.close() | |
print host_commands |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
device_list = [] | |
f = open('device_list.txt', 'r') | |
for line in f: | |
device_list.append(line.strip()) | |
f.close() | |
print device_list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.