-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjunos_hidden_search.py
65 lines (57 loc) · 2.35 KB
/
junos_hidden_search.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
#!/usr/bin/python3
HOST = "192.168.65.198"
user = "lab"
password = "lab123"
commandStart = "show version " # Must have space at the end!
import telnetlib
import re
alphabet = "abcdefghijklmnopqrstuvwxyz-1234567890."
PAUSE = 3
def SearchCommands(cmd, on_hidden_now = False):
for char in alphabet:
tn.write(cmd.encode('ascii') + char.encode('ascii') + b"\n")
totData=""
finished = False
while not finished:
inpData = tn.read_until(prompt.encode('ascii'), PAUSE)
totData = totData + inpData.decode('ascii')
if "---(more" in inpData.decode('ascii'):
tn.write(b" ")
else:
finished = True
cmdNext = cmd + str(char)
synt_error_exp_cmd = False
synt_error_period = False
if "syntax error, expecting <command>." in totData:
synt_error_exp_cmd = True
if "syntax error." in totData:
synt_error_period = True
if not (synt_error_exp_cmd or synt_error_period): # normal output or ambiguity
if on_hidden_now:
print("hidden command >> " + cmdNext)
else:
SearchCommands(cmdNext, on_hidden_now) # i.e. False
else:
l = re.findall(' *\^', totData)
lenToHat = len(l[len(l)-1])
if synt_error_period:
if lenToHat > lenPrompt + len(cmdNext):
SearchCommands(cmdNext, True) # Hidden command in progress
if synt_error_exp_cmd:
if lenToHat == 2 + lenPrompt + len(cmdNext):
if on_hidden_now:
print("hidden command >> " + cmdNext + " (incomplete)")
# else: print("Entering: " + cmdNext)
SearchCommands(cmdNext+" ", on_hidden_now)
if lenToHat > 2 + lenPrompt + len(cmdNext):
SearchCommands(cmdNext, on_hidden_now)
tn = telnetlib.Telnet(HOST)
tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"Password:")
tn.write(password.encode('ascii') + b"\n")
loginText = tn.read_until(b"> ").decode('ascii')
prompt = re.search(".*@.*", loginText).group()
print("Working with prompt = " + prompt)
lenPrompt = len(prompt)
SearchCommands(commandStart)