diff options
author | Sylvain Munaut <tnt@246tNt.com> | 2010-12-08 22:35:04 +0100 |
---|---|---|
committer | Sylvain Munaut <tnt@246tNt.com> | 2010-12-08 22:35:04 +0100 |
commit | 143e99d1c6db78bd6ace798ad8fb240b804fd7de (patch) | |
tree | 9b1ccdf08bf09d93e6bce1b12fc0e672953e0e54 | |
parent | 1771603806ec97d0284348f349a9427a53c1488f (diff) |
pySim-prog: Add options to save generated parameters to CVS or OpenBSC HLR
Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
-rwxr-xr-x | pySim-prog.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/pySim-prog.py b/pySim-prog.py index b570c9a..d7792a5 100755 --- a/pySim-prog.py +++ b/pySim-prog.py @@ -32,6 +32,7 @@ import sys from pySim.commands import SimCardCommands from pySim.cards import _cards_classes +from pySim.utils import h2b def parse_options(): @@ -96,6 +97,13 @@ def parse_options(): help="Card # used for ICCID/IMSI autogen", ) + parser.add_option("--write-csv", dest="write_csv", metavar="FILE", + help="Append generated parameters in CSV file", + ) + parser.add_option("--write-hlr", dest="write_hlr", metavar="FILE", + help="Append generated parameters to OpenBSC HLR sqlite3", + ) + (options, args) = parser.parse_args() if options.type == 'list': @@ -235,12 +243,55 @@ def print_parameters(params): """ % params +def write_parameters(opts, params): + # CSV + if opts.write_csv: + import csv + row = ['name', 'iccid', 'mcc', 'mnc', 'imsi', 'smsp', 'ki'] + f = open(opts.write_csv, 'a') + cw = csv.writer(f) + cw.writerow([params[x] for x in row]) + f.close() + + # SQLite3 OpenBSC HLR + if opts.write_hlr: + import sqlite3 + conn = sqlite3.connect(opts.write_hlr) + + c = conn.execute( + 'INSERT INTO Subscriber ' + + '(imsi, name, extension, authorized, created, updated) ' + + 'VALUES ' + + '(?,?,?,1,datetime(\'now\'),datetime(\'now\'));', + [ + params['imsi'], + params['name'], + '9' + params['iccid'][-5:] + ], + ) + sub_id = c.lastrowid + c.close() + + c = conn.execute( + 'INSERT INTO AuthKeys ' + + '(subscriber_id, algorithm_id, a3a8_ki)' + + 'VALUES ' + + '(?,?,?)', + [ sub_id, 2, sqlite3.Binary(h2b(params['ki'])) ], + ) + + conn.commit() + conn.close() + + + if __name__ == '__main__': # Get/Gen the parameters opts = parse_options() cp = gen_parameters(opts) print_parameters(cp) + write_parameters(opts, cp) # Connect to the card if opts.pcsc_dev is None: |