def crearUsuario(usuario, psw, tipo): #client = TikapySslClient('186.83.194.106', 8729) routeros = login('admin', 'Unifiwifi2k17', '186.83.194.106') #client.login('admin', 'Unifiwifi2k17') #pprint(client.talk(['/routing/ospf/neighbor/getall'])) sentence = '/ip/hotspot/user/add=profile=default=limit-uptime=8640=comment=hola=name=' + usuario + '=password=' + psw routeros(sentence) routeros.close() return True
def get_info_from_router(): password = input('Enter your pass here: ') list_of_data = [] try: router_cli = login('admin', password, '192.168.200.1') data = router_cli('/ip/arp/print') for arp in data: list_of_data.append(arp) router_cli.close() return list_of_data except ValueError: raise utils.FatalError
def entry(): while 1: hosts_info = configparser.ConfigParser() hosts_info.read('hosts_info.ini') for nodename in hosts_info.sections(): ip = hosts_info[nodename]['ip'] dc = hosts_info[nodename]['dc'] device = nodename username = hosts_info[nodename]['username'] password = hosts_info[nodename]['password'] try: for interface in eval(hosts_info[nodename]['interfaces']): interface_name = interface routeros = login(username, password, ip) result = routeros.query('/ip/arp/print').equal(interface=interface_name) event = { # 'dt': (datetime.datetime.now() + datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M'), 'interface': interface_name, 'status': '' } is_success = ip + interface_name + "is_success" # is_success_result = 1 #接口状态ok is_noticed = ip + interface_name + "is_noticed" # is_noticed_result = 0 #未发送短信通知 message = "数据中心:%s\n设备:%s\n线路:%s\n状态:" \ % (dc,nodename, event['interface']) if (len(result) == 0): #超出arp老化时间,条目不存在 event['status'] = "abnormal" message = message + event['status'] w_redis(is_success,'n') if (r_redis(is_noticed) == None or r_redis(is_noticed).decode('utf-8') == 'n'): w_redis(is_noticed,'y') send_message(message) else: #在arp老化时间内,判断mac-address是否还在 is_exist = result[0].__contains__('mac-address') if(is_exist == True and r_redis(is_success) != None and r_redis(is_success).decode('utf-8') == 'n'): event['status'] = "ok" message = message + event['status'] w_redis(is_success,'y') w_redis(is_noticed,'n') send_message(message) elif(is_exist == False): w_redis(is_success, 'n') event['status'] = "abnormal" message = message + event['status'] if (r_redis(is_noticed) == None or r_redis(is_noticed).decode('utf-8') == 'n'): w_redis(is_noticed, 'y') send_message(message) routeros.close() except Exception as e: print("%s.连接失败."%(nodename)) time.sleep(30)
async def get_route(client: Client, args, kwargs) -> KanTeXDocument: config = Config() try: router = routeros.login(config.mikrotik_user, config.mikrotik_passwd, args[0]) res = router('/ip/route/print') sec = Section('Routen') for i in res: sec.append(KeyValueItem(i['dst-address'], Code(i['gateway-status'].replace('reachable', '')))) return KanTeXDocument(sec) except: pass
async def get_addr(client: Client, args, kwargs) -> KanTeXDocument: config = Config() try: router = routeros.login(config.mikrotik_user, config.mikrotik_passwd, args[0]) res = router('/ip/address/print') sec = Section('Addressen') for i in res: sec.append(KeyValueItem(i['interface'], Code(i['address']))) return KanTeXDocument(sec) except: pass
async def firewall(client: Client, args, kwargs) -> KanTeXDocument: config = Config() # noinspection PyCallByClass try: router = routeros.login(config.mikrotik_user, config.mikrotik_passwd, args[0]) res = router('/ip/firewall/filter/add', **{'action': args[1], 'protocol': args[2], 'chain': 'forward', 'src-address': args[3]}) sec = Section('OK') sec.append(KeyValueItem('Returncode', Code(res))) return KanTeXDocument(sec) except: pass