-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadMultiSsh.py
More file actions
123 lines (115 loc) · 4.71 KB
/
Copy pathRadMultiSsh.py
File metadata and controls
123 lines (115 loc) · 4.71 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#python script version 0.3 December 2021 to ssh multiple Rad devices all
#with same username and password
#enter commands and save the entire log into a .txt file
#whose name is made by IP address of target device + date/time
#tested in python 3.8.5 on windows 7 pro 64 bit + pip version 20.1.1
#ssh libraries: paramiko version 2.7.2, netmiko version 3.3.2
#import required python libraries to execute this script
import paramiko
import time
import getpass
import sys
import time
import os
from datetime import datetime
print ("#########################")
print ("Python ssh script for Rad")
print ("######FaAndàIMan#########")
username=input("Enter Username: ")
password=getpass.getpass()
#open file ip.txt to retrieve addresses of target devices
#this ip.txt file must be in same folder of this python script
iplist=open("ip.txt")
for line in iplist:
#open file commands.txt to retrieve commands to be executed on target devices
#this commands.txt file must be in same folder of this python script
#commands.txt must begin with terminal pagination disable in order to collect
#entire outputs of certain long show commands
commands=open("commands.txt")
Host=line.strip("\n")
print ("Working on device",Host )
ip = Host
#ssh performed on default TCP port 22
try:
SESSION = paramiko.SSHClient()
SESSION.set_missing_host_key_policy(paramiko.AutoAddPolicy())
SESSION.connect(ip,port=22,
username=username,
password=password,
look_for_keys=False,
allow_agent=False)
time.sleep(2)
DEVICE_ACCESS = SESSION.invoke_shell()
print ("Executing and logging these commands:")
for line in commands:
DEVICE_ACCESS.send(line + "\n")
print (line)
time.sleep(3)
#each ssh command is executed every 3 seconds: increase it if some output is missing
#ssh session MUST be closed properly with a logout command otherwise log files will not be
#created or the ssh session will not work reliably
DEVICE_ACCESS.send("logout\n")
#Max size of log file that can be recorded is 1 Mbyte: it can be increased up to 2 Mbytes
output = DEVICE_ACCESS.recv(1024000)
str_all = output.decode('ascii')
timestr = datetime.now().strftime("%m%d%Y-%H%M%S")
log_file = Host + "_" + timestr + ".txt"
f = open (log_file,"w")
f.write(str_all)
emptyline = "\n"
confirmation = "Log closed on "
f.writelines(emptyline)
f.writelines(confirmation)
f.writelines(timestr)
print ("End of operations on device",Host)
print ("All logged into file ", Host, "_", timestr, ".txt", sep="" )
print ("Wait conclusion of the entire process before opening any log file")
#all operations are logged in a file with name: IP-address_Date-Time.txt
#this file is created in the same folder of this script
SESSION.close
commands.close()
except paramiko.AuthenticationException:
timestr = datetime.now().strftime("%m%d%Y-%H%M%S")
log_file = Host + "_ERROR_" + timestr + ".txt"
with open(log_file, "a") as f:
print("Ssh authentication failed: verify your credentials", file=f)
time.sleep(2)
except paramiko.SSHException as sshException:
timestr = datetime.now().strftime("%m%d%Y-%H%M%S")
log_file = Host + "_ERROR_" + timestr + ".txt"
with open(log_file, "a") as f:
print("Check if ssh is properly enabled on target device", file=f)
print(sshException.args, file=f)
time.sleep(2)
except paramiko.BadHostKeyException as badHostKeyException:
timestr = datetime.now().strftime("%m%d%Y-%H%M%S")
log_file = Host + "_ERROR_" + timestr + ".txt"
with open(log_file, "a") as f:
print("Check if ssh key is properly set on target device", file=f)
print (badHostKeyException.args, file=f)
time.sleep(2)
except Exception:
timestr = datetime.now().strftime("%m%d%Y-%H%M%S")
log_file = Host + "_ERROR_" + timestr + ".txt"
with open(log_file, "a") as f:
print("Unable to reach via ssh target device", file=f)
print("Check if IP address", Host, "is connected", file=f)
time.sleep(2)
print ("##############################")
print ("##########FaAndàIMan##########")
print ("##############################")
iplist.close()
#remove empty lines from log ( file with name: IP-address_Date-Time.txt )
#this file is created in the same folder of this script
filename = log_file
def remove_empty_lines(filename):
if not os.path.isfile(filename):
print("{} does not exist ".format(filename))
return
with open(filename) as filehandle:
lines = filehandle.readlines()
with open(filename, 'w') as filehandle:
lines = filter(lambda x: x.strip(), lines)
filehandle.writelines(lines)
remove_empty_lines(filename)
f.close()