def setUp(self): host_data = [ { "hostname": "demo-web1", "ip": "192.168.33.101", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web2", "ip": "192.168.33.102", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web3", "ip": "192.168.33.103", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web4", "ip": "192.168.33.104", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, ] inventory = BaseInventory(host_data) self.runner = CommandRunner(inventory)
async def is_available(self) -> bool: headers = {'content-type': 'application/json'} async with aiohttp.ClientSession(connector=aiohttp.TCPConnector( verify_ssl=False)) as session: try: response = await session.post(self.address + self.CHECK_ENDPOINT, data=json.dumps({ 'password': self.password }).encode('utf-8'), headers=headers) if (await response.text()) != self.OK_ANSWER: print( f"Seems that you specified wrong password for host {self.address}" ) return False if self.architecture == '': await self.get_server_info() this_computer_architecture = CommandRunner( '/').run_one_command('uname -m').strip() if this_computer_architecture != self.architecture: print( f"Host {self.address} has architecture {self.architecture}, which is not te same as yours ({this_computer_architecture})" ) return False return True except Exception as e: print(e) print(f"Seems that host {self.address} was unable to reach") return False
def build(): logger = logging.create_logger(app) print(request.form) password = request.form['password'] if not is_password_acceptable(password): return make_response('', 400) # Extracting files tempdir = tempfile.TemporaryDirectory() print(request.files) f = request.files['file'] commands_file = request.form['commands_file'].strip('/') archive_file = tempfile.NamedTemporaryFile(suffix='.tar.xz', dir=tempdir.name) archive_filename = archive_file.name.split('/')[-1] print(archive_filename) f.save(archive_file.name) compressor = Compressor(tempdir.name) compressor.extract_files(archive_filename) new_root = f'{tempdir.name}/{archive_filename.split(".")[0]}/' archive_file.close() # Run commands print(new_root) command_runner = CommandRunner(new_root + request.form['workdir'].strip('/'), app.config['LIBRARIES']) output, code = command_runner.run_commands(new_root + commands_file, new_root, logger, request.form['exact_lib_versions'] == 'true') if code != 0: logger.debug(code) response = output, 400 else: # Sending files back target_files = [target.strip('/') for target in request.form['targets'].split(', ')] compressor.root_path = new_root output_file = tempfile.NamedTemporaryFile(suffix='.tar.xz') compressor.compress(target_files, output_file.name) response = send_file(output_file, mimetype='application/x-object') tempdir.cleanup() return response
def get_info(): try: data = json.loads(request.data) if not is_password_acceptable(data['password']): return jsonify({}) return jsonify({'architecture': CommandRunner.run_one_command('uname -m').strip()}) except Exception as e: print(e) return jsonify({})
async def get_headers_dependencies(self): if not config.USE_CXX_M: return added = set(self.all_dependency_files) for file in self.all_dependency_files: try: r = CommandRunner( os.getcwd()).run_one_command(f'g++ -M {file}') for line in r.splitlines()[1:]: headers = line.strip().strip('\\').strip().split() for header in headers: added.add(header) except: continue self.all_dependency_files = list( set(self.all_dependency_files).union(added)) self.dependencies_files_only = list( set(self.dependencies_files_only).union(added))
def TestCommandRunner(): """ 执行单个命令,返回结果 :return: """ host_data = [ { "hostname": "10.211.55.13", "ip": "10.211.55.13", "port": 22, "username": "******", # "private_key": "/home/python/.ssh/id_rsa_1", }, ] inventory = Inventory(host_data) runner = CommandRunner(inventory) res = runner.execute('pwd', 'all') print(res.results_command) print(res.results_raw) print(res.results_command['10.211.55.13']['stdout'])
def TestCommandRunner(): """ 执行单个命令,返回结果 :return: """ host_data = [ { "hostname": "testserver", "ip": "192.168.10.93", "port": 22, "username": "******", "password": "******", }, ] inventory = BaseInventory(host_data) runner = CommandRunner(inventory) res = runner.execute('pwd', 'all') print(res.results_command) print(res.results_raw) print(res.results_command['testserver']['stdout'])
def TestCommandRunner(): """ 执行单个命令,返回结果 :return: """ host_data = [ { "hostname": "localhost", "ip": "152.55.249.20", "port": 22, "username": "******", "password": "******", }, ] #xx=[{'ip': '127.0.0.1', 'hostname': 'localhost', 'username': '******', 'password': '******', 'port': '22', 'groups': ['group1'], 'vars': {'var1': 'xxxx', 'var2': 'yyy'}}] runner = CommandRunner(host_data) res = runner.execute('pwd', 'all') print(res.results_command) print(res.results_raw) print(res.results_command['localhost']['stdout'])
async def get_libraries_dependencies(self): if not config.CHECK_MORE_DEPENDENCIES: return dependencies_libraries = { file for file in self.dependencies_files_only if file.endswith('.so') or '.so.' in file } added = len(dependencies_libraries) != 0 while added: new_dependencies = set() added = False for library in dependencies_libraries: library_dir = os.path.dirname(library) library_filename = library.split('/')[-1] # check symlinks try: result = CommandRunner(os.getcwd()).run_one_command( f'ls {library_dir} -la | grep {library_filename}') if ' -> ' in result: lib = result.split(' -> ')[1].split('\n')[0].strip() if not lib.startswith('/'): lib = library_dir + '/' + result.split( ' -> ')[1].split('\n')[0].strip() if lib not in dependencies_libraries: new_dependencies.add(lib) added = True except: pass try: # Check other dependencies result = CommandRunner( os.getcwd()).run_one_command(f'ldd {library}') for line in result.splitlines(): if match := re.match(r'.*\s(/.+?)\s', line): if match.groups( '0')[0] not in dependencies_libraries: new_dependencies.add(match.groups('0')[0]) added = True except: pass for library in new_dependencies: dependencies_libraries.add(library) self.dependencies_files_only = list( set(self.dependencies_files_only).union(dependencies_libraries)) self.all_dependency_files = list( set(self.all_dependency_files).union(dependencies_libraries))
class TestCommandRunner(unittest.TestCase): def setUp(self): host_data = [ { "hostname": "demo-web1", "ip": "192.168.33.101", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web2", "ip": "192.168.33.102", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web3", "ip": "192.168.33.103", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web4", "ip": "192.168.33.104", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, ] # inventory = BaseInventory(host_data) self.runner = CommandRunner(inventory=host_data) def test_execute(self): # res = self.runner.execute('ls', 'all') res = self.runner.execute(cmd='ls', pattern='all') print(res.results_command) print(res.results_raw)
class TestPingRunner(unittest.TestCase): def setUp(self): host_data = [ { "hostname": "demo-web1", "ip": "192.168.33.101", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web2", "ip": "192.168.33.102", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web3", "ip": "192.168.33.103", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, { "hostname": "demo-web4", "ip": "192.168.33.104", "port": 2222, "username": "******", "private_key": "/Users/maliao/.ssh/id_rsa", }, ] inventory = BaseInventory(host_data) self.runner = CommandRunner(inventory) def test_ping(self): tasks = [ { "action": { "module": "ping", "data": "pong" }, "name": "check connect to host" }, ] # ret = self.runner.run(tasks, "all") ret = self.runner.run(tasks, "demo*") print(ret.results_summary) print(ret.results_raw) def test_idrac(self): tasks = [ { "action": { "module": "shell", "args": "racadm getsysinfo" }, "name": "check connect to host" }, ] # ret = self.runner.run(tasks, "all") ret = self.runner.run(tasks, "esxi1*") print(ret.results_summary) print(ret.results_raw)
async def get_compiled_file(self, session: aiohttp.ClientSession, target: BuildTarget, lock: asyncio.Lock, compressor: Compressor): print(f"Building the target locally...") for command in target.initial_bash_commands: CommandRunner(os.getcwd()).run_one_command(command)