def run(self): logger = utils.log_setup(self.app_name, self.enable_logging) self.engine, self.cnn, self.metadata =db.connect_to_postgres(logger, self.DB) self.start_ip = ipaddress.IPv4Address(self.start_ip) self.end_ip = ipaddress.IPv4Address(self.end_ip) self.task_id = db.insert_task_processing(logger, self.cnn, self.app_name, datetime.datetime.now(), str(self.start_ip), str(self.end_ip)) access_lists_all = [] for ip_int in range(int(self.start_ip), int(self.end_ip)): self.ip = str(ipaddress.IPv4Address(ip_int)) if(not utils.ip_is_up(self.ip)): continue api_conn = Api(self.ip, logger, user=self.username, password=self.password, use_ssl=False, port=8728) if(api_conn.successful == False): continue access_lists = api_conn.talk(["/interface/wireless/access-list/print"]) access_lists = utils.process_response(access_lists, to_add_params = {"IP" : self.ip, "task_id" : self.task_id} ) access_lists_all = access_lists_all + access_lists access_lists_df = pd.DataFrame(access_lists_all) access_lists_df.to_sql("access_list", self.engine, if_exists='append', index=True) db.update_task_completed(logger, self.cnn, self.task_id)
def run(self): logger = utils.log_setup(self.app_name, self.enable_logging) self.engine, self.cnn, self.metadata =db.connect_to_postgres(logger, self.DB) self.task_id = db.insert_task_processing(logger, self.cnn, self.app_name, datetime.datetime.now(), str(self.ip), str(self.ip)) logger.info("Processing IP {}".format(self.ip)) logger.info("Testing IP status . . .") if(not utils.ip_is_up(self.ip)): logger.info("IP {} is down".format(self.ip)) db.update_task_completed(logger, self.cnn, self.task_id, status="IP Down") return self.api_conn = api_dev.Api(self.ip, logger, user=self.username, password=self.password, use_ssl=False, port=8728 ) if(self.api_conn.successful == False): db.update_task_completed(logger, self.cnn, self.task_id, status = "Connection Failed") return res = self.api_conn.talk([ "/interface/wireless/access-list/add", "=mac-address={}".format(self.mac_address), "=comment={}".format(self.comment) ]) if(len(res)==1 and res[0][0]=="!done"): db.update_task_completed(logger, self.cnn, self.task_id) else : db.update_task_completed(logger, self.cnn, self.task_id, status = "Execution Failed") return self.task_id
def run(self): logger = utils.log_setup(self.app_name, self.enable_logging) self.engine, self.cnn, self.metadata =db.connect_to_postgres(logger, self.DB) self.create_nas_pppoe_table(logger) self.start_ip = ipaddress.IPv4Address(self.start_ip) self.end_ip = ipaddress.IPv4Address(self.end_ip) self.task_id = db.insert_task_processing(logger, self.cnn, self.app_name, datetime.datetime.now(), str(self.start_ip), str(self.end_ip)) for ip_int in range(int(self.start_ip), int(self.end_ip)): self.ip = str(ipaddress.IPv4Address(ip_int)) if(not utils.ip_is_up(self.ip)): continue api_conn = Api(self.ip, logger, user=self.username, password=self.password, use_ssl=False, port=8728) if(api_conn.successful == False): continue response = api_conn.talk(["/ppp/active/print"]) pppoe_list = utils.process_response(response, to_add_params = {"NAS_IP" : self.ip, "task_id" : self.task_id} ) for pppoe in pppoe_list : self.upsert(logger, pppoe) db.update_task_completed(logger, self.cnn, self.task_id)
def run(self): logger = utils.log_setup(self.app_name, self.enable_logging, logs_queue=self.logs_queue) self.engine, self.cnn, self.metadata = db.connect_to_postgres( logger, self.DB) self.task_id = db.insert_task_processing(logger, self.cnn, self.app_name, datetime.datetime.now(), self.start_ip_str, self.end_ip_str) up_ips = utils.get_up_ips(logger, self.start_ip_str, self.end_ip_str, num_worker_threads=200, ip_version=self.ip_version) for up_ip in up_ips: self.ip = up_ip if (not self.is_standardized(self.ip)): logger.info( "IP {} is not standardized, please standardize it first". format(self.ip)) continue if (self.connection_type == 'ssh'): self.ssh, success = utils.ssh_connect(logger, self.ip, self.username, self.password) if (not success): continue elif (self.connection_type == 'api'): self.api_conn = api_dev.Api(self.ip, logger, user=self.username, password=self.password, use_ssl=False, port=8728) if (self.api_conn.successful == False): continue for command in self.commands_list: self.execute_command(logger, command, self.ip) db.update_task_completed(logger, self.cnn, self.task_id) self.logs_queue.put(('completed', str(self.task_id))) return self.task_id
def run(self): logger = utils.log_setup(self.app_name, self.enable_logging) self.engine, self.cnn, self.metadata =db.connect_to_postgres(logger, self.DB) self.start_ip = ipaddress.IPv4Address(self.start_ip) self.end_ip = ipaddress.IPv4Address(self.end_ip) self.task_id = db.insert_task_processing(logger, self.cnn, self.app_name, datetime.datetime.now(), str(self.start_ip), str(self.end_ip)) for ip_int in range(int(self.start_ip), int(self.end_ip)): self.ip = str(ipaddress.IPv4Address(ip_int)) if(not utils.ip_is_up(self.ip)): continue ftp = FTP(host = self.ip, user = self.username, passwd = self.password, acct = 'root') ftp.connect() ftp.login(user = self.username, passwd = self.password) client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(self.ip, port = 22, username = self.username, password = self.password) file = open('packages/routeros-mipsbe-6.43.16.npk','rb') ftp.storbinary('STOR routeros-mipsbe-6.43.16.npk', file) file.close() stdin, stdout, stderr = client.exec_command('/system reboot' + '\r\n' + 'y') while(True): sleep(20) try : client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(self.ip, port = 22, username = self.username, password = self.password) stdin, stdout, stderr = client.exec_command('/system resource print') output = stdout.readlines() output=''.join(output) version = re.findall("version:.*\n", output)[0][9:-2] if(version =="6.43.16 (long-term)"): print("version confirmed {}".format(version)) break except Exception as e: print("connection failed : {}".format(e)) stdin, stdout, stderr = client.exec_command('/system routerboard upgrade' + '\r\n' + 'y') stdin, stdout, stderr = client.exec_command('/system reboot' + '\r\n' + 'y')
def run(self): from random import randrange logger = utils.log_setup(self.app_name, self.enable_logging) self.engine, self.cnn, self.metadata = db.connect_to_postgres( logger, self.DB) real_time_monitor = RealTimeMonitor(link_id=-1, server_ip='test', client_ip='test', link_name="test", set_datetime=False) real_time_monitor.cnn = self.cnn dt = datetime.datetime(2019, 1, 1) end = datetime.datetime.today() step = datetime.timedelta(days=1) real_time_monitor.create_traffic_monitor(logger) while dt < end: values = { "server_ip": "test", "client_ip": "test", "both_tx_max": randrange(100), "both_tx_avg": randrange(100), "both_tx_min": randrange(100), "both_rx_max": randrange(100), "both_rx_avg": randrange(100), "both_rx_min": randrange(100), "send_tx_max": randrange(100), "send_tx_avg": randrange(100), "send_tx_min": randrange(100), "receive_rx_max": randrange(100), "receive_rx_avg": randrange(100), "receive_rx_min": randrange(100), "datetime": dt, "status": "done", "error_details": None } real_time_monitor.insert(values) dt += step
def execute_api_command_onestep(self, command, IP, log_tag='', end_signal=None): logger = utils.log_setup(self.app_name, self.enable_logging, logs_queue=self.logs_queue) self.engine, self.cnn, self.metadata = db.connect_to_postgres( logger, self.DB) response = {} if (not self.is_standardized(IP)): response["result"] = False response[ "message"] = "IP {} is not standardized, please standardize it first".format( IP) logger.info(log_tag + json.dumps(response)) else: self.api_conn = api_dev.Api(IP, logger, user=self.username, password=self.password, use_ssl=False, port=8728) if (self.api_conn.successful == False): response["result"] = False response["message"] = "connection to device failed" logger.info(log_tag + json.dumps(response)) else: res = self.execute_command(logger, command, IP) response_data = [] for element in res: response_data.append({ "flag": element[0], "message": element[1] }) response["result"] = True response["data"] = response_data logger.info(log_tag + json.dumps(response)) if (end_signal): logger.info(end_signal) return response
def run(self, logger=None): if (not logger): logger = utils.log_setup(self.app_name, self.enable_logging, logs_queue=self.logs_queue) self.engine, self.cnn, self.metadata = db.connect_to_postgres( logger, self.DB) self.task_id = db.insert_task_processing(logger, self.cnn, self.app_name, datetime.datetime.now(), self.start_ip_str, self.end_ip_str) self.create_account_table(logger) up_ips = utils.get_up_ips(logger, self.start_ip_str, self.end_ip_str, num_worker_threads=200, ip_version=self.ip_version) for up_ip in up_ips: self.ip = up_ip logger.info("Processing IP {}".format(self.ip)) if (self.is_standardized(self.ip)): logger.info("IP {} is already standardized".format(self.ip)) continue done = False for username, password in self.accounts: self.ssh, success = utils.ssh_connect(logger, self.ip, username, password) if (not success): continue #for standard_account in [self.standard_account, self.standard_account_kubi]: for standard_account in [self.standard_account]: self.upsert( logger, { "username": standard_account["username"], "password": standard_account["password"], "IP": self.ip, "status": "Processing", "date": datetime.datetime.now(), "version": None, "mac": None, "task_id": self.task_id }) stdin, stdout, stderr = self.ssh.exec_command( '/system identity print') output = stdout.read().decode("utf-8", 'ignore') device_name = output[8:-4] backup_filename = '{0}{1}.rsc'.format( device_name, datetime.datetime.now().strftime('%d_%m_%Y-%H_%M')) stdin, stdout, stderr = self.ssh.exec_command( '/export file={}'.format(backup_filename)) output = stdout.readlines() backup_taken = True logger.info("export backup results : {}".format(output)) if (output != []): backup_taken = False logger.info( "Error: couldn't generate backup file for device {0} {1}" .format(device_name, self.ip)) if (backup_taken): stdin, stdout, stderr = self.ssh.exec_command( '/tool fetch address={address} port={port} src-path={0} user={name} mode=ftp password={password} dst-path={0} upload=yes' .format(backup_filename, **self.ftp_server)) print(stdout.read().decode("utf-8", 'ignore')) stdin, stdout, stderr = self.ssh.exec_command( '/system resource print') output = stdout.read().decode("utf-8", 'ignore') version = re.findall("version:.*\n", output)[0][9:-2] stdin, stdout, stderr = self.ssh.exec_command( '/interface ethernet print') output = stdout.read().decode("utf-8", 'ignore') self.mac = None try: self.mac = re.findall( "[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}", output)[0] except: logger.info("mac not found, skipping IP") break stdin, stdout, stderr = self.ssh.exec_command( '/user add name={username} password={password} group={group}' .format(**self.standard_account)) output = stdout.read().decode("utf-8", 'ignore') """ stdin, stdout, stderr = self.ssh.exec_command( '/user add name={username} password={password} group={group}'.format(**self.standard_account_kubi) ) output = stdout.readlines()""" stdin, stdout, stderr = self.ssh.exec_command('/user print') output = stdout.read().decode("utf-8", 'ignore') output = re.sub("\n ", "\n", output) output = re.sub(" +", " ", output) output = output.split('\n')[2:-2] users = [line.split(' ')[0:2] for line in output] logger.info("users before deletion for ip {0} : {1}".format( self.ip, output)) for user in users: #if(user[1]!=self.standard_account["username"] and user[1]!="admin" # and user[1]!=self.standard_account_kubi["username"]): if (user[1] != self.standard_account["username"] and user[1] != "admin"): stdin, stdout, stderr = self.ssh.exec_command( '/user remove {}'.format(user[1])) output = stdout.read().decode("utf-8", 'ignore') stdin, stdout, stderr = self.ssh.exec_command( '/ip service set api,ssh,winbox disabled=no') output = stdout.read().decode("utf-8", 'ignore') stdin, stdout, stderr = self.ssh.exec_command( '/ip service set api-ssl,ftp,telnet,www,www-ssl disabled=yes' ) output = stdout.read().decode("utf-8", 'ignore') stdin, stdout, stderr = self.ssh.exec_command( "system ntp client set enabled=yes primary-ntp=95.0.60.220" ) output = stdout.read().decode("utf-8", 'ignore') #for standard_account in [self.standard_account, self.standard_account_kubi] : for standard_account in [self.standard_account]: status = "Done" if (not backup_taken): status = "Done without backup" self.upsert( logger, { "username": standard_account["username"], "password": standard_account["password"], "IP": self.ip, "status": status, "date": datetime.datetime.now(), "version": version, "mac": self.mac, "task_id": self.task_id }) done = True break if (not done): self.upsert( logger, { "username": "******", "password": None, "IP": self.ip, "status": "to kubi", "date": datetime.datetime.now(), "version": None, "mac": None, "task_id": self.task_id }) db.update_task_completed(logger, self.cnn, self.task_id) self.logs_queue.put(('completed', str(self.task_id))) return self.task_id
def init(self): self.logger = utils.log_setup(self.app_name, self.enable_logging) self.engine, self.cnn, self.metadata = db.connect_to_postgres( self.logger, self.DB) self.create_link_table() return self
def test(): from utilities.utils import log_setup logger = log_setup("test route", False, logs_queue = socketio) logger.info("Test") return jsonify({"result" : True})
def run(self): logger = utils.log_setup(self.app_name, self.enable_logging) self.engine, self.cnn, self.metadata = db.connect_to_postgres(logger, self.DB) self.task_id = db.insert_task_processing(logger, self.cnn, self.app_name, datetime.datetime.now(), str(self.server["IP"]), str(self.client["IP"])) self.create_traffic_monitor(logger) try : if(not utils.ip_is_up(self.server["IP"]) or not utils.ip_is_up(self.client["IP"])): raise Exception("IP down") api_conn = Api(self.server["IP"], logger, user=self.server["username"], password=self.server["password"], use_ssl=False, port=8728 ) if(api_conn.successful == False): raise Exception("connection failed") both_res = api_conn.talk([ "/tool/bandwidth-test", "=direction=both", "=address={}".format(self.client["IP"]), "=user={}".format(self.client["username"]), "=password={}".format(self.client["password"]), "=protocol=tcp", "=duration=10s", "=connection-count=20" ]) processed = utils.process_response(both_res) processed = processed[2:-2] both_tx = [int(el['tx_current']) for el in processed] both_rx = [int(el['rx_current']) for el in processed] both_tx_avg = sum(both_tx)/len(both_tx) both_tx_min = min(both_tx) both_tx_max = max(both_tx) both_rx_avg = sum(both_rx)/len(both_rx) both_rx_min = min(both_rx) both_rx_max = max(both_rx) send_res = api_conn.talk([ "/tool/bandwidth-test", "=direction=transmit", "=address={}".format(self.client["IP"]), "=user={}".format(self.client["username"]), "=password={}".format(self.client["password"]), "=protocol=tcp", "=duration=10s", "=connection-count=20" ]) processed = utils.process_response(send_res) processed = processed[2:-2] send_tx = [int(el['tx_current']) for el in processed] send_tx_avg = sum(send_tx)/len(send_tx) send_tx_min = min(send_tx) send_tx_max = max(send_tx) receive_res = api_conn.talk([ "/tool/bandwidth-test", "=direction=receive", "=address={}".format(self.client["IP"]), "=user={}".format(self.client["username"]), "=password={}".format(self.client["password"]), "=protocol=tcp", "=duration=10s", "=connection-count=20" ]) processed = utils.process_response(receive_res) processed = processed[2:-2] receive_rx = [int(el['rx_current']) for el in processed] receive_rx_avg = sum(receive_rx)/len(receive_rx) receive_rx_min = min(receive_rx) receive_rx_max = max(receive_rx) values = { "server_ip" : self.server["IP"], "client_ip" : self.client["IP"], "both_tx_max" : both_tx_max, "both_tx_avg" : both_tx_avg, "both_tx_min" : both_tx_min, "both_rx_max" : both_rx_max, "both_rx_avg" : both_rx_avg, "both_rx_min" : both_rx_min, "send_tx_max" : send_tx_max, "send_tx_avg" : send_tx_avg, "send_tx_min" : send_tx_min, "receive_rx_max" : receive_rx_max, "receive_rx_avg" : receive_rx_avg, "receive_rx_min" : receive_rx_min, "status" : "done", "error_details" : None } if(self.insert_to_db): self.insert(values) except Exception as e: values = { "server_ip" : self.server["IP"], "client_ip" : self.client["IP"], "both_tx_max" : None, "both_tx_avg" : None, "both_tx_min" : None, "both_rx_max" : None, "both_rx_avg" : None, "both_rx_min" : None, "send_tx_max" : None, "send_tx_avg" : None, "send_tx_min" : None, "receive_rx_max" : None, "receive_rx_avg" : None, "receive_rx_min" : None, "status" : "error", "error_details" : str(e) } if(self.insert_to_db): self.insert(values) else : raise Exception(str(e))