-
Notifications
You must be signed in to change notification settings - Fork 7
/
sms_broadcast.py
66 lines (51 loc) · 1.9 KB
/
sms_broadcast.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
#!/usr/bin/env python
import telnetlib
import sqlite3
import sys
imsi = 999999999999999
HLR_DATABASE = "hlr.sqlite3"
def check_extension(extension):
conn.write(b"show subscriber extension %s\n" % extension)
res = conn.read_until(b"OpenBSC> ")
if b"No subscriber found for extension" in res:
create_subscriber(extension)
def create_subscriber(extension):
print("No user with excension %s found. Creating new..." % extension)
print("Extension: %s, IMSI: %d" % (extension, imsi))
conn.write(b"show subscriber imsi %d\n" % imsi)
res = conn.read_until(b"OpenBSC> ")
if b"No subscriber found for imsi" in res:
conn.write(b"subscriber create imsi %d\n" % imsi)
conn.read_until(b"OpenBSC> ")
conn.write(b"enable\n")
conn.read_until(b"OpenBSC# ")
conn.write(b"subscriber imsi %d extension %s\n" % (imsi, extension))
conn.read_until(b"OpenBSC# ")
conn.write(b"disable\n")
conn.read_until(b"OpenBSC> ")
def get_users():
# returns user id list generator
db = sqlite3.connect(HLR_DATABASE)
c = db.cursor()
c.execute("SELECT * FROM Subscriber")
for subscriber in c.fetchall():
yield subscriber[0]
def send_sms(id, extension, message):
conn.write(b"subscriber id %d sms sender extension %s send %s\n" % (id, extension, message))
res = conn.read_until(b"OpenBSC> ")
if b"%" in res:
print(res)
exit(1)
if __name__ == "__main__":
try:
extension = sys.argv[1]
message = " ".join(sys.argv[2:])
except:
print("usage: ./sms_broadcast.py extension message")
print("This script sends a message from the specified extension (number) to all devices connected to this base station")
exit(1)
conn = telnetlib.Telnet("127.0.0.1", 4242)
conn.read_until(b"OpenBSC> ")
# check_extension(extension)
for id in get_users():
send_sms(id, extension, message)