def main(user, pkey, password, hosts_str, cmd, file_path, dir_path, delete_dir): hosts = hosts_str.split(",") client = ParallelSSHClient(user=user, pkey_file=pkey, password=password, hosts=hosts) pp = pprint.PrettyPrinter(indent=4) if file_path: if not os.path.exists(file_path): raise Exception("File not found.") results = client.put(file_path, "/home/lakshmi/test_file", mode="0660") pp.pprint("Copy results: \n%s" % results) results = client.run("ls -rlth") pp.pprint("ls results: \n%s" % results) if dir_path: if not os.path.exists(dir_path): raise Exception("File not found.") results = client.put(dir_path, "/home/lakshmi/", mode="0660") pp.pprint("Copy results: \n%s" % results) results = client.run("ls -rlth") pp.pprint("ls results: \n%s" % results) if cmd: results = client.run(cmd) pp.pprint("cmd results: \n%s" % results) if delete_dir: results = client.delete_dir(delete_dir, force=True) pp.pprint("Delete results: \n%s" % results)
def test_run_command(self): hosts = ['localhost', '127.0.0.1', 'st2build001'] client = ParallelSSHClient(hosts=hosts, user='******', pkey_file='~/.ssh/id_rsa', connect=True) client.run('pwd', timeout=60) expected_kwargs = {'timeout': 60, 'call_line_handler_func': True} for host in hosts: hostname, _ = client._get_host_port_info(host) client._hosts_client[hostname].run.assert_called_with( 'pwd', **expected_kwargs)
def test_run_command(self): hosts = ["localhost", "127.0.0.1", "st2build001"] client = ParallelSSHClient(hosts=hosts, user="******", pkey_file="~/.ssh/id_rsa", connect=True) client.run("pwd", timeout=60) expected_kwargs = {"timeout": 60, "call_line_handler_func": True} for host in hosts: hostname, _ = client._get_host_port_info(host) client._hosts_client[hostname].run.assert_called_with( "pwd", **expected_kwargs)
def test_run_command(self): hosts = ['localhost', '127.0.0.1', 'st2build001'] client = ParallelSSHClient(hosts=hosts, user='******', pkey_file='~/.ssh/id_rsa', connect=True) client.run('pwd', timeout=60) expected_kwargs = { 'timeout': 60 } for host in hosts: hostname, _ = client._get_host_port_info(host) client._hosts_client[hostname].run.assert_called_with('pwd', **expected_kwargs)
def test_run_command_timeout(self): # Make sure stdout and stderr is included on timeout hosts = ['localhost', '127.0.0.1', 'st2build001'] client = ParallelSSHClient(hosts=hosts, user='******', pkey_file='~/.ssh/id_rsa', connect=True) mock_run = Mock( side_effect=SSHCommandTimeoutError(cmd='pwd', timeout=10, stdout='a', stderr='b', ssh_connect_timeout=30)) for host in hosts: hostname, _ = client._get_host_port_info(host) host_client = client._hosts_client[host] host_client.run = mock_run results = client.run('pwd') for host in hosts: result = results[host] self.assertEqual(result['failed'], True) self.assertEqual(result['stdout'], 'a') self.assertEqual(result['stderr'], 'b') self.assertEqual(result['return_code'], -9)
def test_run_command_timeout(self): # Make sure stdout and stderr is included on timeout hosts = ["localhost", "127.0.0.1", "st2build001"] client = ParallelSSHClient(hosts=hosts, user="******", pkey_file="~/.ssh/id_rsa", connect=True) mock_run = Mock( side_effect=SSHCommandTimeoutError(cmd="pwd", timeout=10, stdout="a", stderr="b", ssh_connect_timeout=30)) for host in hosts: hostname, _ = client._get_host_port_info(host) host_client = client._hosts_client[host] host_client.run = mock_run results = client.run("pwd") for host in hosts: result = results[host] self.assertEqual(result["failed"], True) self.assertEqual(result["stdout"], "a") self.assertEqual(result["stderr"], "b") self.assertEqual(result["return_code"], -9)
def test_run_command_json_output_transformed_to_object(self): hosts = ['127.0.0.1'] client = ParallelSSHClient(hosts=hosts, user='******', pkey_file='~/.ssh/id_rsa', connect=True) results = client.run('stuff', timeout=60) self.assertIn('127.0.0.1', results) self.assertDictEqual(results['127.0.0.1']['stdout'], {'foo': 'bar'})
def test_run_command_json_output_transformed_to_object(self): hosts = ['127.0.0.1'] client = ParallelSSHClient(hosts=hosts, user='******', pkey_file='~/.ssh/id_rsa', connect=True) results = client.run('stuff', timeout=60) self.assertTrue('127.0.0.1' in results) self.assertDictEqual(results['127.0.0.1']['stdout'], {'foo': 'bar'})
def test_run_command_json_output_transformed_to_object(self): hosts = ["127.0.0.1"] client = ParallelSSHClient(hosts=hosts, user="******", pkey_file="~/.ssh/id_rsa", connect=True) results = client.run("stuff", timeout=60) self.assertIn("127.0.0.1", results) self.assertDictEqual(results["127.0.0.1"]["stdout"], {"foo": "bar"})
def test_run_sudo_password_user_friendly_error(self): hosts = ['127.0.0.1'] client = ParallelSSHClient(hosts=hosts, user='******', pkey_file='~/.ssh/id_rsa', connect=True, sudo_password=True) results = client.run('stuff', timeout=60) expected_error = ('Failed executing command "stuff" on host "127.0.0.1" ' 'Invalid sudo password provided or sudo is not configured for ' 'this user (bar)') self.assertIn('127.0.0.1', results) self.assertEqual(results['127.0.0.1']['succeeded'], False) self.assertEqual(results['127.0.0.1']['failed'], True) self.assertIn(expected_error, results['127.0.0.1']['error'])
def test_run_sudo_password_user_friendly_error(self): hosts = ['127.0.0.1'] client = ParallelSSHClient(hosts=hosts, user='******', pkey_file='~/.ssh/id_rsa', connect=True, sudo_password=True) results = client.run('stuff', timeout=60) expected_error = ('Failed executing command "stuff" on host "127.0.0.1" ' 'Invalid sudo password provided or sudo is not configured for ' 'this user (bar)') self.assertTrue('127.0.0.1' in results) self.assertEqual(results['127.0.0.1']['succeeded'], False) self.assertEqual(results['127.0.0.1']['failed'], True) self.assertTrue(expected_error in results['127.0.0.1']['error'])
def test_run_sudo_password_user_friendly_error(self): hosts = ["127.0.0.1"] client = ParallelSSHClient( hosts=hosts, user="******", pkey_file="~/.ssh/id_rsa", connect=True, sudo_password=True, ) results = client.run("stuff", timeout=60) expected_error = ( 'Failed executing command "stuff" on host "127.0.0.1" ' "Invalid sudo password provided or sudo is not configured for " "this user (bar)") self.assertIn("127.0.0.1", results) self.assertEqual(results["127.0.0.1"]["succeeded"], False) self.assertEqual(results["127.0.0.1"]["failed"], True) self.assertIn(expected_error, results["127.0.0.1"]["error"])
def test_run_command_timeout(self): # Make sure stdout and stderr is included on timeout hosts = ['localhost', '127.0.0.1', 'st2build001'] client = ParallelSSHClient(hosts=hosts, user='******', pkey_file='~/.ssh/id_rsa', connect=True) mock_run = Mock(side_effect=SSHCommandTimeoutError(cmd='pwd', timeout=10, stdout='a', stderr='b')) for host in hosts: hostname, _ = client._get_host_port_info(host) host_client = client._hosts_client[host] host_client.run = mock_run results = client.run('pwd') for host in hosts: result = results[host] self.assertEqual(result['failed'], True) self.assertEqual(result['stdout'], 'a') self.assertEqual(result['stderr'], 'b') self.assertEqual(result['return_code'], -9)