def get_pdf_file(self): try: print(f"[+] file being processed {self.file}") return PdfFileReader(self.file) except FileNotFoundError as e: Console.error(e) exit(1)
def appFactory(choice, modules): try: Console.info(f"{modules[str(choice)]['desc']}") time.sleep(.5) return modules[str(choice)]['name']() except KeyError as e: Console.error('Wrong module') exit(1)
def getUserOptions(self): self.instructions() self.get_user_choice() while self.is_choice_valid(self.user_choice) is False: Console.error('Please enter a valid option') self.get_user_choice() self.generate_password()
def get_place(self): place_name = input( f"\n{Console.green('Where to find the file (Valid path) > ')}") folder_path = get_file_or_folder_path(place_name) if folder_path: self.folder_path = folder_path return Console.error('Path should be valid...') self.get_place()
def get_path_to_store_cloned_site(self): path_to_store = input( f"\n{Console.green('Enter path to store cloned site ( Leave empty to store in current directory ) > ')}" ) path_to_store = get_file_or_folder_path(path_to_store) if path_to_store == None: Console.error('Invalid path. Please try again!') return self.get_path_to_store_cloned_site() self.path_to_store = path_to_store[0]
def get_file(self): file_name = input( f"\n{Console.green('Enter name of a file you would like to find > ')}" ) valid = len(file_name.strip()) > 1 if valid: self.file_name = file_name return Console.error('Can not be empty, please try again...') self.get_file()
def run(self): self.get_file() passwd = input( f'Please enter a secure password Default ({self.passwd})> ') if passwd.strip(): self.passwd = passwd try: self.enc_pdf() except Exception as e: Console.error(str(e)) self.run()
def get_file(self): file = input('Please enter a valid pdf file name > ') file_path, file_name, file_root_dir = get_file_or_folder_path(file) if not file_path or file_name.split('.')[-1].lower() != 'pdf': Console.error( f'{file} does not exist or not pdf. Try again or (Ctrl + C) to quit' ) self.get_file() self.file = file_path self.file_name = file_name self.file_root_dir = file_root_dir
def main(modules): time.sleep(.3) Console.info('Welcome to libs helper\n') time.sleep(.5) instructions(modules) try: choice = int(input('\nPick your poison > ')) app = appFactory(choice, modules) app.run() except ValueError as e: Console.error('Invalid input') exit(1)
def validate_path(self, path): if len(path) == 0: Console.error('Path can not be empty') return None, False abs_path_turple = get_file_or_folder_path(path) if not abs_path_turple: Console.error( 'Path to file or folder does not exist. Try again...') return None, False return abs_path_turple, True
def user_choice(self, next_page_url=None): choice = input( f"\n{Console.green('Enter (n) for next page or post number for post details > ')}" ) try: post_url = self.posts[int(choice) - 1]['post_link'] self.get_post_details(post_url) except Exception: if choice.strip().lower() == 'n': self.parse_next_page(next_page_url) else: Console.error("Wrong choice") self.user_choice()
def get_copy_destination(self): dest = input( f"\n{Console.green('Do you wish to copy these files to? > ')}") folder_name = input( f"\n{Console.yellow('Folder name to store files ? > ')}") destination_turple = get_file_or_folder_path(dest) if destination_turple is not None and folder_name.strip(): return destination_turple[0], folder_name else: Console.error( 'Invalid path or missing folder name. Please try again!') return self.get_copy_destination()
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 get_user_input(self): choice = None try: choice = int( input( f"\n{Console.green('Choose (1) for media files, (2) for other queries > ')}" )) except Exception: pass if choice not in [1, 2]: Console.error('Wrong input. Please try again!') return self.get_user_input() if choice == 2: self.get_file() else: self.file_name = self.supported_media_files self.get_place() return self.file_name, self.folder_path
def choose_operation(self): Console.warn('\t\t Choose ( 1 ) for compressing') time.sleep(.5) Console.warn('\t\t Choose ( 2 ) for decompressing') time.sleep(.3) operation = input(f"\n{Console.green('Operation > ')}") try: choice = int(operation) if choice not in [1, 2]: Console.error('Not among opertaion. Retry again...') time.sleep(.3) self.choose_operation() if choice == 1: return self.compress_operation() else: return self.decompress_operation() except Exception as e: # print(e.with_traceback()) Console.error('Invalid opertaion. Retry again...') time.sleep(.3) return self.choose_operation()
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 encrypt_file(self, filename): try: GenKeys.encrypt(filename[0]) except Exception as e: Console.error(str(e))
def get_website_url(self): url = input(f"\n{Console.green('Enter URL of the site to clone > ')}") if url.strip() == '': Console.error('URL can not be empty. Please try again!') return self.get_website_url() self.url = url
def get_passwd_lenght(self): try: self.password_length = int(input('[-] Length of a password > ')) except ValueError: Console.error('Invalid password length') self.get_passwd_lenght()