def test_shell_error_bad_port(self, _, password): sshcp = Sshcp(host=self.host, port=6666, user=self.user, password=password) with self.assertRaises(SshcpError) as ctx: sshcp.shell("cd {}; pwd".format(self.local_dir)) self.assertTrue("Connection refused by server" in str(ctx.exception))
def test_shell_error_bad_command(self, _, password): sshcp = Sshcp(host=self.host, port=self.port, user=self.user, password=password) with self.assertRaises(SshcpError) as ctx: sshcp.shell("./some_bad_command.sh".format(self.local_dir)) self.assertTrue("./some_bad_command.sh" in str(ctx.exception))
def test_shell_error_bad_password(self): sshcp = Sshcp(host=self.host, port=self.port, user=self.user, password="******") with self.assertRaises(SshcpError) as ctx: sshcp.shell("cd {}; pwd".format(self.local_dir)) self.assertEqual("Incorrect password", str(ctx.exception))
def test_shell_error_bad_host(self, _, password): sshcp = Sshcp(host="badhost", port=self.port, user=self.user, password=password) with self.assertRaises(SshcpError) as ctx: sshcp.shell("cd {}; pwd".format(self.local_dir)) self.assertTrue("Bad hostname" in str(ctx.exception))
def test_shell(self, _, password): sshcp = Sshcp(host=self.host, port=self.port, user=self.user, password=password) out = sshcp.shell("cd {}; pwd".format(self.local_dir)) out_str = out.decode().strip() self.assertEqual(self.local_dir, out_str)
def test_copy_error_bad_password(self): sshcp = Sshcp(host=self.host, port=self.port, user=self.user, password="******") with self.assertRaises(SshcpError) as ctx: sshcp.copy(local_path=self.local_file, remote_path=self.remote_file) self.assertEqual("Incorrect password", str(ctx.exception))
def test_copy(self, _, password): self.assertFalse(os.path.exists(self.remote_file)) sshcp = Sshcp(host=self.host, port=self.port, user=self.user, password=password) sshcp.copy(local_path=self.local_file, remote_path=self.remote_file) self.assertTrue(filecmp.cmp(self.local_file, self.remote_file))
def test_copy_error_bad_host(self, _, password): sshcp = Sshcp(host="badhost", port=self.port, user=self.user, password=password) with self.assertRaises(SshcpError) as ctx: sshcp.copy(local_path=self.local_file, remote_path=self.remote_file) self.assertTrue("Connection refused by server" in str(ctx.exception))
def __init__(self, remote_address: str, remote_username: str, remote_password: Optional[str], remote_port: int, remote_path: str, file_name: str): super().__init__(name=self.__class__.__name__) self.__remote_path = remote_path self.__file_name = file_name self.__ssh = Sshcp(host=remote_address, port=remote_port, user=remote_username, password=remote_password)
def test_copy_error_missing_remote_dir(self, _, password): remote_file = os.path.join(self.remote_dir, "nodir", "file2.txt") self.assertFalse(os.path.exists(remote_file)) sshcp = Sshcp(host=self.host, port=self.port, user=self.user, password=password) with self.assertRaises(SshcpError) as ctx: sshcp.copy(local_path=self.local_file, remote_path=remote_file) self.assertTrue("No such file or directory" in str(ctx.exception))
def __init__(self, remote_address: str, remote_username: str, remote_password: Optional[str], remote_port: int, remote_path_to_scan: str, local_path_to_scan_script: str, remote_path_to_scan_script: str): self.logger = logging.getLogger("RemoteScanner") self.__remote_path_to_scan = remote_path_to_scan self.__local_path_to_scan_script = local_path_to_scan_script self.__remote_path_to_scan_script = remote_path_to_scan_script self.__ssh = Sshcp(host=remote_address, port=remote_port, user=remote_username, password=remote_password) self.__first_run = True
def __init__(self, remote_address: str, remote_username: str, remote_password: Optional[str], remote_port: int, remote_path_to_scan: str, local_path_to_scan_script: str, remote_path_to_scan_script: str): self.logger = logging.getLogger("RemoteScanner") self.__remote_path_to_scan = remote_path_to_scan self.__local_path_to_scan_script = local_path_to_scan_script self.__remote_path_to_scan_script = remote_path_to_scan_script self.__ssh = Sshcp(host=remote_address, port=remote_port, user=remote_username, password=remote_password) self.__first_run = True # Append scan script name to remote path if not there already script_name = os.path.basename(self.__local_path_to_scan_script) if os.path.basename(self.__remote_path_to_scan_script) != script_name: self.__remote_path_to_scan_script = os.path.join( self.__remote_path_to_scan_script, script_name)
def test_shell_with_escape_characters(self, _, password): sshcp = Sshcp(host=self.host, port=self.port, user=self.user, password=password) # single quotes _dir = os.path.join(self.remote_dir, "a a") out = sshcp.shell("mkdir '{}' && cd '{}' && pwd".format(_dir, _dir)) out_str = out.decode().strip() self.assertEqual(_dir, out_str) # double quotes _dir = os.path.join(self.remote_dir, "a b") out = sshcp.shell('mkdir "{}" && cd "{}" && pwd'.format(_dir, _dir)) out_str = out.decode().strip() self.assertEqual(_dir, out_str) # single and double quotes - error out _dir = os.path.join(self.remote_dir, "a b") with self.assertRaises(ValueError): sshcp.shell('mkdir "{}" && cd \'{}\' && pwd'.format(_dir, _dir))
def test_ctor(self): sshcp = Sshcp(host=self.host, port=self.port) self.assertIsNotNone(sshcp)