def delete(self, msisdn): subscriber_number = msisdn[-5:] appstring = 'OpenBSC' appport = 4242 try: vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'enable' vty.command(cmd) cmd = 'subscriber extension %s extension %s' % (msisdn, subscriber_number) vty.command(cmd) except: pass # PG_HLR delete subscriber try: cur = db_conn.cursor() cur.execute('DELETE FROM subscribers WHERE msisdn=%(msisdn)s', {'msisdn': msisdn}) cur.execute('DELETE FROM hlr WHERE msisdn=%(msisdn)s', {'msisdn': msisdn}) if cur.rowcount > 0: db_conn.commit() cur.close() except psycopg2.DatabaseError as e: cur.close() pass self._delete_in_distributed_hlr(msisdn)
def send_immediate(self, num, text): appstring = 'OpenBSC' appport = 4242 vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'subscriber extension %s sms sender extension %s send %s' % ( num, config['smsc'], text) vty.command(cmd)
def print_vty_hlr_info(self, msisdn): appstring = 'OpenBSC' appport = 4242 vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'enable' vty.command(cmd) cmd = 'show subscriber extension %s' % msisdn return vty.command(cmd)
def purge(self, msisdn): # delete subscriber on the HLR sqlite DB appstring = 'OpenBSC' appport = 4242 vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'enable' vty.command(cmd) cmd = 'subscriber extension %s delete' % msisdn vty.command(cmd)
def authorized(self, msisdn, auth): # auth 0 subscriber disabled # auth 1 subscriber enabled # disable/enable subscriber in Osmo try: appstring = 'OpenBSC' appport = 4242 vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'enable' vty.command(cmd) cmd = 'subscriber extension %s authorized %s' % (msisdn, auth) vty.command(cmd) except: print "VTY Exception" pass # disable/enable subscriber on PG Subscribers try: cur = db_conn.cursor() cur.execute( 'UPDATE subscribers SET authorized=%(auth)s WHERE msisdn=%(msisdn)s', { 'auth': auth, 'msisdn': msisdn }) if cur.rowcount > 0: db_conn.commit() else: db_conn.rollback() raise SubscriberException('PG_HLR Subscriber not found') except psycopg2.DatabaseError as e: db_conn.rollback() raise SubscriberException('PG_HLR error changing auth status: %s' % e) try: now = int(time.time()) imsi = self._get_imsi(msisdn) rk_hlr = riak_client.bucket('hlr') subscriber = rk_hlr.get(imsi, timeout=RIAK_TIMEOUT) if subscriber.exists: subscriber.data['authorized'] = auth subscriber.data['updated'] = now subscriber.indexes = set([('modified_int', now), ('msisdn_bin', subscriber.data['msisdn'])]) subscriber.store() else: # There's no riak entry for this subscriber, add it. self._provision_in_distributed_hlr(imsi, msisdn) except riak.RiakError as e: raise SubscriberException('RK_HLR error: %s' % e) except socket.error: raise SubscriberException('RK_HLR error: unable to connect')
def expire_lu(self, msisdn): appstring = 'OpenBSC' appport = 4242 try: vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'enable' vty.command(cmd) cmd = 'subscriber extension %s expire' % (msisdn) ret = vty.command(cmd) api_log.debug('VTY: %s' % ret) if ret: raise SubscriberException('VTY: %s' % ret) except IOError as e: api_log.debug('Exception in expire_lu! %s' % e) pass
def _authorize_subscriber_in_local_hlr(self, msisdn, new_msisdn, name): try: appstring = 'OpenBSC' appport = 4242 vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'enable' vty.command(cmd) cmd = 'subscriber extension %s extension %s' % (msisdn, new_msisdn) vty.command(cmd) cmd = 'subscriber extension %s authorized 1' % new_msisdn vty.command(cmd) cmd = 'subscriber extension %s name %s' % (new_msisdn, unidecode(name)) vty.command(cmd) except: raise SubscriberException('SQ_HLR error provisioning the subscriber')
def osmo_ext2imsi(ext): try: vty = obscvty.VTYInteract('OpenBSC', '127.0.0.1', 4242) cmd = 'show subscriber extension %s' % (ext) t = vty.command(cmd) m=re.compile('IMSI: ([0-9]*)').search(t) if m: return m.group(1) else: return False except socket.error as err: print sys.exc_info()[1][1] print "Osmo VTY refused connection. Aborting.\n" sys.exit(1) except Exception as e: print sys.exc_info()[1] return False
def edit(self, msisdn, name, balance, location, equipment, roaming): params = locals() updating = [k for k, v in params.items() if v != ""] updating.remove('self') updating.remove('msisdn') # edit subscriber data in the Osmo try: appstring = 'OpenBSC' appport = 4242 vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'enable' vty.command(cmd) cmd = 'subscriber extension %s name %s' % (msisdn, name) vty.command(cmd) except Exception as e: raise SubscriberException( 'VTY error updating subscriber data: %s' % e.args[0]) # PG_HLR update subscriber data try: _set = {} for i in updating: _set[i] = params[i] cur = db_conn.cursor() sql_template = "UPDATE subscribers SET ({}) = %s WHERE msisdn = '{}'" sql = sql_template.format(', '.join(_set.keys()), msisdn) params = (tuple(_set.values()), ) cur.execute(sql, params) if cur.rowcount > 0: db_conn.commit() else: db_conn.commit() raise SubscriberException('PG_HLR No subscriber found') except psycopg2.DatabaseError, e: cur.execute("rollback") raise SubscriberException( 'PG_HLR error updating subscriber data: %s' % e)
#!/usr/bin/env python import obscvty if __name__ == '__main__': import argparse import os import sys import re parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", help="verbose mode") args = parser.parse_args() verbose_level = 1 if args.verbose: verbose_level = 2 appstring = "OsmoBTS" appport = 4241 vty = obscvty.VTYInteract(appstring, "127.0.0.1", appport) vty.command("enable") vty.command("bts 0 trx 0 ts 2 lchan 0 activate") vty.command("bts 0 trx 0 ts 2 lchan 0 loopback")
except config.NumberingException as ne: print str(ne) return print "That is from %s last seen %s" % (bts['home_bts'], bts['current_bts']) def smpp_bind(): client = smpplib.client.Client("127.0.0.1", 2775) client.set_message_received_handler(rx_alert_notification) client.connect() client.bind_transceiver(system_id="NOTIFY", password="******") client.listen() if __name__ == "__main__": re = config.re sys = config.sys riak_client = config.riak_client myprefix = config.config['internal_prefix'] myip = config.config['local_ip'] log = config.roaming_log sub = config.Subscriber() num = config.Numbering() #open a VTY console, don't bring up and down all the time. vty = obscvty.VTYInteract('OpenBSC', '127.0.0.1', 4242) log.info('Starting up alert notification listener for %s on %s' % (myprefix, myip)) smpp_bind()
def edit(self, msisdn, name, balance, location): # edit subscriber data in the Osmo try: appstring = 'OpenBSC' appport = 4242 vty = obscvty.VTYInteract(appstring, '127.0.0.1', appport) cmd = 'enable' vty.command(cmd) cmd = 'subscriber extension %s name %s' % (msisdn, name) vty.command(cmd) except e: raise SubscriberException( 'VTY error updating subscriber data: %s' % e.args[0]) # PG_HLR update subscriber data try: cur = db_conn.cursor() if balance != "": if location != "": cur.execute( 'UPDATE subscribers SET msisdn=%(msisdn)s,name=%(name)s,balance=%(balance)s,location=%(location)s WHERE msisdn=%(msisdn2)s', { 'msisdn': msisdn, 'name': name, 'balance': Decimal(str(balance)), 'msisdn2': msisdn, 'location': location }) else: cur.execute( 'UPDATE subscribers SET msisdn=%(msisdn)s,name=%(name)s,balance=%(balance)s WHERE msisdn=%(msisdn2)s', { 'msisdn': msisdn, 'name': name, 'balance': Decimal(str(balance)), 'msisdn2': msisdn }) else: if location != "": cur.execute( 'UPDATE subscribers SET msisdn=%(msisdn)s,name=%(name)s,location=%(location)s WHERE msisdn=%(msisdn2)s', { 'msisdn': msisdn, 'name': name, 'msisdn2': msisdn, 'location': location }) else: cur.execute( 'UPDATE subscribers SET msisdn=%(msisdn)s,name=%(name)s WHERE msisdn=%(msisdn2)s', { 'msisdn': msisdn, 'name': name, 'msisdn2': msisdn }) if cur.rowcount > 0: db_conn.commit() else: raise SubscriberException('PG_HLR No subscriber found') except psycopg2.DatabaseError, e: raise SubscriberException( 'PG_HLR error updating subscriber data: %s' % e)