aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2012-08-13 20:07:41 +0200
committerHarald Welte <laforge@gnumonks.org>2012-08-13 20:19:09 +0200
commit8b59a554887205271e7cb7d09788b8a040b8dcf2 (patch)
treee2a9793ec5ad07773a882b1d7c6467ce901465ab
parent1d5968cfcf37b99d34c11521c26d676f6e244c54 (diff)
pySim-prog: Add mode where it can re-generate a card from CSV
Rather than just having the capability of writing to CSV, it now has the capability to (re)write a card based on data from the CSV: ./pySim-prog.py -S csv --read-csv /tmp/sim.csv -i 901701234567890 or in batch mode (from the first line onwards): ./pySim-prog.py -S csv --read-csv /tmp/sim.csv --batch -j 1
-rwxr-xr-xpySim-prog.py59
1 files changed, 54 insertions, 5 deletions
diff --git a/pySim-prog.py b/pySim-prog.py
index fa342e4..ce67dbd 100755
--- a/pySim-prog.py
+++ b/pySim-prog.py
@@ -67,6 +67,12 @@ def parse_options():
default=False,
)
+ parser.add_option("-S", "--source", dest="source",
+ help="Data Source[default: %default]",
+ default="cmdline",
+ )
+
+ # if mode is "cmdline"
parser.add_option("-n", "--name", dest="name",
help="Operator name [default: %default]",
default="Magic",
@@ -121,6 +127,11 @@ def parse_options():
help="Optional batch state file",
)
+ # if mode is "csv"
+ parser.add_option("--read-csv", dest="read_csv", metavar="FILE",
+ help="Read parameters from CSV file rather than command line")
+
+
parser.add_option("--write-csv", dest="write_csv", metavar="FILE",
help="Append generated parameters in CSV file",
)
@@ -135,6 +146,20 @@ def parse_options():
print kls.name
sys.exit(0)
+ if options.source == 'csv':
+ if (options.imsi is None) and (options.batch_mode is False):
+ parser.error("CSV mode needs either an IMSI or batch mode")
+ if options.read_csv is None:
+ parser.error("CSV mode requires a CSV input file")
+ elif options.source == 'cmdline':
+ if ((options.imsi is None) or (options.iccid is None)) and (options.num is None):
+ parser.error("If either IMSI or ICCID isn't specified, num is required")
+ else:
+ parser.error("Only `cmdline' and `csv' sources supported")
+
+ if (options.read_csv is not None) and (options.source != 'csv'):
+ parser.error("You cannot specify a CSV input file in source != csv")
+
if (options.batch_mode) and (options.num is None):
options.num = 0
@@ -142,9 +167,6 @@ def parse_options():
if (options.imsi is not None) or (options.iccid is not None):
parser.error("Can't give ICCID/IMSI for batch mode, need to use automatic parameters ! see --num and --secret for more informations")
- if ((options.imsi is None) or (options.iccid is None)) and (options.num is None):
- parser.error("If either IMSI or ICCID isn't specified, num is required")
-
if args:
parser.error("Extraneous arguments")
@@ -372,6 +394,27 @@ def write_params_csv(opts, params):
cw.writerow([params[x] for x in row])
f.close()
+def read_params_csv(opts, imsi):
+ import csv
+ row = ['name', 'iccid', 'mcc', 'mnc', 'imsi', 'smsp', 'ki', 'opc']
+ f = open(opts.read_csv, 'r')
+ cr = csv.DictReader(f, row)
+ i = 0
+ for row in cr:
+ if opts.num is not None:
+ i += 1
+ if opts.num == i:
+ break
+ if row['imsi'] == imsi:
+ break
+
+ if row:
+ row['mcc'] = int(row['mcc'])
+ row['mnc'] = int(row['mnc'])
+
+ f.close()
+ return row
+
def write_params_hlr(opts, params):
# SQLite3 OpenBSC HLR
if opts.write_hlr:
@@ -405,7 +448,7 @@ def write_params_hlr(opts, params):
def write_parameters(opts, params):
write_params_csv(opts, params)
- write_params_hldr(opts, params)
+ write_params_hlr(opts, params)
BATCH_STATE = [ 'name', 'country', 'mcc', 'mnc', 'smsp', 'secret', 'num' ]
@@ -527,7 +570,13 @@ if __name__ == '__main__':
card.reset()
# Generate parameters
- cp = gen_parameters(opts)
+ if opts.source == 'cmdline':
+ cp = gen_parameters(opts)
+ elif opts.source == 'csv':
+ cp = read_params_csv(opts, opts.imsi)
+ if cp is None:
+ print "Error reading parameters\n"
+ sys.exit(2)
print_parameters(cp)
# Program the card