-
Notifications
You must be signed in to change notification settings - Fork 1
/
bulk_call_library.py
221 lines (209 loc) · 6.7 KB
/
bulk_call_library.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python
###############################################################################
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU public license as published by the Free Software
# Foundation.
#
# This program is distributed in the hopes that it will be useful but without
# any warranty, even implied, of merchantability or fitness for a particular
# purpose.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Author Jeffrey McAnarney from U.S. 8/8/2013
#
#
# Beautiful is better than ugly
# Explicit is better than implicit
# Simple is better than complex
# Complex is better than complicated
# Readability counts
#
###############################################################################
#####################################################################################################
# <COMMANDS> <PURPOSE> <ARGUMENTS>
# script-manager - parent command (fxo <slot/port>)
#########CHILDREN########
# detect-battery - Check for battery. (none)
# flash - Perform a hook flash. (none)
# listen - Listen for DTMF path confirmation tones. (wait (MS), #expected digits)
# off-hook - Answer an inbound call. (none)
# on-hook - Go on hook. (none)
# seize - Go off hook and detect dialtone. (none)
# send-digits - Send DTMF digits. (phone #)
# send-tones - Send path confirmation tones. (digits to send)
# supervision - Set the supervision of an FXO port. (loop-start, ground-start)
######################################################################################################
# import library modules
import sys
import re
import telnetlib
import time
from sys import exit
from polycom_library import isRinging
username, password, enable = 'adtran\n', 'adtran\n', 'adtran\n'
#just because I hate strings in code
db=" detect-battery\n"
flash=" flash 10\n"
listen6=" listen 8000 6"
off=" off-hook\n"
on=" on-hook\n"
seize=" seize\n"
dial=" send-digits "
send=" send-tones 123456\n"
sls=" supervision loop-start\n"
#takes an IP address (string) as argument, returns Telnet object
def telnet(address):
try:
con = telnetlib.Telnet(address,23,10)
return con
except:
print "Error connecting to %s:" %(address)
print sys.exc_info()[0]
return -1
#takes Telnet object as argument, uses global user/pass/enable, returns prompt
def login(con):
global username
global password
global enable
temp=con.expect(['Username:'], 2)
if temp[0] == -1:
print temp
exit()
con.write(username)
temp=con.expect(['Password:'], 2)
if temp[0] == -1:
print temp
exit()
con.write(password)
con.expect(['>'], 2)
if temp[0] == -1:
print temp
exit()
con.write("en\n")
con.expect(['Password:'], 2)
if temp[0] == -1:
print temp
exit()
con.write(enable)
con.expect(['#'], 2)
if temp[0] == -1:
print temp
exit()
con.write('\n')
prompt=con.read_until('#')
con.write("terminal length 0\n")
if temp[0] == -1:
print temp
exit()
con.expect([prompt])
return prompt
def initialize(con, port):
"""
Takes connection and FXO port number (string)
as arguments and sets the given port into Idle state
"""
baseCommand="script-manager fxo %s " % (port,)
# results in Offline state
cmd=baseCommand + on
con.write(cmd)
con.expect([''], 2)
# results in Idle state
cmd=baseCommand + db
con.write(cmd)
con.expect(['to Idle'], 2)
def callStart(con, portA, portB, number):
"""
Initiates call between A-B
"""
#puts ports into Idle state
initialize(con, portA)
initialize(con, portB)
baseA="script-manager fxo %s " % (portA,)
baseB="script-manager fxo %s " % (portB,)
# does not change state of portA
cmd=baseA + sls
con.write(cmd)
con.expect(['#'], 2)
# does not change state portB
cmd=baseB + sls
con.write(cmd)
con.expect(['#'], 2)
#results in Dialtone portA
cmd=baseA + seize
con.write(cmd)
con.expect(['Dialtone'], 2)
#results in Connecting portA, portB ringing
print "calling %s" %(number,)
cmd=baseA + dial + " " + number +"\n"
con.write(cmd)
con.expect(['Connecting'], 2)
def confirmPath(con, portA, portB):
"""
!!!!Assumes any actual phones are in headset mode!!!
Confirms talk path from A->B
by setting B to listen and then sending
tones from A->B and confirming receipt
and decodability
"""
baseA="script-manager fxo %s " % (portA,)
baseB="script-manager fxo %s " % (portB,)
#results in Dialtone portB
cmd=baseB + seize
con.write(cmd)
con.expect(['#'], 2)
#results in Connected portB
cmd=baseB + flash
con.write(cmd)
con.expect(['Connected'], 2)
#results in Connected portB
cmd=baseB + listen6
print "Sending tones"
con.write(cmd)
con.expect(['#'], 2)
cmd=baseA + send
con.write(cmd)
con.expect(['123456'], 2)
result=""
def initializeSIP(con, port):
"""
Initiallizes a port connected to the
headset on a polycom phone which is
affilliated with an existing call and
leaves it in connected mode
"""
baseCommand="script-manager fxo %s " % (port,)
# results in Idle state
cmd=baseCommand + on
con.write(cmd)
con.expect(['Idle'], 2)
time.sleep(1)
#results in Dialtone state
cmd=baseCommand + seize
con.write(cmd)
con.expect(['Dialtone'], 2)
time.sleep(1)
#results in Connected state
cmd=baseCommand + flash
con.write(cmd)
con.expect(['Connected'], 2)
def listenForTones(con, port, time='18000', tones='1'):
"""
Takes a port in Connected State and
listens for $time MS for $tones tones
"""
cmd="script-manager fxo %s listen %s %s\n" % (port, time, tones)
con.write(cmd)
def test():
#portA='0/7'
#portB='0/2'
con=telnet("10.17.220.7")
prompt=login(con)
#callStart(con, portA, portB, '5551112')
#time.sleep(10)
#confirmPath(con, portA, portB)
initializeSIP(con, '0/1')
if __name__ == '__main__':
test()