-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSHTester.py
More file actions
124 lines (119 loc) · 4.2 KB
/
SSHTester.py
File metadata and controls
124 lines (119 loc) · 4.2 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
124
import paramiko
import time
def ApplyCommands(address,username,password,commands,output):
with open(commands, 'r') as myfile:
data = myfile.read()
data = data.splitlines()
f = open(output, 'w')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(address, 22, username, password)
chan = ssh.invoke_shell()
time.sleep(1)
str = ""
for lines in data:
chan.send(lines + "\n")
print(lines)
print("Waiting for response from server")
while 1:
time.sleep(3)
str = str + chan.recv(65535).decode('utf-8')
str2 = str.splitlines()
if "lines" in str2[len(str2) - 1]:
chan.send(" ")
elif "--More--" in str2[len(str2) - 1]:
chan.send(" ")
elif "<--- More --->" in str2[len(str2) - 1]:
chan.send(" ")
elif "---(more" in str2[len(str2) - 1]:
chan.send(" ")
else:
break
f.write(str)
myfile.close()
def CheckCommandsDiff():
with open('Aftershowoutput.txt', 'r') as file1:
with open('beforeshowoutput.txt', 'r') as file2:
diff = set(file1).difference(file2)
diff.discard('\n')
with open('Changes.txt', 'w') as file_out:
for line in diff:
file_out.write(line)
def CheckCommandsLines():
diff = ""
diff2 = ""
with open('Aftershowoutput.txt', 'r') as file1:
with open('beforeshowoutput.txt', 'r') as file2:
data1 = file1.read()
data2 = file2.read()
str = data1.splitlines()
str2 = data2.splitlines()
if len(str) <= len(str2):
for index, line in enumerate(str):
if (line != str2[index]):
diff = diff + line + '\n'
diff2 = diff2 + str2[index] + '\n'
else:
for index, line in enumerate(str2):
if(line != str[index]):
diff2 = diff2 + line +'\n'
diff = diff + str[index] + '\n'
with open('Changes2.txt', 'w') as file_out:
for line in diff2:
file_out.write(line)
with open('Changes.txt', 'w') as file_out:
for line in diff:
file_out.write(line)
def OutputResults(type):
print("*****************************************************************")
print("------------------------SSH-Changes-Output-----------------------")
print("*****************************************************************")
if type == "interfaces":
print("Before")
file1 = open('Changes2.txt', 'r')
for line1 in file1:
print(line1)
print("After")
file2 = open('Changes.txt', 'r')
for line2 in file2:
print(line2)
file1.close()
file2.close()
elif type == "diff":
print("Differences:")
file1 = open('Changes.txt', 'r')
for line in file1:
print(line)
file1.close()
try:
type = "interfaces"
address = "10.150.254.212"
username = "username"
password = "password"
commands = "commands.txt"
commandoutput = "output.txt"
show = "show.txt"
beforeshow = "beforeshowoutput.txt"
aftershow = "Aftershowoutput.txt"
print("put changes in commands.txt")
print("put show commands in show.txt")
print("\n")
print("starting preshow commands:" + '\n')
ApplyCommands(address, username, password, show, beforeshow)
print("finished preshow commands")
time.sleep(4)
print("starting command executions:" + '\n')
ApplyCommands(address, username, password, commands, commandoutput)
print("finished command executions")
time.sleep(4)
print("starting aftershow commands:" + '\n')
ApplyCommands(address, username, password, show, aftershow)
if type == "interfaces":
CheckCommandsLines()
elif type == "diff":
CheckCommandsDiff()
OutputResults(type)
print("done")
finally:
pass