def main(): nav = Nav() # 这里需要加一个登陆判断 nav.print_nav() try: while True: try: option = input("\033[1;32mOpt or ID>:\033[0m ").strip() except EOFError: nav.print_nav() continue except KeyboardInterrupt: sys.exit(0) if option in ['P', 'p', '\n', '']: nav.search() # nav.print_search_result() continue # if option.startswith('/'): # nav.search(option.lstrip('/')) # nav.print_all_hosts() # elif gid_pattern.match(option): # nav.get_asset_group_member(str_r=option) # nav.print_search_result() # elif option in ['G', 'g']: # nav.print_asset_group() # continue # elif option in ['E', 'e']: # nav.exec_cmd() # continue # elif option in ['U', 'u']: # nav.upload() # elif option in ['D', 'd']: # nav.download() elif option in ['H', 'h']: nav.print_nav() elif option in ['Q', 'q', 'exit']: sys.exit() else: nav.search(option) # if len(nav.search_result) == 1: # print('Only match Host: %s ' % nav.search_result[0].hostname) # nav.try_connect() # else: # nav.print_search_result() except IndexError as e: color_print(e)
def print_all_hosts(self): system_user_list = self.get_system_user() for i in system_user_list: system_user = i.get('system_user') line = '[%-3s] %-16s %-5s %-' + str(30) + 's %-10s %s' color_print(line % ('ID', 'IP', 'Port', 'Hostname', 'SystemUser', 'Comment'), 'title') server_list = self.get_asset_info() for host in server_list: _id = host.get('id') _ip = host.get('ip') _port = host.get('port') _hostname = host.get('hostname') _system_user = system_user _detail = host.get('detail') # print(line %(_id, _hostname, _port, _admin_user, _detail)) print(line % (_id, _ip, _port, _hostname, _system_user, _detail)) print()
def search(self, input_str=''): # 根据搜索内容进行判断连接什么主机 with DBContext('r') as session: if input_str: try: id_ = int(input_str) print('根据ID进行连接, ID:{}'.format(id_)) server_info = session.query(Server.ip, Server.port).filter(Server.id == id_).first() if not server_info: color_print('没有匹配到任何主机', color='red') return None else: self.start_connect(id_) except (ValueError, TypeError): # print('根据关键字进行匹配连接') server_info_list = session.query(Server).filter(or_( Server.ip.like('%{}%'.format(input_str)), Server.hostname.like('%{}%'.format(input_str)), Server.port.like('%{}%'.format(input_str)), Server.admin_user.like('%{}%'.format(input_str)), Server.detail.like('%{}%'.format(input_str)))).order_by( Server.id).all() server_info_count = session.query(Server).filter(or_( Server.ip.like('%{}%'.format(input_str)), Server.hostname.like('%{}%'.format(input_str)), Server.port.like('%{}%'.format(input_str)), Server.admin_user.like('%{}%'.format(input_str)), Server.detail.like('%{}%'.format(input_str)))).order_by( Server.id).count() if server_info_count == 0: color_print('没有匹配到任何主机', color='red') return if server_info_count == 1: print('直接连接,不让用户再次选择了') for msg in server_info_list: print('连接主机IP:{}'.format(msg.ip)) self.start_connect(msg.id) else: #让用户再次选择匹配出来的数据 line = '[%-3s] %-16s %-5s %-' + str(30) + 's %-10s %s' color_print(line % ('ID', 'IP', 'Port', 'Hostname', 'AdminUser', 'Comment'), 'title') for msg in server_info_list: _id = msg.id _ip = msg.ip _port = msg.port _hostname = msg.hostname _admin_user = msg.admin_user _detail = msg.detail print(line % (_id, _ip, _port, _hostname, _admin_user, _detail)) print() else: # 如果没有输入就展现所有 self.print_all_hosts()