def do_agents(self, args): """ list all agents in interacting """ list_agents = [] # get all session activated if self.settings['agents'].keys() != {}: for agent in self.settings['agents'].keys(): if self.settings['agents'][agent]['tunel'] != None: if self.settings['agents'][agent]['tunel'].activated: list_agents.append([ agent, self.settings['agents'][agent]['creds']['Host'], self.settings['agents'][agent]['creds']['Port'], self.settings['agents'][agent] ['tunel'].session.name, self.settings['agents'][agent]['tunel'].session.pid ]) if list_agents != []: color.display_messages('Session Agents:', info=True, sublime=True) print tabulate(list_agents, headers=funcSQL.sqlite.headersAgents) color.linefeed() return color.display_messages('Online Agents: {}'.format( color.setcolor(str(len(list_agents)), color='blue')), info=True) color.display_messages('No agents in interacting', info=True)
def do_help(self, args): """ show this help """ names = self.get_names() cmds_doc = [] names.sort() pname = '' color.display_messages('Available Commands:', info=True, sublime=True) for name in names: if name[:3] == 'do_': pname = name cmd = name[3:] if getattr(self, name).__doc__: cmds_doc.append((cmd, getattr(self, name).__doc__)) else: cmds_doc.append((cmd, "")) #self.stdout.write('%s\n'%str(self.doc_header)) self.stdout.write(' {} {}\n'.format('Commands', 'Description')) self.stdout.write(' {} {}\n'.format('--------', '-----------')) for command, doc in cmds_doc: self.stdout.write(' {:<10} {}\n'.format(command, doc)) color.linefeed()
def do_check(self, args): """ test all agents login ssh """ agentsCount = 0 self.settings['check'] = [] for agent in self.db.execute(funcSQL.sqlite.selectAllBots): agent = list(agent) agent.insert( len(agent), self.sshConnect.ssh(agent[1], agent[2], agent[3], agent[4], checkconnect=True).status) self.settings['check'].append(agent) color.display_messages('Available Bots:', info=True, sublime=True) print tabulate(self.settings['check'], headers=funcSQL.sqlite.headersCheck) for items in self.settings['check']: if search('ON', items[6]): agentsCount += 1 color.linefeed() color.display_messages('Online Agents: {}'.format( color.setcolor(str(agentsCount), color='blue')), info=True)
def do_register(self, args): """ add bot on database """ arg_parser = argparse.ArgumentParser( prog='register', description='add bot on database clients') arg_parser.add_argument('--host', dest='host', metavar='<Host>', help='ipaddress/host/dns connect ssh') arg_parser.add_argument('--pass', dest='password', metavar='<Password>', help='password ssh client') arg_parser.add_argument('-u', '--user', dest='user', metavar='<user>', help='delete all bot registered') arg_parser.add_argument('-p', '--port', dest='port', metavar='<Port>', default='22', help='port connect ssh') arg_parser.add_argument('-f', '--file', dest='file', metavar='<filepath>', help='imports clients from a file') try: args = arg_parser.parse_args(shlex.split(args)) except: return if args.host and args.password and args.user: color.display_messages( 'Insert Data: SQL statement will insert a new row', info=True) funcSQL.DB_insert(self.con, self.db, args.host, args.port, args.user, args.password) color.display_messages('credentials ssh added with success', sucess=True) elif args.file: self.all_bot_checked = [] color.display_messages('searching for: {} ...'.format( path.realpath(args.file)), info=True) if path.exists(args.file): self.lines_all_read = [ line.rstrip('\n') for line in open(path.realpath(args.file), 'r') ] for items in self.lines_all_read: if len(items.split()) == 4: self.all_bot_checked.append(items) if len(self.all_bot_checked) == 0: color.display_messages('Instruction for -f argumments:', info=True, sublime=True) print( 'You need to use the separator character [space] in this format below\n' ) print('-----cut here -------\n') print('root:~# cat example.txt\n') print('192.168.0.100 22 DemoBOT P@ssW0rd') print('192.168.0.120 22 Userdr4g0n botP@ssW0rd') print('\n-----cut here -------\n') print('') return None self.ListBot = [] color.display_messages('All agents imported from file:', info=True, sublime=True) for agent in self.all_bot_checked: self.ListBot += list([agent.split()]) print tabulate(self.ListBot, headers=funcSQL.sqlite.headersimport) color.linefeed() choise = raw_input( '{}[*]{} Do you want to import?(S/N):'.format( color.colors.GREEN, color.colors.ENDC, )) if choise.lower() == 's': color.display_messages('Importing agents...', info=True) for agent in self.all_bot_checked: funcSQL.DB_insert(self.con, self.db, agent.split()[0], agent.split()[1], agent.split()[2], agent.split()[3]) return color.display_messages( 'all agents ssh added with success', sucess=True) color.display_messages('import was been canceled.', error=True) else: color.display_messages('file: could not be found', error=True) else: arg_parser.print_help()
def do_list(self, args): """ list/check/filter list agents on database """ arg_parser = argparse.ArgumentParser( prog='list', description='interact with one/all agents') arg_parser.add_argument('-i', '--id', dest='id', type=int, metavar='<id>', help='list agent by id') arg_parser.add_argument('-c', '--check', dest='check', action='store_true', help='check credentials by agent Available') arg_parser.add_argument('-d', '--db', dest='database', action='store_true', help='list all agents on database') try: args = arg_parser.parse_args(shlex.split(args)) except: return self.settings['all'] = {} self.listbotsprint = [] for agent in self.db.execute(funcSQL.sqlite.selectAllBots): self.settings['all'][agent[0]] = agent if self.settings['all'] == {}: return color.display_messages('No Agents registered', info=True) if args.database: if args.id and not args.check: if not args.id in self.settings['all'].keys(): return color.display_messages('ID not registered', info=True) color.display_messages('Agents:', info=True, sublime=True) agent = list(self.settings['all'][args.id]) self.listbotsprint += list([agent]) print tabulate(self.listbotsprint, headers=funcSQL.sqlite.headersCheck) elif args.check and args.id: if not args.id in self.settings['all'].keys(): return color.display_messages('ID not registered', info=True) color.display_messages('Agents:', info=True, sublime=True) agent = list(self.settings['all'][args.id]) agent.insert( len(agent), self.sshConnect.ssh(agent[1], agent[2], agent[3], agent[4], checkconnect=True).status) self.listbotsprint += list([agent]) print tabulate(self.listbotsprint, headers=funcSQL.sqlite.headersCheck) elif args.database and not args.check: color.display_messages('Agents:', info=True, sublime=True) for bots in self.settings['all'].items(): self.listbotsprint += list([bots[1]]) print tabulate(self.listbotsprint, headers=funcSQL.sqlite.headers) return color.linefeed() elif args.database and not args.id: color.display_messages('Agents:', info=True, sublime=True) for agent in self.settings['all'].items(): agent = list(agent[1]) agent.insert( len(agent), self.sshConnect.ssh(agent[1], agent[2], agent[3], agent[4], checkconnect=True).status) self.listbotsprint += list([agent]) print tabulate(self.listbotsprint, headers=funcSQL.sqlite.headersCheck) color.linefeed() else: arg_parser.print_help()