Compare commits

..

No commits in common. "main" and "master" have entirely different histories.
main ... master

4 changed files with 82 additions and 78 deletions

2
.gitignore vendored
View File

@ -1,2 +0,0 @@
/target
SIM_config.csv

View File

@ -1,6 +1,6 @@
[package] [package]
name = "sim_card_batcher" name = "sim_card_batcher"
version = "0.3.0" version = "0.2.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,11 +1,13 @@
use clap::Parser;
use colored::*;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::process::{exit, Command}; use std::process::{Command, exit};
use clap::Parser;
use colored::*;
mod parser; mod parser;
// setup cli parser // setup cli parser
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command( #[command(
@ -19,7 +21,7 @@ pub struct CliArgs {
/// The path to the csv file to read /// The path to the csv file to read
#[arg(short = 'c', long = "csv")] #[arg(short = 'c', long = "csv")]
csv: String, csv: String,
/// Set the output file with the commands for the UDM/Sql on the core /// Set the output file with the commands for the HSS on the core
#[arg(short = 'o', long = "output", default_value_t = String::from("output"))] #[arg(short = 'o', long = "output", default_value_t = String::from("output"))]
output_file: String, output_file: String,
/// The name of the nework /// The name of the nework
@ -42,9 +44,9 @@ pub struct CliArgs {
use_op: bool, use_op: bool,
#[arg(long = "op", default_value_t = String::from("00000000000000000000000000000000"))] #[arg(long = "op", default_value_t = String::from("00000000000000000000000000000000"))]
op: String, op: String,
/// Set the ACC /// Set true if IMS is used
#[arg(long = "acc", default_value_t = String::from("0020"))] #[arg(long = "ims", default_value_t = false)]
acc: String, ims: bool,
/// Set true to only do a dry run (print commands and not execute them) /// Set true to only do a dry run (print commands and not execute them)
#[arg(long = "dry-run", default_value_t = false)] #[arg(long = "dry-run", default_value_t = false)]
dry_run: bool, dry_run: bool,
@ -70,19 +72,12 @@ fn main() {
print!(" {}", pysim_arg); print!(" {}", pysim_arg);
} }
if !wait_for_user(&format!( if !wait_for_user(&format!("Please insert the sim-card with IMSI ending in: {}", imsi_ending.green())) {
"Please insert the sim-card with IMSI ending in: {}",
imsi_ending.green()
)) {
exit_programm(1); exit_programm(1);
} }
if args.dry_run { if args.dry_run {
println!( println!("{}: The command is not executed since {} is enabled", "WARNING".yellow(), "dry_run".yellow());
"{}: The command is not executed since {} is enabled",
"WARNING".yellow(),
"dry_run".yellow()
);
} else { } else {
let mut command = Command::new(&args.bin); let mut command = Command::new(&args.bin);
for pysim_arg in pysim_args { for pysim_arg in pysim_args {
@ -101,21 +96,19 @@ fn main() {
} }
} }
// add command to be run on the open5gcore // add command to be run on the open5gcore
/* /*
* S = imsi * S = imsi
* k = Ki * k = Ki
* a = 8000 amf * a = 8000 amf
* o = opc/op * o = opc/op
* s = sqn (= 000000000017) * s = sqn (= 0)
* t = auth_type (= 0) * t = auth_type (= 0)
* c = op is opc * c = op is opc
* u = usim_type (= 0) * u = usim_type (= 0)
* n = Domain Name (= mnc030.mcc999.3gppnetwork.org)
* T = MSISDN/Phone Number (= 49{imsi_ending})
*/ */
let user_provision_command = format!( let mut user_provision_command = format!("./provision_user.sh -S {} -k {} -a {} -o {} -s {} -t {} -c {} -u {}",
"./provision_user.sh -S {} -k {} -a {} -o {} -s {} -t {} -c {} -u {} -n {} -T 49{}",
String::from(&conf.IMSI), String::from(&conf.IMSI),
args.ki, args.ki,
"8000", "8000",
@ -126,7 +119,7 @@ fn main() {
&args.opc &args.opc
} }
}, },
"000000000017", "0",
"0", "0",
{ {
if args.use_op { if args.use_op {
@ -135,17 +128,13 @@ fn main() {
"1" "1"
} }
}, },
"0", "0"
{
if args.mnc.len() == 3 {
format!("mnc{}.mcc{}.3gppnetwork.org", args.mnc, args.mcc)
} else {
format!("mnc0{}.mcc{}.3gppnetwork.org", args.mnc, args.mcc)
}
},
imsi_ending
); );
if args.ims {
user_provision_command.push_str(&format!(" -n mnc{}.mcc{}.3gppnetwork.org -T 49{}", args.mnc, args.mcc, imsi_ending));
}
user_provision_commands.push(user_provision_command); user_provision_commands.push(user_provision_command);
} }
@ -168,7 +157,9 @@ fn wait_for_user(msg: &str) -> bool {
println!("\n{}\nPress enter to continue or 'a' to abort:", msg); println!("\n{}\nPress enter to continue or 'a' to abort:", msg);
let mut next_string = String::new(); let mut next_string = String::new();
match std::io::stdin().read_line(&mut next_string) { match std::io::stdin().read_line(&mut next_string) {
Ok(n) if n > 0 => !next_string.starts_with('a'), Ok(n) if n > 0 => {
!next_string.starts_with('a')
}
Ok(_) => true, // other input is ignored Ok(_) => true, // other input is ignored
Err(err) => panic!("{}", err), Err(err) => panic!("{}", err),
} }

View File

@ -1,6 +1,6 @@
use std::error::Error; use std::error::Error;
use std::fs::File;
use std::io::{self, BufRead}; use std::io::{self, BufRead};
use std::fs::File;
use std::path::Path; use std::path::Path;
use csv::StringRecord; use csv::StringRecord;
@ -8,6 +8,7 @@ use serde::Deserialize;
use crate::CliArgs; use crate::CliArgs;
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
// disable compiler warnings as this is just a struct to save data as it is in csv data // disable compiler warnings as this is just a struct to save data as it is in csv data
#[allow(dead_code, non_snake_case)] #[allow(dead_code, non_snake_case)]
@ -38,8 +39,7 @@ pub fn get_imsi_ending(card_config: &mut CardValues) -> String {
} }
pub fn update_imsi(card_config: &mut CardValues, cli_args: &CliArgs) { pub fn update_imsi(card_config: &mut CardValues, cli_args: &CliArgs) {
card_config.IMSI = format!( card_config.IMSI = format!("{}{}{}",
"{}{}{}",
cli_args.mcc, cli_args.mcc,
cli_args.mnc, cli_args.mnc,
&card_config.IMSI[(cli_args.mnc.len() + cli_args.mcc.len())..] &card_config.IMSI[(cli_args.mnc.len() + cli_args.mcc.len())..]
@ -56,14 +56,33 @@ pub fn generate_pysim_args(card_config: &CardValues, cli_args: &CliArgs) -> Vec<
let iccid = format!("--iccid={}", card_config.ICCID); let iccid = format!("--iccid={}", card_config.ICCID);
let pin_adm = format!("--pin-adm={}", card_config.ADM1); let pin_adm = format!("--pin-adm={}", card_config.ADM1);
let ki = format!("--ki={}", cli_args.ki); let ki = format!("--ki={}", cli_args.ki);
let acc = format!("--acc={}", cli_args.acc);
if cli_args.use_op { if cli_args.use_op {
let op = format!("--op={}", cli_args.op); let op = format!("--op={}", cli_args.op);
vec![p, imsi, name, mnc, mcc, iccid, pin_adm, ki, acc, op] vec![
p,
imsi,
name,
mnc,
mcc,
iccid,
pin_adm,
ki,
op
]
} else { } else {
let opc = format!("--opc={}", cli_args.opc); let opc = format!("--opc={}", cli_args.opc);
vec![p, imsi, name, mnc, mcc, iccid, pin_adm, ki, acc, opc] vec![
p,
imsi,
name,
mnc,
mcc,
iccid,
pin_adm,
ki,
opc
]
} }
} }
@ -75,8 +94,7 @@ pub fn parse_csv(path: &str) -> Result<Vec<CardValues>, Box<dyn Error>> {
let first_line: Vec<&str> = first_line.split(',').collect(); let first_line: Vec<&str> = first_line.split(',').collect();
let header = StringRecord::from(first_line); let header = StringRecord::from(first_line);
// now we got the header. Continue to deserialize every other line // now we got the header. Continue to deserialize every other line
Ok(lines Ok(lines.map(|line| {
.map(|line| {
// unpack line // unpack line
let line = line.unwrap(); let line = line.unwrap();
let line = line.replace(' ', ""); let line = line.replace(' ', "");
@ -84,8 +102,7 @@ pub fn parse_csv(path: &str) -> Result<Vec<CardValues>, Box<dyn Error>> {
let record = StringRecord::from(line); let record = StringRecord::from(line);
let sim_conf: CardValues = record.deserialize(Some(&header)).unwrap(); let sim_conf: CardValues = record.deserialize(Some(&header)).unwrap();
sim_conf sim_conf
}) }).collect())
.collect())
} else { } else {
panic!("could not read csv"); panic!("could not read csv");
} }
@ -94,9 +111,7 @@ pub fn parse_csv(path: &str) -> Result<Vec<CardValues>, Box<dyn Error>> {
// The output is wrapped in a Result to allow matching on errors // The output is wrapped in a Result to allow matching on errors
// Returns an Iterator to the Reader of the lines of the file. // Returns an Iterator to the Reader of the lines of the file.
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where where P: AsRef<Path>, {
P: AsRef<Path>,
{
let file = File::open(filename)?; let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines()) Ok(io::BufReader::new(file).lines())
} }