-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCisco Device SSH with paramiko including thread
70 lines (60 loc) · 2.23 KB
/
Cisco Device SSH with paramiko including thread
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
import paramiko
import time
import datetime
import re
import sys
import getpass
import time
from datetime import date
from datetime import datetime
import os
import socket
import threading
def get_filename_datetime():
# Use current date & time to get a text file name.
return str(datetime.now().strftime('%Y_%m_%d %H_%M_%S'))
# Get full path for writing.
fname = get_filename_datetime()
user = input("Enter your M-account username: ")
password = getpass.getpass("Enter your M-account Password: ")
def ssh_conn(HOST):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(HOST, port=22, username=user, password=password, look_for_keys=False, timeout=8, banner_timeout=5)
connection = ssh.invoke_shell()
connection.send("show run | se vty\n")
time.sleep(10)
connection.send("configure terminal\n")
connection.send("\n")
connection.send("line vty 0 15\n")
connection.send("transport input ssh\n")
connection.send("exit\n")
connection.send("do write memory\n")
connection.send("\n" * 2)
time.sleep(3)
file_output = connection.recv(99999999).decode(encoding='utf-8')
hostname = (re.search('(.+)#', file_output)).group().strip('#')
print(file_output)
outFile = open(hostname + "_" + HOST + ".txt", "w")
outFile.writelines(file_output)
outFile.close()
ssh.close()
print("*" * 20 + " " + "%s is done" % hostname + " " + "*" * 20)
except paramiko.AuthenticationException:
print("X" * 20 + " " + HOST + ' === Bad credentials ' + "X" * 20)
except paramiko.SSHException:
print("X" * 20 + " " + HOST + ' === Issues with ssh service ' + "X" * 20)
except socket.error:
print("X" * 20 + " " + HOST + ' === Device unreachable ' + "X" * 20)
def SSH_Thread():
thread_instance = []
ips = [i.strip() for i in open("switchlist.txt")] # creates a list from input file
for HOST in ips:
trd = threading.Thread(target=ssh_conn, args=(HOST.strip("\n"),))
trd.start()
thread_instance.append(trd)
for trd in thread_instance:
trd.join()
if __name__ == '__main__':
SSH_Thread()