aboutsummaryrefslogtreecommitdiffstats
path: root/osmo-cn-latest/create_hlr.py
blob: b754af7aef0f9ba1ef3e909f6e6e4fde8fc83feb (plain)
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
#!/usr/bin/env python

import csv
import sys
import sqlite3
from optparse import OptionParser

def parse_options():
    parser = OptionParser()

    parser.add_option("-c", "--mcc", dest="mcc", help="Mobile Country Code")
    parser.add_option("-n", "--mnc", dest="mnc", help="Mobile Network Code")
    (options, args) = parser.parse_args()

    return options, args

def create_hlr_3g(db):
	conn = sqlite3.connect(db)
	c = conn.execute(
		"""CREATE TABLE IF NOT EXISTS subscriber (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		imsi		VARCHAR(15) UNIQUE NOT NULL,
		msisdn		VARCHAR(15) UNIQUE,
		imeisv		VARCHAR,
		vlr_number	VARCHAR(15),
		hlr_number	VARCHAR(15),
		sgsn_number	VARCHAR(15),
		sgsn_address	VARCHAR,
		ggsn_number	VARCHAR(15),
		gmlc_number	VARCHAR(15),
		smsc_number	VARCHAR(15),
		periodic_lu_tmr	INTEGER,
		periodic_rau_tau_tmr INTEGER,
		nam_cs		BOOLEAN NOT NULL DEFAULT 1,
		nam_ps		BOOLEAN NOT NULL DEFAULT 1,
		lmsi		INTEGER,
		ms_purged_cs	BOOLEAN NOT NULL DEFAULT 0,
		ms_purged_ps	BOOLEAN NOT NULL DEFAULT 0
		);"""
	)
	c.close()
	c = conn.execute(
		"""CREATE TABLE IF NOT EXISTS subscriber_apn (
		subscriber_id	INTEGER,
		apn		VARCHAR(256) NOT NULL
		);"""
	)
	c.close()
	c = conn.execute(
		"""CREATE TABLE IF NOT EXISTS subscriber_multi_msisdn (
		subscriber_id	INTEGER,
		msisdn		VARCHAR(15) NOT NULL
		);"""
	)
	c.close()
	c = conn.execute(
		"""CREATE TABLE IF NOT EXISTS auc_2g (
		subscriber_id	INTEGER PRIMARY KEY,
		algo_id_2g	INTEGER NOT NULL,
		ki		VARCHAR(32) NOT NULL
		);"""
	)
	c.close()
	c = conn.execute(
		"""CREATE TABLE IF NOT EXISTS auc_3g (
		subscriber_id	INTEGER PRIMARY KEY,
		algo_id_3g	INTEGER NOT NULL,
		k		VARCHAR(32) NOT NULL,
		op		VARCHAR(32),
		opc		VARCHAR(32),
		sqn		INTEGER NOT NULL DEFAULT 0,
		ind_bitlen	INTEGER NOT NULL DEFAULT 5
		);"""
	)
	c.close()
	c = conn.execute(
		"""CREATE UNIQUE INDEX idx_subscr_imsi ON subscriber (imsi);"""
	)
	conn.commit()
	conn.close()

def write_hlr_3g(db, data):
	conn = sqlite3.connect(db)
	c = conn.execute(
		'INSERT INTO subscriber ' +
		'(imsi, msisdn) ' +
		'VALUES ' +
		'(?,?);',
		[
			data['imsi'],
			data['extension']
		],
	)
	sub_id= c.lastrowid
	c.close()
	c = conn.execute(
		'INSERT INTO auc_2g ' +
		'(subscriber_id, algo_id_2g, ki)' +
		'VALUES ' +
		'(?,?,?);',
		[
			sub_id,
			1,
			data['ki']
		],
	)
	c.close()
	c = conn.execute(
		'INSERT INTO auc_3g ' +
		'(subscriber_id, algo_id_3g, k, opc, sqn)' +
		'VALUES ' +
		'(?, ?, ?, ?, ?);',
		[
			sub_id,
			5,
			data['ki'],
			data['opc'],
			0
		],
	)
	conn.commit()
	conn.close()

def main():
        options, args = parse_options()

        infilename = args[0]
	csvfields = ['name', 'iccid', 'mcc', 'mnc', 'imsi', 'extension', 'smsp', 'ki', 'opc', 'adm1']

	create_hlr_3g("hlr.db")
	inf = open(infilename, "r")
	outf = open("simcards.csv", "w")

	cr = csv.DictReader(inf)
	cw = csv.DictWriter(outf, csvfields)

	cw.writeheader()
	for row in cr:
		imsi = row['imsi']
                if options.mcc:
                    imsi = options.mcc + imsi[3:]
                if options.mnc:
                    imsi = imsi[0:3] + options.mnc + imsi[5:]
                    
		data = {}
		data['name'] = "Subscriber " + row['iccid'][-6:-1]
		data['iccid'] = row['iccid']
                data['imsi'] = imsi
		data['mcc'] = data['imsi'][0:3]
		data['mnc'] = data['imsi'][3:5]
		data['ki'] = row['ki']
		data['opc'] = row['opc']
		data['extension'] = row['iccid'][-6:-1]
		data['smsp'] = '00495555'
		if "adm1" in row:
			data['adm1'] = row['adm1']
		cw.writerow(data)
		write_hlr_3g("hlr.db", data)
	inf.close()
	outf.close()


if __name__ == '__main__':
    main()