Skip to content

Example Usage

Dan edited this page Nov 30, 2025 · 1 revision

OsmoHLR

Adding a Subscriber

This example shows use of the Osmocom Control Interface from a OsmoHLR instance to add a new subscriber to the database.

from pyosmoctrl import CtrlInterface, CtrlError

OSMO_HLR_CTRL_IP = '192.168.0.250'
OSMO_HLR_CTRL_PORT = 4259

ctrl = CtrlInterface(OSMO_HLR_CTRL_IP, OSMO_HLR_CTRL_PORT)

def create_subscriber(imsi: int, msisdn = None, cs = None,
               ps = None, algo = None, ki = None, op = None,
               opc = None, ind_bitlen = 5):
    
    db_id = (ctrl.set('subscriber.create', str(imsi)))['val']['value']

    subscriber_ref_var = f'subscriber.by-id-{db_id}'
    
    if msisdn: ctrl.set(f'{subscriber_ref_var}.msisdn', str(msisdn))
    if cs: ctrl.set(f'{subscriber_ref_var}.cs-enabled', str(int(cs)))
    if ps: ctrl.set(f'{subscriber_ref_var}.ps-enabled', str(int(ps)))
    
    if ki and algo:
        auth_update_var = f'{subscriber_ref_var}.aud'
        auth_val = f'{algo},{ki}'
        
        if (op or opc) and ind_bitlen:
            auth_update_var += '3g'
            if op:
                auth_val += f',OP,{op},{ind_bitlen}'
            elif opc:
                auth_val += f',OPC,{opc},{ind_bitlen}'
        else: 
            auth_update_var += '2g'

        ctrl.set(auth_update_var, auth_val)

    return ctrl.get(f'{subscriber_ref_var}.info')

if __name__ == "__main__":
    try:
        new_subscriber = create_subscriber(imsi=310170845466094, msisdn=123, cs=True,
                                           ps=True, algo='MILENAGE', ki='780E6AC95A2E43449C15BDCDD0450982',
                                           opc='2274B84B8043105A28AABBE53EF1D014')
        print(f'Added SIM Successfully! [Database ID: {new_subscriber['val']['id']}]\n')
        print(f'OsmoHLR: {new_subscriber['val']}')
    except CtrlError as err:
        print(f'Failed to add SIM!\n')
        print(f'OsmoHLR: {err.args[-1]}')
        raise err

Clone this wiki locally