def run(self): Console.log('Lets clone you some sites') time.sleep(.25) self.get_website_url() time.sleep(.25) self.get_path_to_store_cloned_site() self.clone_site()
def compress_operation(self): time.sleep(.5) Console.log( 'Compress files and folders, even encrypt them if option chosen') is_valid = False while is_valid is False: abs_path_turple, is_valid = self.get_user_input() self.process_compression(abs_path_turple)
def write_encripted_data_to_file(self, file_data): file = f'encrypted_{self.file_name}' file = os.path.join(self.file_root_dir, file) Console.info('Writting encrypted data to file...') with open(file, 'wb') as f: time.sleep(1) file_data.write(f) Console.log(f'{file} written successfully')
def decompress_operation(self): time.sleep(.3) path = input( f"\n{Console.green('Enter path to file or folder you want to decompress > ')}" ) file_path_turple, is_path_valid = self.validate_path(path) if is_path_valid is not True: self.decompress_operation() if self.process_de_compression(file_path_turple) is not True: self.decompress_operation() Console.log('Done decompressing...') time.sleep(.3) self.remove_file(file_path_turple[0])
def generate_password(self): passwd_chars = self.get_chosen_chars() Console.info(f'Your choice {passwd_chars}') time.sleep(.2) Console.info('Generating password, Please wait...') time.sleep(1) pass_list = random.sample(passwd_chars, int(self.password_length)) generated_passwd = "".join(pass_list) Console.info( f'Your newly generated password is {Console.green(generated_passwd)}' ) time.sleep(.4) Console.log('[+] Done') raise KeyboardInterrupt
def choose_to_delete(self, file_name): choice = input( Console.yellow(f'Do you wish to delete {file_name}? (N/y) > ')) choice = choice.strip().lower() not_valid = choice not in ['n', 'y'] if not_valid: Console.error('Please choose a valid input!') time.sleep(.2) self.choose_to_delete(file_name) if choice == 'n': Console.log('Bye!') return Console.info(f'Deleting {file_name}...') delete_file(file_name)
def choose_to_copy_files(self): choosen = 'y' choice = input( f"\n{Console.green('Do you wish to copy these files? (Y/n) > ')}") if choice.strip() == '': choice = choosen else: choice = choice.strip() if 'y' != choice.lower() and 'n' != choice.lower(): Console.error('Wrong choice. Try again') self.choose_to_copy_files() elif 'n' in choice.lower(): Console.log(f'Bye!!') elif 'y' in choice.lower(): self.copy_files() else: Console.log(f'Something went wrong')
def process_de_compression(self, filename): if not filename: return file_path, file_name, parent_dir = filename # print(shutil.get_unpack_formats()) extraction_file_name = ''.join(file_name.split('.')[:-2]) extraction_folder_name = os.path.join(parent_dir, extraction_file_name) Console.log( f'Decompressing to { extraction_folder_name } , Please wait...') time.sleep(.3) try: shutil.unpack_archive(filename=file_path, extract_dir=extraction_folder_name) return True except shutil.ReadError as e: Console.error('Folder not of valid type') return False
def find_file(self, file_name): self.file_to_find = file_name[0] Console.log( f'Finding "{Console.green(self.file_to_find)}" in {Console.green(file_name[1][0])}. Please wait...' ) for file_obj in os.walk(file_name[1][0]): self.fnames = file_obj[2] self.dir_path = file_obj[0] multi_process_this(self.fnames, self.actual_finding_of_a_file) print('\n') print_out = f"found {len(self.found_files)} resluts of '{self.file_name}'" time.sleep(.2) Console.warn(line(print_out)) time.sleep(.2) Console.log(print_out) time.sleep(.2) Console.warn(line(print_out, '-')) if len(self.found_files): self.choose_to_copy_files()
class UTMFailOver: def __init__(self, cfg, curr, targ): self.cfg = cfg self.LOG = Console() # 상태 : active, standby # curr : 현재상태 # targ : 다음상태 self.curr = curr self.targ = targ def loop_l3_connectivity(self, run_l3_check=False, max_count = 10, time_sleep=3): # L3 연결성 체크 count = 0 if run_l3_check: while (count < max_count): if self.check_l3(): break self.LOG.log(" ====================================================") if count != max_count-1: time.sleep(time_sleep) count = count + 1 # utm의 interface가 올라왔는지 ping과 arp로 확인한다. # target의 ip 주소로 ping을 하고, # 그 결과 arp를 확인하여 mac주소가 설정값과 동일한지 확인 def is_l3_connected(self, ip_addr, mac_in_cfg): mac_in_real = NetUtil().get_mac_from_arp(ip_addr) self.LOG.log(" - CONF ip= %-15s mac= %s" % (ip_addr, mac_in_cfg)) self.LOG.log(" - REAL ip= %-15s mac= %s" % (ip_addr, mac_in_real)) if mac_in_cfg.upper() == mac_in_real.upper(): return True else: return False def check_l3(self): customer = self.cfg.get("failover", "customer") ip_red = self.cfg.get(customer, "ip_red") ip_green = self.cfg.get(customer, "ip_green") mac_red_in_cfg = self.cfg.get(self.target, "port_red_mac") mac_green_in_cfg = self.cfg.get(self.target, "port_green_mac") self.LOG.info(" *** [INFO] Checking L3 connectivity...") red_l3_failover = is_l3_connected(ip_red, mac_red_in_cfg) green_l3_failover = is_l3_connected(ip_green, mac_green_in_cfg) if red_l3_failover and green_l3_failover: self.LOG.info(" *** 성공!!!") return True else: self.LOG.error(" *** 진행중!!!") return False # L2 연결성 체크, 성공시 L3 연결 체크 문의 def loop_l2_connectivity(self, ask_l3_check=False, max_count = 10, time_sleep=3 ): run_l3_check=False count = 0 while (count < max_count): if self.check_l2(): if ask_l3_check: run_l3_check=InteractionUtil().ask_user("L3 연결을 체크하시겠습니까?") break self.LOG.log(" ====================================================") if count != max_count-1: time.sleep(time_sleep) count = count + 1 return run_l3_check # 스위치 인터페이스 중 active가 disable되고 # standby가 active되었는지를 확인 def is_l2_connected(self, intf_curr, intf_targ): l2_connected = False if intf_curr.is_disabled() and intf_targ.is_enabled(): l2_connected = True else: l2_connected = False return l2_connected # switch의 interface가 올라왔는지 확인한다. # current interface는 down되고, target interface는 up def check_l2(self): port_red_curr = self.cfg.get(self.curr, "port_red") port_red_targ = self.cfg.get(self.targ, "port_red") port_green_curr = self.cfg.get(self.curr, "port_green") port_green_targ = self.cfg.get(self.targ, "port_green") intf_red_curr = Interface(port_red_curr) intf_red_targ = Interface(port_red_targ) intf_green_curr = Interface(port_green_curr) intf_green_targ = Interface(port_green_targ) red_failover = self.is_l2_connected(intf_red_curr, intf_red_targ) green_failover = self.is_l2_connected(intf_green_curr, intf_green_targ) self.LOG.info(" *** [INFO] Checking L2 connectivity...") self.LOG.log(" - %-5s interface failover result= %s" % ("RED", red_failover)) self.LOG.log(" - %-5s interface failover result= %s" % ("GREEN", green_failover)) if red_failover and green_failover: self.LOG.log(" ----------------------------------------------------") self.LOG.info(" *** 성공!!!") self.LOG.log(" ----------------------------------------------------") return True else: self.LOG.log(" ----------------------------------------------------") self.LOG.error(" *** 진행중!!!") self.LOG.log(" ----------------------------------------------------") return False def failover(self, curr, targ): curr_port_green = self.cfg.get(curr, "port_green") curr_port_red = self.cfg.get(curr, "port_red") targ_port_green = self.cfg.get(targ, "port_green") targ_port_red = self.cfg.get(targ, "port_red") intf_curr_green = Interface(curr_port_green) intf_curr_red = Interface(curr_port_red) intf_targ_green = Interface(targ_port_green) intf_targ_red = Interface(targ_port_red) self.LOG.info(" *** [INFO] Running failover for GREEN interface") self.LOG.log(" - Shutdown interface= %s" % intf_curr_green.interface_id) intf_curr_green.shutdown(True) self.LOG.log(" - Enable interface= %s" % intf_targ_green.interface_id) intf_targ_green.shutdown(False) self.LOG.info(" *** [INFO] Running failover for RED interface") self.LOG.log(" - Shutdown interface= %s" % intf_curr_red.interface_id) intf_curr_red.shutdown(True) self.LOG.log(" - Enable interface= %s" % intf_targ_red.interface_id) intf_targ_red.shutdown(False) self.LOG.log(" ----------------------------------------------------") def is_applied(self, cfg_name): customer = self.cfg.get("failover", "customer") cfg_red_ip = self.cfg.get(customer, "red_ip") cfg_green_ip = self.cfg.get(customer, "green_ip") cfg_red_port = self.cfg.get(cfg_name, "port_red") cfg_green_port = self.cfg.get(cfg_name, "port_green") cfg_orange_port = self.cfg.get(cfg_name, "port_orange") intf_red = Interface(cfg_red_port) intf_green = Interface(cfg_green_port) intf_orange = Interface(cfg_orange_port) if intf_red.is_enabled() and intf_green.is_enabled() and intf_orange.is_enabled(): return True else: return False def show_config(self, cfg_name, title): self.LOG.log(" ----------------------------------------------------") self.LOG.info(" *** %s (%s)" % ( title, cfg_name) ) self.LOG.log(" ----------------------------------------------------") customer = self.cfg.get("failover", "customer") cfg_red_ip = self.cfg.get(customer, "red_ip") cfg_green_ip = self.cfg.get(customer, "green_ip") cfg_red_port = self.cfg.get(cfg_name, "port_red") cfg_green_port = self.cfg.get(cfg_name, "port_green") cfg_orange_port = self.cfg.get(cfg_name, "port_orange") intf_red = Interface(cfg_red_port) intf_green = Interface(cfg_green_port) intf_orange = Interface(cfg_orange_port) self.LOG.info(" - %s" % "RED") self.LOG.log(" . switch port= %s (%s)" % (cfg_red_port, intf_red.get_status())) self.LOG.log(" . ip address = %s" % cfg_red_ip) self.LOG.info(" - %s" % "GREEN") self.LOG.log(" . switch port= %s (%s)" % (cfg_green_port, intf_red.get_status())) self.LOG.log(" . ip address = %s" % cfg_green_ip) self.LOG.info(" - %s" % "ORANGE") self.LOG.log(" . switch port= %s (%s)" % (cfg_orange_port, intf_red.get_status())) self.LOG.log(" ----------------------------------------------------")
def write_to_file(self, data): fname = 'districts.json' Console.log(f'Writting results to {fname}...') time.sleep(.5) with open(fname, 'w') as f: f.write(json.dumps(data))
def copy_files(self): Console.log(f'Copying {len(self.found_files)}') self.file_dest, self.folder_name = self.get_copy_destination() multi_process_this(self.found_files, self.copy_file)
def found_file_cb(self, found_file): self.found_files.append(found_file) Console.log(f'file found at : {Console.yellow(found_file)}')
def actual_compression(self, fname): # print(shutil.get_archive_formats()) shutil.make_archive(fname, 'gztar', fname) Console.log('Compression has finished successfully')