-
Notifications
You must be signed in to change notification settings - Fork 0
/
tacacs_server_update.py
203 lines (184 loc) · 10 KB
/
tacacs_server_update.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
line_break = "\r\n"
line_end = "\r"
vrp_cli_length = "screen-length 0 tempo"
#### Both HUAWEI&CISCO CPE's via Telnet&SSH via LDAP ######
##new features : Aware that CPE is Cisco or Huawei besides via Telnet or SSH and sends to appropriate commands.###
def connectionkapici(ip):
kapici_prompt = '.*\$ '
bash_prompt = "[tc036721@tcellsconn3 ~]$ "
telnet_prompt = ".*:"
configure_prompt = '.*)#'
user_prompt = '.*sername:'
pass_prompt = '.*assword: '
huawei_sys_prompt = '.*]'
cpe_prompt = ".*>"
enable_prompt = ".*#"
ssh_prompt = ".*? "
Username = "tc036721"
Password = "****"
kapici = "10.35.175.10"
SSH = paramiko.SSHClient()
SSH.load_system_host_keys()
SSH.set_missing_host_key_policy(paramiko.AutoAddPolicy)
telnet = telnetlib.Telnet()
# remote_connection = SSH.invoke_shell()
ldap_user = 'test'
ldap_pass = 'test'
tellcom = 'test'
tellcompass = 'test'
file1 = open("Cisco&Huawei-Telnet&SSH-LDAP3.txt", "a")
try:
SSH.connect(hostname=kapici, username=Username, password=Password, port=22)
with SSHClientInteraction(SSH, timeout=10, display=False, buffer_size=65535) as command:
command.expect(kapici_prompt, timeout=1)
command.send('bash')
command.expect(kapici_prompt)
command.send('telnet ' + ip) ###at first trying telnet
print(ip)
command.expect([user_prompt, kapici_prompt])
if command.last_match == kapici_prompt: #continue with ssh
command.send('ssh {}@{}'.format(ldap_user, ip))
print(ip + ' via SSH')
command.expect(ssh_prompt)
command.send("yes")
command.expect(pass_prompt)
if pass_prompt == command.last_match:
print("###########Router is reachable.###########")
# file1.write(ip + "successful access\n")
command.send(ldap_pass)
command.expect([cpe_prompt, enable_prompt])
# command.expect(cpe_prompt) or command.expect(enable_prompt)
if command.last_match == cpe_prompt:
print("###########Connection to Huawei Router was established.###########")
file1.write(ip + " Huawei" + " SSH" + " successful access\n") ####test if connection to router exactly##
command.send('terminal monitor')
command.expect(cpe_prompt)
command.send('sys')
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server template tacacs")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server authentication 192.168.0.1 third")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server authorization 192.168.0.1 third")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server accounting 192.168.0.1 third")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server authentication 192.168.0.1 sec")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server authorization 192.168.0.1 sec")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server accounting 192.168.0.1 sec")
command.expect(huawei_sys_prompt)
command.send("acl number 2307")
command.expect(huawei_sys_prompt)
command.send("rule 170 permit source 192.168.0.1 0.0.0.0")
command.expect(huawei_sys_prompt)
command.send("rule 180 permit source 192.168.0.2 0.0.0.0")
command.expect(huawei_sys_prompt)
command.send("q")
command.expect(huawei_sys_prompt)
command.send("q")
command.expect(cpe_prompt)
command.send("save")
command.expect(cpe_prompt)
command.send("q")
elif command.last_match == enable_prompt:
print("###########Connection to Cisco Router was established.###########")
file1.write(ip + " Cisco" + " SSH" + " successful access\n") ####test if connection to router exactly##
command.send('terminal length 0')
command.expect(enable_prompt)
command.send('conf t')
command.expect(enable_prompt)
command.send("tacacs-server host 192.168.0.1")
command.expect(enable_prompt)
command.send("tacacs-server host 192.168.0.2")
command.expect(enable_prompt)
command.send("ip access-list standard VTY_ACL")
command.expect(enable_prompt)
command.send("permit 192.168.0.1 0.0.0.0")
command.expect(enable_prompt)
command.send("permit 192.168.0.2 0.0.0.0")
command.expect(enable_prompt)
command.send("end")
command.expect(enable_prompt)
command.send("write")
command.expect(enable_prompt)
command.send("exit")
else:
print("###########CPE is uncreachable via LDAP,please check your user,pass info.(might be tellcom.)")
file1.write(ip + " CPE is unreachable via LDAP\n")
else:
print("###########CPE is uncreachable.###########") ###cannot access via telnet or ssh
file1.write(ip + " CPE is unreachable\n")
elif command.last_match == user_prompt: #continue with telnet
print(ip + ' via Telnet')
command.send(ldap_user)
command.expect(pass_prompt)
command.send(ldap_pass)
command.expect([cpe_prompt, enable_prompt])
if cpe_prompt == command.last_match:
print("###########Connection to Huawei Router was established.###########")
file1.write(ip + " Huawei" + " Telnet" + " successful access\n") ####test if connection to router exactly##
command.send('terminal monitor')
command.expect(cpe_prompt)
command.send('sys')
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server template tacacs")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server authentication 192.168.0.1 third")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server authorization 192.168.0.1 third")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server accounting 192.168.0.1 third")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server authentication 192.168.0.1 sec")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server authorization 192.168.0.1 sec")
command.expect(huawei_sys_prompt)
command.send("hwtacacs-server accounting 192.168.0.1 sec")
command.expect(huawei_sys_prompt)
command.send("acl number 2307")
command.expect(huawei_sys_prompt)
command.send("rule 170 permit source 192.168.0.1 0.0.0.0")
command.expect(huawei_sys_prompt)
command.send("rule 180 permit source 192.168.0.2 0.0.0.0")
command.expect(huawei_sys_prompt)
command.send("q")
command.expect(huawei_sys_prompt)
command.send("q")
command.expect(cpe_prompt)
command.send("save")
command.expect(cpe_prompt)
command.send("q")
elif enable_prompt == command.last_match:
print("###########Connection to Cisco Router was established.###########")
file1.write(ip + " Cisco" + " Telnet" + " successful access\n") ####test if connection to router exactly##
command.send('terminal length 0')
command.expect(enable_prompt)
command.send('conf t')
command.expect(enable_prompt)
command.send("tacacs-server host 192.168.0.1")
command.expect(enable_prompt)
command.send("tacacs-server host 192.168.0.2")
command.expect(enable_prompt)
command.send("ip access-list standard VTY_ACL")
command.expect(enable_prompt)
command.send("permit 192.168.0.1 0.0.0.0")
command.expect(enable_prompt)
command.send("permit 192.168.0.2 0.0.0.0")
command.expect(enable_prompt)
command.send("end")
command.expect(enable_prompt)
command.send("write")
command.expect(enable_prompt)
command.send("exit")
else:
print("###########CPE is uncreachable.###########") ###cannot access via telnet or ssh
file1.write(ip + " CPE is unreachable\n")
else:
print("###########CPE is unreachable at first.###########")
file1.write(ip + " CPE is unreachable at first\n")
except Exception as Error:
print(Error)
return 0
sys.exit()