def f_off_client(self, login_device, password_device, ip_device, api_port, ip_user, ip_local): try: a = Core(ip_device, api_port) a.login(login_device, password_device) #While disabling user it still stay exist enabled in arp #item = self.f_get_item_by_ip_in_arp_list(a, ip_user) #if not item is None: # a.response_handler(a.talk(["/ip/arp/disable", "=.id=" + item[".id"],])) """ip local""" item = self.f_get_item_by_ip_in_address_list(a, ip_local) if not item is None: a.response_handler( a.talk([ "/ip/firewall/address-list/disable", "=.id=" + item[".id"], ])) """ip vpn""" item = self.f_get_item_by_ip_in_address_list(a, ip_user) if not item is None: a.response_handler( a.talk([ "/ip/firewall/address-list/disable", "=.id=" + item[".id"], ])) #send_email(login_client, speed_down, "?", off_backup_date, "add user" ) return "worked" except: return "no"
def f_check_client(self, login_device, password_device, ip_device, api_port, ip_user): try: a = Core(ip_device, api_port) a.login(login_device, password_device) item = self.f_get_item_by_ip_in_address_list(a, ip_user) if not item is None: return "worked" else: return "no" except: return "no"
def getdata(self): """从rosAPI获取数据""" try: api1 = Core(self.r1Host, self.r1Port) api1.login(self.r1User, self.r1Pwd) """获取hotspot数据""" hptdata = api1.response_handler(api1.talk(["/ip/hotspot/host/print"])) """获取设备数据""" devname = api1.response_handler(api1.talk(["/sys/identity/print"])) """获取当前时间""" timenow = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time())) """定义处理数据所需的list""" keyslist = [ "mac-address", "DHCP", "address", "to-address", "uptime", "server", "bypassed", "bridge-port", "authorized", "http-proxy", "packets-out", "packets-in", "bytes-out", "bytes-in", "found-by", "idle-timeout", "idle-time", "host-dead-time", ".id", ] vlist = [] VLIST = [] except Exception as e: print("执行MySQL:%s时出错:%s" % (e)) """数据排版成需要的格式""" for i in hptdata: vlist.append(timenow) vlist.append(devname[0]["name"]) for j in keyslist: if j in i.keys(): vlist.append(i[j]) else: vlist.append("NULL") vtuple = tuple(vlist) vlist = [] VLIST.append(vtuple) return VLIST
def login(self): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print "DEBUG --> LOGIN2 "+ self.net_device.ip s.connect((self.net_device.ip, 8728)) self.tik = Core(s, DEBUG=self.debug) self.tik.login(username=self.net_device.admin, pwd=self.net_device.password) except Exception, e: raise RuntimeError("No puc connectar amb el router")
def getdata(self): '''从rosAPI获取数据''' try: # print 'connect ....' api1 = Core(self.r1Host, self.r1Port) # print 'connect ok' # print 'login ....' api1.login(self.r1User, self.r1Pwd) # print 'login ok' '''获取hotspot数据''' # print 'get hptdata ....' hptdata = (api1.response_handler(api1.talk(["/ip/hotspot/host/print"]))) # print 'get hptdata ok' '''获取设备数据''' # print 'get device name .....' devname = (api1.response_handler(api1.talk(['/sys/identity/print']))) # print 'get device name ok' # print devname '''获取当前时间''' timenow = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) '''定义处理数据所需的list''' keyslist = ['mac-address', 'DHCP', 'address', 'to-address', 'uptime', 'server', \ 'bypassed', 'bridge-port', 'authorized', 'http-proxy', 'packets-out' \ , 'packets-in', 'bytes-out', 'bytes-in', 'found-by', \ 'idle-timeout', 'idle-time', 'host-dead-time', '.id'] vlist = [] VLIST = [] except Exception as e: print ("执行MySQL:%s时出错:%s"%(e)) '''数据排版成需要的格式''' # print 'handles data....' for i in hptdata: vlist.append(timenow) vlist.append(devname[0]['name']) for j in keyslist: if j in i.keys(): vlist.append(i[j]) else: vlist.append('NULL') vtuple = tuple(vlist) vlist = [] VLIST.append(vtuple) # print 'handles data ok' return VLIST
def f_off_client(self, login_device, password_device, ip_device, api_port, ip_user, ip_local): try: a = Core(ip_device, api_port) a.login(login_device, password_device) #While disabling user it still stay exist enabled in arp #item = self.f_get_item_by_ip_in_arp_list(a, ip_user) #if not item is None: # a.response_handler(a.talk(["/ip/arp/disable", "=.id=" + item[".id"],])) """ip local""" item = self.f_get_item_by_ip_in_address_list(a, ip_local) if not item is None: a.response_handler(a.talk(["/ip/firewall/address-list/disable", "=.id=" + item[".id"],])) """ip vpn""" item = self.f_get_item_by_ip_in_address_list(a, ip_user) if not item is None: a.response_handler(a.talk(["/ip/firewall/address-list/disable", "=.id=" + item[".id"],])) #send_email(login_client, speed_down, "?", off_backup_date, "add user" ) return "worked" except: return "no"
def f_add_client(self, login_device, password_device, ip_device, mac_user, ip_local_user, ip_user, login_client, password_client, speed_up, speed_down, address_list_name, unit, off_backup_date, api_port): try: current_loc = locale.getlocale() locale.setlocale(locale.LC_ALL, 'C') off_backup_txt_date = off_backup_date.strftime('%b/%d/%Y').lower() locale.setlocale(locale.LC_ALL, current_loc) a = Core(ip_device, api_port) a.login(login_device, password_device) if int(speed_down)== 3 or int(speed_down)== 2 or int(speed_down)== 0 or int(speed_down)== 1: """turbonet style queue""" queue = "Upload Tarif"+str(speed_up)+"/Download Tarif"+str(speed_down) a.response_handler(a.talk(["/queue/simple/add", "=name=" + login_client, "=target-addresses=" + ip_local_user, "=queue=" + queue,])) else: """pythonix style queue""" speed = str(speed_up)+"M/"+str(speed_down)+"M" a.response_handler(a.talk(["/queue/simple/add", "=name=" + login_client, "=target-addresses=" + ip_local_user, "=max-limit=" + speed,])) a.response_handler(a.talk(["/ppp/secret/add", "=name=" + login_client, "=password="******"=local-address=" + ip_device, "=remote-address=" + ip_user,])) a.response_handler(a.talk(["/ip/arp/add", "=address=" + ip_local_user, "=mac-address=" + mac_user, "=interface=ether2-master-local", "=comment="+login_client, ])) a.response_handler(a.talk(["/ip/dhcp-server/lease/add", "=address=" + ip_local_user, "=mac-address=" + mac_user, "=comment="+login_client, ])) """Check if address already added""" if self.f_get_item_by_ip_in_address_list(a, ip_user) is None: """add local addr """ a.response_handler(a.talk(["/ip/firewall/address-list/add", "=address=" + ip_local_user, "=list=" + "internet", "=comment=" + off_backup_txt_date,])) """add vpn addr """ a.response_handler(a.talk(["/ip/firewall/address-list/add", "=address=" + ip_user, "=list=" + "internet", "=comment=" + off_backup_txt_date,])) #send_email(login_client, speed_down, "?", off_backup_date, "add user" ) return "worked" except: return "no"
def f_update_client(self, login_device, password_device, ip_device, api_port, name_field, old_value_field, new_value_field): try: a = Core(ip_device) a.login(login_device, password_device) if name_field == "update_ip": item =self.f_get_item_by_ip_in_address_list(a,old_value_field) if not item is None: a.response_handler(a.talk(["/ip/firewall/address-list/set", "=address=" + new_value_field, "=.id=" + item[".id"],])) item2 = self.f_get_item_by_ip_in_secrets(a, old_value_field) if not item2 is None: a.response_handler(a.talk(["/ppp/secret/set", "=remote-address=" + new_value_field, "=.id=" + item2[".id"],])) if name_field == "update_mac": item = self.f_get_item_by_mac_in_arp_list(a, old_value_field) if not item is None: a.response_handler(a.talk(["/ip/arp/set", "=mac-address=" + new_value_field, "=.id=" + item[".id"],])) item = self.f_get_item_by_mac_in_dhcplease_list(a, old_value_field) if not item is None: a.response_handler(a.talk(["/ip/dhcp-server/lease/set", "=mac-address=" + new_value_field, "=.id=" + item[".id"],])) if name_field == "update_ip_local": item =self.f_get_item_by_ip_in_address_list(a,old_value_field) if not item is None: a.response_handler(a.talk(["/ip/firewall/address-list/set", "=address=" + new_value_field, "=.id=" + item[".id"],])) item = self.f_get_item_by_ip_in_arp_list(a, old_value_field) if not item is None: a.response_handler(a.talk(["/ip/arp/set", "=address=" + new_value_field, "=.id=" + item[".id"],])) item = self.f_get_item_by_ip_in_queues(a, old_value_field) if not item is None: a.response_handler(a.talk(["/queue/simple/set", "=target-addresses=" + new_value_field, "=.id=" + item[".id"],])) item = self.f_get_item_by_ip_in_dhcplease_list(a, old_value_field) if not item is None: a.response_handler(a.talk(["/ip/dhcp-server/lease/set", "=address=" + new_value_field, "=.id=" + item[".id"],])) if name_field == "update_login": item = self.f_get_item_by_name_in_secrets(a, old_value_field) if not item is None: a.response_handler(a.talk(["/ppp/secret/set", "=name=" + new_value_field, "=.id=" + item[".id"],])) item2 = self.f_get_item_by_name_in_queues(a, old_value_field) if not item2 is None: a.response_handler(a.talk(["/queue/simple/set", "=name=" + new_value_field, "=.id=" + item2[".id"],])) if name_field == "update_password": item = self.f_get_item_by_name_in_secrets(a, old_value_field) if not item is None: a.response_handler(a.talk(["/ppp/secret/set", "=password="******"=.id=" + item[".id"],])) if name_field == "update_queue": item = self.f_get_item_by_name_in_queues(a, old_value_field) if not item is None: if int(new_value_field)== 3 or int(new_value_field)== 2 or int(new_value_field)== 0 or int(new_value_field)== 1: """turbonet style queue""" queue = "Upload Tarif"+str(new_value_field)+"/Download Tarif"+str(new_value_field) a.response_handler(a.talk(["/queue/simple/set", "=queue=" + queue, "=.id=" + item[".id"],])) else: """pythonix style queue""" a.response_handler(a.talk(["/queue/simple/set", "=max-limit=" + new_value_field, "=.id=" + item[".id"],])) info_mes = "New tarif=" + str(new_value_field) + " for user: "******"/log/info", "=message=" + info_mes, ])) return "worked" except: return "no"
def f_on_client(self, login_device, password_device, ip_device, api_port, ip_user, ip_local, off_backup_date): try: current_loc = locale.getlocale() locale.setlocale(locale.LC_ALL, 'C') off_backup_txt_date = off_backup_date.strftime('%b/%d/%Y').lower() locale.setlocale(locale.LC_ALL, current_loc) a = Core(ip_device, api_port) a.login(login_device, password_device) #Do not touch arp as it's stay unmodified if disabling user #item = self.f_get_item_by_ip_in_arp_list(a, ip_user) #if not item is None: # a.response_handler(a.talk(["/ip/arp/enable", "=.id=" + item[".id"],])) # a.response_handler(a.talk(["/ip/arp/comment", "=comment=" + off_backup_txt_date, "=.id=" + item[".id"], ])) """ip local""" item = self.f_get_item_by_ip_in_address_list(a, ip_local) if not item is None: a.response_handler(a.talk(["/ip/firewall/address-list/enable", "=.id=" + item[".id"],])) a.response_handler(a.talk(["/ip/firewall/address-list/comment", "=comment=" + off_backup_txt_date, "=.id=" + item[".id"], ])) """ip vpn""" item = self.f_get_item_by_ip_in_address_list(a, ip_user) if not item is None: a.response_handler(a.talk(["/ip/firewall/address-list/enable", "=.id=" + item[".id"],])) a.response_handler(a.talk(["/ip/firewall/address-list/comment", "=comment=" + off_backup_txt_date, "=.id=" + item[".id"], ])) #send_email(ip_user"/"ip_local, "&", "?", off_backup_date, "on user" ) return "worked" except: return "no"
def f_remove_client(self, login_device, password_device, ip_device, api_port, ip_local, ip_user): try: a = Core(ip_device, api_port) a.login(login_device, password_device) item = self.f_get_item_by_ip_in_arp_list(a, ip_local) if not item is None: a.response_handler(a.talk(["/ip/arp/remove", "=.id=" + item[".id"],])) item = self.f_get_item_by_ip_in_dhcplease_list(a, ip_local) if not item is None: a.response_handler(a.talk(["/ip/dhcp-server/lease/remove", "=.id=" + item[".id"],])) """remove local addr from firewall addresses""" item = self.f_get_item_by_ip_in_address_list(a, ip_local) if not item is None: a.response_handler(a.talk(["/ip/firewall/address-list/remove", "=.id=" + item[".id"],])) """remove vpn addr from firewall addres""" item = self.f_get_item_by_ip_in_address_list(a, ip_user) if not item is None: a.response_handler(a.talk(["/ip/firewall/address-list/remove", "=.id=" + item[".id"],])) item = self.f_get_item_by_ip_in_secrets(a, ip_user) if not item is None: a.response_handler(a.talk(["/ppp/secret/remove", "=.id=" + item[".id"],])) item = self.f_get_item_by_ip_in_queues(a, ip_local) if not item is None: a.response_handler(a.talk(["/queue/simple/remove", "=.id=" + item[".id"],])) except: return "no" return "worked"
#!/usr/bin/env python from RosAPI import Core def prettify(data): for x in data: for y in x.keys(): print "%-20s: %50s" % (y, x[y]) if __name__ == "__main__": a = Core("192.168.101.254") a.login("user1", "user") #a.run_interpreter() #prettify(a.response_handler(a.talk(["/tool/torch","=interface=ether2 - Lokal","=src-address=192.168.101.160","=dst-address=218.45.174.214"]))) prettify( a.response_handler( a.talk([ "/tool/torch", "=interface=ether2 - Lokal", "=src-address=192.168.101.160", "=dst-address=0.0.0.0/0" ]))) #prettify(a.response_handler(a.talk(["/tool/torch","=interface=ether2 - Lokal","=src-address=192.168.101.160"])))
#!/usr/bin/env python from RosAPI import Core def prettify(data): for x in data: for y in x.keys(): print "%-20s: %50s" % (y, x[y]) if __name__ == "__main__": a = Core("172.16.1.1") a.login("admin", "") #a.run_interpreter() prettify(a.response_handler(a.talk(["/ip/address/print"])))
if opts.all_routes: for i in opts.all_routes.split(","): if ":" in i: ipv6_routes.append(i) else: routes.append(i) if opts.ipv4: routes.append("0.0.0.0") if opts.ipv6: ipv6_routes.append("::0") # Instantiating the session and loggin in try: a = Core(host) except: print "Can't connect. Check your connection." sys.exit() try: a.login(user, pw) except: print "Unable to log in. Check your credentials." sys.exit() # Simple connection tester to make sure you can hit the API def check_api(): try: z = a.response_handler(a.talk(["/system/resource/print"])) return z
for msgspec in msglist : # msgspec is something like "3 3941", # msg number and size in octets msgnum = int(msgspec.split(' ')[0]) print "Deleting msg %d\r" % msgnum pop.dele(msgnum) else : print "No messages for", user else : print "Couldn't list messages: status", poplist[0] pop.quit() if connect: try: print("Connecting to Monitor server API") child = Core(hostdude) except: print('Host is unavailiable;please check /ip/services/api: %s'%host) else: user= "******" password="******" child.login(user,password) try: for message in arraysms: message=message+" SMStoday:%s"%numberofsmsout for abonent in smsreceivers: # child.response_handler(child.talk(["/ip/service/set", "=disabled="+"yes", "=numbers="+"www"])) print ("Sending message:%s to abonent:%s"%(message,abonent)) numberofsmsout+=1 child.response_handler(child.talk(["/tool/sms/send","=port="+"usb3","=phone-number="+abonent, "=message="+message])) except:
def f_on_client(self, login_device, password_device, ip_device, api_port, ip_user, ip_local, off_backup_date): try: current_loc = locale.getlocale() locale.setlocale(locale.LC_ALL, 'C') off_backup_txt_date = off_backup_date.strftime('%b/%d/%Y').lower() locale.setlocale(locale.LC_ALL, current_loc) a = Core(ip_device, api_port) a.login(login_device, password_device) #Do not touch arp as it's stay unmodified if disabling user #item = self.f_get_item_by_ip_in_arp_list(a, ip_user) #if not item is None: # a.response_handler(a.talk(["/ip/arp/enable", "=.id=" + item[".id"],])) # a.response_handler(a.talk(["/ip/arp/comment", "=comment=" + off_backup_txt_date, "=.id=" + item[".id"], ])) """ip local""" item = self.f_get_item_by_ip_in_address_list(a, ip_local) if not item is None: a.response_handler( a.talk([ "/ip/firewall/address-list/enable", "=.id=" + item[".id"], ])) a.response_handler( a.talk([ "/ip/firewall/address-list/comment", "=comment=" + off_backup_txt_date, "=.id=" + item[".id"], ])) """ip vpn""" item = self.f_get_item_by_ip_in_address_list(a, ip_user) if not item is None: a.response_handler( a.talk([ "/ip/firewall/address-list/enable", "=.id=" + item[".id"], ])) a.response_handler( a.talk([ "/ip/firewall/address-list/comment", "=comment=" + off_backup_txt_date, "=.id=" + item[".id"], ])) #send_email(ip_user"/"ip_local, "&", "?", off_backup_date, "on user" ) return "worked" except: return "no"
def f_add_client(self, login_device, password_device, ip_device, mac_user, ip_local_user, ip_user, login_client, password_client, speed_up, speed_down, address_list_name, unit, off_backup_date, api_port): try: current_loc = locale.getlocale() locale.setlocale(locale.LC_ALL, 'C') off_backup_txt_date = off_backup_date.strftime('%b/%d/%Y').lower() locale.setlocale(locale.LC_ALL, current_loc) a = Core(ip_device, api_port) a.login(login_device, password_device) if int(speed_down) == 3 or int(speed_down) == 2 or int( speed_down) == 0 or int(speed_down) == 1: """turbonet style queue""" queue = "Upload Tarif" + str( speed_up) + "/Download Tarif" + str(speed_down) a.response_handler( a.talk([ "/queue/simple/add", "=name=" + login_client, "=target-addresses=" + ip_local_user, "=queue=" + queue, ])) else: """pythonix style queue""" speed = str(speed_up) + "M/" + str(speed_down) + "M" a.response_handler( a.talk([ "/queue/simple/add", "=name=" + login_client, "=target-addresses=" + ip_local_user, "=max-limit=" + speed, ])) a.response_handler( a.talk([ "/ppp/secret/add", "=name=" + login_client, "=password="******"=local-address=" + ip_device, "=remote-address=" + ip_user, ])) a.response_handler( a.talk([ "/ip/arp/add", "=address=" + ip_local_user, "=mac-address=" + mac_user, "=interface=ether2-master-local", "=comment=" + login_client, ])) a.response_handler( a.talk([ "/ip/dhcp-server/lease/add", "=address=" + ip_local_user, "=mac-address=" + mac_user, "=comment=" + login_client, ])) """Check if address already added""" if self.f_get_item_by_ip_in_address_list(a, ip_user) is None: """add local addr """ a.response_handler( a.talk([ "/ip/firewall/address-list/add", "=address=" + ip_local_user, "=list=" + "internet", "=comment=" + off_backup_txt_date, ])) """add vpn addr """ a.response_handler( a.talk([ "/ip/firewall/address-list/add", "=address=" + ip_user, "=list=" + "internet", "=comment=" + off_backup_txt_date, ])) #send_email(login_client, speed_down, "?", off_backup_date, "add user" ) return "worked" except: return "no"
def f_remove_client(self, login_device, password_device, ip_device, api_port, ip_local, ip_user): try: a = Core(ip_device, api_port) a.login(login_device, password_device) item = self.f_get_item_by_ip_in_arp_list(a, ip_local) if not item is None: a.response_handler( a.talk([ "/ip/arp/remove", "=.id=" + item[".id"], ])) item = self.f_get_item_by_ip_in_dhcplease_list(a, ip_local) if not item is None: a.response_handler( a.talk([ "/ip/dhcp-server/lease/remove", "=.id=" + item[".id"], ])) """remove local addr from firewall addresses""" item = self.f_get_item_by_ip_in_address_list(a, ip_local) if not item is None: a.response_handler( a.talk([ "/ip/firewall/address-list/remove", "=.id=" + item[".id"], ])) """remove vpn addr from firewall addres""" item = self.f_get_item_by_ip_in_address_list(a, ip_user) if not item is None: a.response_handler( a.talk([ "/ip/firewall/address-list/remove", "=.id=" + item[".id"], ])) item = self.f_get_item_by_ip_in_secrets(a, ip_user) if not item is None: a.response_handler( a.talk([ "/ppp/secret/remove", "=.id=" + item[".id"], ])) item = self.f_get_item_by_ip_in_queues(a, ip_local) if not item is None: a.response_handler( a.talk([ "/queue/simple/remove", "=.id=" + item[".id"], ])) except: return "no" return "worked"
#!/usr/bin/python from RosAPI import Core from sys import argv try: a = Core(argv[1]) except: print("No connection") a.login("bgpmonlogin", "bgpmonpass") peers = a.response_handler(a.talk(["/routing/bgp/peer/print"])) peers_dict = {} if len(peers) > 0: for peer in peers: # print("{} {} {}".format(peer['remote-address'], peer['remote-as'], peer['established'])) peers_dict[peer['remote-address']] = peer['established'] if argv[2] in peers_dict: if peers_dict[argv[2]] == 'true': print(1) else: print(0) else: print(0)
def f_update_client(self, login_device, password_device, ip_device, api_port, name_field, old_value_field, new_value_field): try: a = Core(ip_device) a.login(login_device, password_device) if name_field == "update_ip": item = self.f_get_item_by_ip_in_address_list( a, old_value_field) if not item is None: a.response_handler( a.talk([ "/ip/firewall/address-list/set", "=address=" + new_value_field, "=.id=" + item[".id"], ])) item2 = self.f_get_item_by_ip_in_secrets(a, old_value_field) if not item2 is None: a.response_handler( a.talk([ "/ppp/secret/set", "=remote-address=" + new_value_field, "=.id=" + item2[".id"], ])) if name_field == "update_mac": item = self.f_get_item_by_mac_in_arp_list(a, old_value_field) if not item is None: a.response_handler( a.talk([ "/ip/arp/set", "=mac-address=" + new_value_field, "=.id=" + item[".id"], ])) item = self.f_get_item_by_mac_in_dhcplease_list( a, old_value_field) if not item is None: a.response_handler( a.talk([ "/ip/dhcp-server/lease/set", "=mac-address=" + new_value_field, "=.id=" + item[".id"], ])) if name_field == "update_ip_local": item = self.f_get_item_by_ip_in_address_list( a, old_value_field) if not item is None: a.response_handler( a.talk([ "/ip/firewall/address-list/set", "=address=" + new_value_field, "=.id=" + item[".id"], ])) item = self.f_get_item_by_ip_in_arp_list(a, old_value_field) if not item is None: a.response_handler( a.talk([ "/ip/arp/set", "=address=" + new_value_field, "=.id=" + item[".id"], ])) item = self.f_get_item_by_ip_in_queues(a, old_value_field) if not item is None: a.response_handler( a.talk([ "/queue/simple/set", "=target-addresses=" + new_value_field, "=.id=" + item[".id"], ])) item = self.f_get_item_by_ip_in_dhcplease_list( a, old_value_field) if not item is None: a.response_handler( a.talk([ "/ip/dhcp-server/lease/set", "=address=" + new_value_field, "=.id=" + item[".id"], ])) if name_field == "update_login": item = self.f_get_item_by_name_in_secrets(a, old_value_field) if not item is None: a.response_handler( a.talk([ "/ppp/secret/set", "=name=" + new_value_field, "=.id=" + item[".id"], ])) item2 = self.f_get_item_by_name_in_queues(a, old_value_field) if not item2 is None: a.response_handler( a.talk([ "/queue/simple/set", "=name=" + new_value_field, "=.id=" + item2[".id"], ])) if name_field == "update_password": item = self.f_get_item_by_name_in_secrets(a, old_value_field) if not item is None: a.response_handler( a.talk([ "/ppp/secret/set", "=password="******"=.id=" + item[".id"], ])) if name_field == "update_queue": item = self.f_get_item_by_name_in_queues(a, old_value_field) if not item is None: if int(new_value_field) == 3 or int( new_value_field) == 2 or int( new_value_field) == 0 or int( new_value_field) == 1: """turbonet style queue""" queue = "Upload Tarif" + str( new_value_field) + "/Download Tarif" + str( new_value_field) a.response_handler( a.talk([ "/queue/simple/set", "=queue=" + queue, "=.id=" + item[".id"], ])) else: """pythonix style queue""" a.response_handler( a.talk([ "/queue/simple/set", "=max-limit=" + new_value_field, "=.id=" + item[".id"], ])) info_mes = "New tarif=" + str( new_value_field) + " for user: "******"/log/info", "=message=" + info_mes, ])) return "worked" except: return "no"
class MikrotikRouter(Exception): debug = True ## Stores the unique Singleton instance _iInstance = None #def __init__(self,networkdevice): # print "DEBUG --> POOO" # self.net_device=networkdevice ## Class used with this Python singleton design pattern # @todo Add all variables, and methods needed for the Singleton class below class Singleton: def __init__(self): ## specific Mikrotik network device self.net_device = None ## Stablished connection? self.tik = None ## The constructor # @param self The object pointer. def __init__( self ): # Check whether we already have an instance self.net_device = None """if MikrotikRouter._iInstance is None: # Create and remember instanc MikrotikRouter._iInstance = MikrotikRouter.Singleton() # Store instance reference as the only member in the handle self._EventHandler_instance = MikrotikRouter._iInstance""" def set_networkdevice(self, networkdevice_): self.net_device = networkdevice_ """if self.net_device is None: self.net_device = networkdevice_ self.tik = None else: if self.net_device.id != networkdevice_.id: self.tik = None self.net_device = networkdevice_ """ ## Delegate access to implementation. # @param self The object pointer. # @param attr Attribute wanted. # @return Attribute #def __getattr__(self, aAttr): # return getattr(self._iInstance, aAttr) ## Delegate access to implementation. # @param self The object pointer. # @param attr Attribute wanted. # @param value Vaule to be set. # @return Result of operation. #def __setattr__(self, aAttr, aValue): # return setattr(self._iInstance, aAttr, aValue) def login(self): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print "DEBUG --> LOGIN2 "+ self.net_device.ip s.connect((self.net_device.ip, 8728)) self.tik = Core(s, DEBUG=self.debug) self.tik.login(username=self.net_device.admin, pwd=self.net_device.password) except Exception, e: raise RuntimeError("No puc connectar amb el router")
#!/usr/bin/env python from RosAPI import Core def prettify(data): for x in data: print "______________________________________________________________" for y in x.keys(): print "%-20s: %50s" % (y, x[y]) if __name__ == '__main__': import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("10.34.240.66", 8728)) tik = Core(s, DEBUG=False) tik.login(username="******", pwd="-x@rx@3W") #a = Core('10.34.240.66:8728') #a.login('admin', '-x@rx@3W') #a.run_interpreter() prettify(tik.response_handler(tik.talk(['/ip/dhcp-server/lease/print'])))