-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathossi.py
More file actions
168 lines (149 loc) · 3.29 KB
/
ossi.py
File metadata and controls
168 lines (149 loc) · 3.29 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
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
import telnetlib
#TODO: Add try-except
import settings
#TODO: Add comments and docstrings
CMD = 'c'
FID = 'f'
DATA = 'd'
ERR = 'e'
EOF = 't'
END = EOF + '\n'
class Ossi():
def __init__(self):
#TODO: Add assertions
self.host = settings.HOST
self.port = settings.PORT
self.username = settings.USERNAME
self.password = settings.PASSWORD
self.pin = settings.PIN
def write_string(self, string):
self.tn.write(string.encode())
self.tn.write('\n'.encode())
def write_command(self, command):
string = CMD + command
self.write_string(string)
def write_field(self, field):
string = FID + field
self.write_string(string)
def write_data(self, desc):
string = DATA + desc
self.write_string(string)
def write_eof(self):
self.write_string(EOF)
def inline(self, output):
lines = output.split('\n')
return lines
def parse(self, output):
"""
Returns:
dict
{
'fields': {
1: str,
2: str,
3: str
},
'data':{
1: str,
2: str,
3: str
}
}
"""
fields = {}
data = {}
lines = self.inline(output)
for line in lines:
if line.startswith(CMD):
pass
elif line.startswith(DATA):
data.update({
len(data): line[1:]
})
elif line.startswith(FID):
fields.update({
len(fields): line[1:]
})
elif line.startswith(ERR):
# TODO: Add error handler
pass
elif line.startswith(EOF):
break
else:
pass
result = {
'fields': fields,
'data': data,
}
return result
def single_to_dict(self, parse):
result = {}
for i in range(len(parse['fields'])):
fids = parse['fields'][i].split('\t')
data = parse['data'][i].split('\t')
if len(fids) != len(data):
# print('ERROR')
break
for i in range(len(fids)):
result.update({
fids[i]: data[i]
})
return result
def multiple_to_dict(self, parse):
fields = parse['fields']
data = parse['data']
instances = {}
result = {}
count = 0
while len(data):
inc = count * len(fields)
instance_data = {}
for key in fields:
instance_data.update({
key: data.pop(key + inc, None)
})
instance = {
'fields': fields,
'data': instance_data
}
instances.update({count: instance})
count += 1
for inst in instances:
result.update({
inst: self.single_to_dict(instances[inst])
})
return result
def command(self, command, params=None):
self.write_command(command)
# TODO: Add connection lost handler
if params:
fields = ''
data = ''
for key in params:
fields += '\t%s' % key
data += '\t%s' % params[key]
self.write_field(fields)
self.write_descript(data)
self.write_eof()
output_bytes = self.tn.read_until(END.encode())
return output_bytes.decode('utf-8')
def list_trunks(self):
t = self.command('list tru')
parse = self.parse(t)
result = self.multiple_to_dict(parse)
return result
def connect(self):
self.tn = telnetlib.Telnet(self.host, self.port)
self.tn.read_until('login'.encode())
self.write_string(self.username)
self.tn.read_until('Password'.encode())
self.write_string(self.password)
self.tn.read_until('Pin'.encode())
self.write_string(self.pin)
self.tn.read_until('Terminal'.encode())
self.write_string('ossi')
self.tn.read_until(EOF.encode())
print('Connected')
def disconnect(self):
self.tn.close()
print('Disconnected')