Esempio n. 1
0
 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))
Esempio n. 2
0
 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))
Esempio n. 3
0
 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))
Esempio n. 4
0
 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))
Esempio n. 5
0
 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)
Esempio n. 6
0
 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))
Esempio n. 7
0
    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))
Esempio n. 8
0
 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))
Esempio n. 9
0
 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)
Esempio n. 10
0
    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))
Esempio n. 11
0
 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
Esempio n. 12
0
    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)
Esempio n. 13
0
    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))
Esempio n. 14
0
 def test_ctor(self):
     sshcp = Sshcp(host=self.host, port=self.port)
     self.assertIsNotNone(sshcp)