-
Notifications
You must be signed in to change notification settings - Fork 0
/
telnetCli.py
82 lines (68 loc) · 2.78 KB
/
telnetCli.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
import datetime
import os
import subprocess
import telnetlib
import time
def executecommands():
# router commands to be executed in the config terminal mode are given here.
tn.write(b"ntp server 10.3.125.1" + b"\n")
tn.read_until(b"#", 5)
tn.write(b"clock timezone IST 05 30" + b"\n")
tn.read_until(b"#", 5)
tn.write(b"archive" + b"\n")
tn.read_until(b"#", 5)
tn.write(b"log config" + b"\n")
tn.read_until(b"#", 5)
now = datetime.datetime.now()
buff = ''
resp = ''
# username and password for authentication when TACACS is used
username = "username"
unamepw = "password"
with open('ips.txt') as f:
for line in f:
line = line.strip()
# telnet credential and ip extraction for remote login and authentication from ips.txt
ip, telnetpw, enablepw = line.split(",")
with open(os.devnull, "wb") as limbo:
# check if host is up
result = subprocess.Popen(["ping", "-n", "2", "-w", "1000", ip],
stdout=limbo, stderr=limbo).wait()
if result:
print(ip, "Link Down - Site unreachable")
f = open('DownSites.txt', 'a+')
f.write(line + '\n')
f.close()
else:
try:
response = ""
tn = telnetlib.Telnet(ip)
print("\n" + ip + " connected!")
time.sleep(2)
response = tn.read_until(b"username: ", 5)
if b"username:" in response:
tn.write(username.encode('ascii') + b"\n")
tn.read_until(b":", 5)
tn.write(unamepw.encode('ascii') + b"\n")
tn.read_until(b"#", 5)
else:
tn.write(telnetpw.encode('ascii') + b"\n")
tn.read_until(b">", 5)
tn.write(b"terminal length 0" + b"\n")
tn.read_until(b">", 5)
tn.write(b"enable" + b"\n")
tn.read_until(b"Password: ", 5)
tn.write(enablepw.encode('ascii') + b"\n")
tn.read_until(b"#", 5)
tn.write(b"conf ter" + b"\n")
tn.read_until(b"#", 5)
print("Init Complete!")
executecommands()
output = tn.read_all()
print(ip, "Reachable ~ Command Executed")
tn.close()
fp = open(ip + '.txt', "wb")
fp.write(output)
fp.close()
except Exception as e:
print(ip, "ERROR: ", e)