Exemplo n.º 1
0
def ssh_pki_import_privkey_file(file_path, pass_phrase=None):
    assert issubclass(file_path.__class__, str)

    logging.debug("Importing private-key from [%s]." % (file_path))

    key = c_ssh_key()
# TODO: This needs to be freed. Use our key class.

    file_path = bytify(file_path)
    if pass_phrase is not None:
        assert issubclass(pass_phrase.__class__, str)
        pass_phrase = bytify(pass_phrase)
    
    result = c_ssh_pki_import_privkey_file(c_char_p(file_path), 
                                           c_char_p(pass_phrase), 
                                           None, 
                                           None, 
                                           byref(key))

    if result == SSH_EOF:
        raise SshError("Key file [%s] does not exist or could not be read." % 
                       (file_path))
    elif result != SSH_OK:
        raise SshError("Could not import key.")

    return key
Exemplo n.º 2
0
def _sftp_symlink(sftp_session_int, to, from_):
    result = c_sftp_symlink(sftp_session_int, c_char_p(bytify(to)),
                            c_char_p(bytify(from_)))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Symlink of [%s] to target [%s] failed: %s" %
                            (from_, to, sftp_get_error_string(type_)))
        else:
            raise SftpError("Symlink of [%s] to target [%s] failed. There was "
                            "an unspecified error." % (from_, to))
Exemplo n.º 3
0
def _sftp_symlink(sftp_session_int, to, from_):
    result = c_sftp_symlink(sftp_session_int, 
                            c_char_p(bytify(to)), 
                            c_char_p(bytify(from_)))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Symlink of [%s] to target [%s] failed: %s" % 
                            (from_, to, sftp_get_error_string(type_)))
        else:
            raise SftpError("Symlink of [%s] to target [%s] failed. There was "
                            "an unspecified error." % (from_, to))
Exemplo n.º 4
0
def _ssh_userauth_password(ssh_session, username, password):
    if username is not None:
        assert issubclass(username.__class__, str)

    assert issubclass(password.__class__, str)

    logging.debug("Authenticating with a password for user [%s]." % (username))

    result = c_ssh_userauth_password(c_void_p(ssh_session), \
                                     c_char_p(bytify(username)), \
                                     c_char_p(bytify(password)))

    _check_auth_response(result)
Exemplo n.º 5
0
def _ssh_userauth_password(ssh_session, username, password):
    if username is not None:
        assert issubclass(username.__class__, str)
    
    assert issubclass(password.__class__, str)

    logging.debug("Authenticating with a password for user [%s]." % (username))
    
    result = c_ssh_userauth_password(c_void_p(ssh_session), \
                                     c_char_p(bytify(username)), \
                                     c_char_p(bytify(password)))

    _check_auth_response(result)
Exemplo n.º 6
0
def _sftp_rename(sftp_session_int, filepath_old, filepath_new):
    result = c_sftp_rename(sftp_session_int, c_char_p(bytify(filepath_old)),
                           c_char_p(bytify(filepath_new)))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError(
                "Rename of [%s] to [%s] failed: %s" %
                (filepath_old, filepath_new, sftp_get_error_string(type_)))
        else:
            raise SftpError("Rename of [%s] to [%s] failed. There was an "
                            "unspecified error." %
                            (filepath_old, filespace_new))
Exemplo n.º 7
0
def _ssh_channel_request_env(ssh_channel_int, name, value):
    logging.debug("Setting remote environment variable [%s] to [%s]." %
                  (name, value))

    # TODO: We haven't been able to get this to work. Reported bug #125.
    result = c_ssh_channel_request_env(ssh_channel_int, c_char_p(bytify(name)),
                                       c_char_p(bytify(value)))

    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        ssh_session_int = _ssh_channel_get_session(ssh_channel_int)
        error = ssh_get_error(ssh_session_int)

        raise SshError("Request-env failed: %s" % (error))
Exemplo n.º 8
0
def _ssh_channel_request_x11(ssh_channel_int, screen_number=0, 
                             single_connection=False, protocol=None, 
                             cookie=None):
    result = c_ssh_channel_request_x11(ssh_channel_int, int(single_connection), 
                                       c_char_p(bytify(protocol)), \
                                       c_char_p(bytify(cookie)), 
                                       screen_number)

    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        ssh_session_int = _ssh_channel_get_session(ssh_channel_int)
        error = ssh_get_error(ssh_session_int)

        raise SshError("Channel request-X11 failed: %s" % (error))
Exemplo n.º 9
0
    def do_command(self,
                   command,
                   block_cb=None,
                   add_nl=True,
                   drop_last_line=True,
                   drop_first_line=True):
        self.__log.debug("Sending shell command: %s" % (command.rstrip()))

        if add_nl is True:
            command += '\n'

        self.__sc.write(bytify(command))

        if block_cb is not None:
            self.__wait_on_output(block_cb)
        else:
            received = bytearray()

            def data_cb(buffer_):
                received.extend(bytify(buffer_))

            self.__wait_on_output_all(data_cb)

            if drop_first_line is True:
                received = received[received.index(b'\n') + 1:]

            # In all likelihood, the last line is probably the prompt.
            if drop_last_line is True:
                received = received[:received.rindex(b'\n')]

            return bytes(received)
Exemplo n.º 10
0
    def do_command(self, command, block_cb=None, add_nl=True, 
                   drop_last_line=True, drop_first_line=True):
        self.__log.debug("Sending shell command: %s" % (command.rstrip()))

        if add_nl is True:
            command += '\n'

        self.__sc.write(bytify(command))
        
        if block_cb is not None:
            self.__wait_on_output(block_cb)
        else:
            received = bytearray()
            def data_cb(buffer_):
                received.extend(bytify(buffer_))
            
            self.__wait_on_output_all(data_cb)

            if drop_first_line is True:
                received = received[received.index(b'\n') + 1:]

            # In all likelihood, the last line is probably the prompt.
            if drop_last_line is True:
                received = received[:received.rindex(b'\n')]

            return bytes(received)
Exemplo n.º 11
0
def _ssh_channel_request_env(ssh_channel_int, name, value):
    logging.debug("Setting remote environment variable [%s] to [%s]." % 
                  (name, value))

# TODO: We haven't been able to get this to work. Reported bug #125.
    result = c_ssh_channel_request_env(ssh_channel_int, 
                                       c_char_p(bytify(name)), 
                                       c_char_p(bytify(value)))

    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        ssh_session_int = _ssh_channel_get_session(ssh_channel_int)
        error = ssh_get_error(ssh_session_int)

        raise SshError("Request-env failed: %s" % (error))
Exemplo n.º 12
0
def _sftp_rename(sftp_session_int, filepath_old, filepath_new):
    result = c_sftp_rename(sftp_session_int, 
                           c_char_p(bytify(filepath_old)), 
                           c_char_p(bytify(filepath_new)))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Rename of [%s] to [%s] failed: %s" % 
                            (filepath_old, 
                             filepath_new, 
                             sftp_get_error_string(type_)))
        else:
            raise SftpError("Rename of [%s] to [%s] failed. There was an "
                            "unspecified error." %
                            (filepath_old, filespace_new))
Exemplo n.º 13
0
def _ssh_channel_request_x11(ssh_channel_int,
                             screen_number=0,
                             single_connection=False,
                             protocol=None,
                             cookie=None):
    result = c_ssh_channel_request_x11(ssh_channel_int, int(single_connection),
                                       c_char_p(bytify(protocol)), \
                                       c_char_p(bytify(cookie)),
                                       screen_number)

    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        ssh_session_int = _ssh_channel_get_session(ssh_channel_int)
        error = ssh_get_error(ssh_session_int)

        raise SshError("Channel request-X11 failed: %s" % (error))
Exemplo n.º 14
0
def _ssh_channel_open_forward(ssh_channel_int, host_remote, port_remote, 
                              host_source, port_local):

    logging.debug("Requesting forward on channel.")

    result = c_ssh_channel_open_forward(ssh_channel_int, 
                                        c_char_p(bytify(host_remote)), 
                                        c_int(port_remote), 
                                        c_char_p(bytify(host_source)), 
                                        c_int(port_local))

    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        ssh_session_int = _ssh_channel_get_session(ssh_channel_int)
        error = ssh_get_error(ssh_session_int)

        raise SshError("Forward failed: %s" % (error))
Exemplo n.º 15
0
def _ssh_channel_open_forward(ssh_channel_int, host_remote, port_remote,
                              host_source, port_local):

    logging.debug("Requesting forward on channel.")

    result = c_ssh_channel_open_forward(ssh_channel_int,
                                        c_char_p(bytify(host_remote)),
                                        c_int(port_remote),
                                        c_char_p(bytify(host_source)),
                                        c_int(port_local))

    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        ssh_session_int = _ssh_channel_get_session(ssh_channel_int)
        error = ssh_get_error(ssh_session_int)

        raise SshError("Forward failed: %s" % (error))
Exemplo n.º 16
0
def _sftp_chmod(sftp_session_int, file_path, mode):
    result = c_sftp_chmod(sftp_session_int, c_char_p(bytify(file_path)),
                          c_int(mode))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("CHMOD of [%s] for mode [%o] failed: %s" %
                            (file_path, mode, sftp_get_error_string(type_)))
        else:
            raise SftpError("CHMOD of [%s] for mode [%o] failed. There was " %
                            "an unspecified error." % (file_path, mode))
Exemplo n.º 17
0
def _sftp_stat(sftp_session_int, file_path):
    attr = c_sftp_stat(sftp_session_int, c_char_p(bytify(file_path)))
    if attr is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Could not acquire attributes for STAT of [%s]: "
                            "%s" % (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Could not acquire attributes for STAT of [%s]. "
                            "There was an unspecified error." % (file_path))

    return EntryAttributes(attr)
Exemplo n.º 18
0
def _ssh_options_set_string(ssh_session, type_, value):
    assert issubclass(value.__class__, str)

    value_charp = c_char_p(bytify(value))

    result = c_ssh_options_set(c_void_p(ssh_session), c_int(type_),
                               cast(value_charp, c_void_p))

    if result < 0:
        error = ssh_get_error(ssh_session)
        raise SshError("Could not set STRING option (%d) to [%s]: %s" %
                       (type_, value, error))
Exemplo n.º 19
0
def _sftp_stat(sftp_session_int, file_path):
    attr = c_sftp_stat(sftp_session_int, c_char_p(bytify(file_path)))
    if attr is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Could not acquire attributes for STAT of [%s]: "
                            "%s" % (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Could not acquire attributes for STAT of [%s]. "
                            "There was an unspecified error." % (file_path))

    return EntryAttributes(attr)
Exemplo n.º 20
0
def _sftp_setstat(sftp_session_int, file_path, entry_attributes):
    result = c_sftp_setstat(sftp_session_int, c_char_p(bytify(file_path)),
                            entry_attributes.raw)

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Set-stat on [%s] failed: %s" %
                            (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Set-stat on [%s] failed. There was an "
                            "unspecified error." % (file_path))
Exemplo n.º 21
0
def _sftp_rmdir(sftp_session_int, path):
    logging.debug("Deleting directory: %s" % (path))

    result = c_sftp_rmdir(sftp_session_int, c_char_p(bytify(path)))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("RMDIR of [%s] failed: %s" % 
                            (path, sftp_get_error_string(type_)))
        else:
            raise SftpError("RMDIR of [%s] failed. There was an unspecified "
                            "error." % (path))
Exemplo n.º 22
0
def _sftp_chmod(sftp_session_int, file_path, mode):
    result = c_sftp_chmod(sftp_session_int, 
                          c_char_p(bytify(file_path)), 
                          c_int(mode))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("CHMOD of [%s] for mode [%o] failed: %s" %
                            (file_path, mode, sftp_get_error_string(type_)))
        else:
            raise SftpError("CHMOD of [%s] for mode [%o] failed. There was " %
                            "an unspecified error." % (file_path, mode))
Exemplo n.º 23
0
def _ssh_channel_request_exec(ssh_channel_int, cmd):
    logging.debug("Requesting channel exec.")

    result = c_ssh_channel_request_exec(ssh_channel_int, c_char_p(bytify(cmd)))
    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        ssh_session_int = _ssh_channel_get_session(ssh_channel_int)
        error = ssh_get_error(ssh_session_int)

        raise SshError("Could not execute shell request on channel: %s" %
                       (error))
    logging.debug("Channel-exec successful.")
Exemplo n.º 24
0
def _sftp_lstat(sftp_session_int, file_path):
    attr = c_sftp_lstat(sftp_session_int, c_char_p(bytify(file_path)))

    if attr is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("LSTAT of [%s] failed: %s" %
                            (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("LSTAT of [%s] failed. There was an unspecified "
                            "error." % (file_path))

    return EntryAttributes(attr)
Exemplo n.º 25
0
def _sftp_readlink(sftp_session_int, file_path):
    target = c_sftp_readlink(sftp_session_int, c_char_p(bytify(file_path)))

    if target is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Read of link [%s] failed: %s" %
                            (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Read of link [%s] failed. There was an "
                            "unspecified error." % (file_path))

    return target
Exemplo n.º 26
0
def _ssh_options_set_string(ssh_session, type_, value):
    assert issubclass(value.__class__, str)

    value_charp = c_char_p(bytify(value))

    result = c_ssh_options_set(c_void_p(ssh_session), 
                               c_int(type_), 
                               cast(value_charp, c_void_p))

    if result < 0:
        error = ssh_get_error(ssh_session)
        raise SshError("Could not set STRING option (%d) to [%s]: %s" % 
                       (type_, value, error))
Exemplo n.º 27
0
def _sftp_setstat(sftp_session_int, file_path, entry_attributes):
    result = c_sftp_setstat(sftp_session_int,
                            c_char_p(bytify(file_path)),
                            entry_attributes.raw)

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Set-stat on [%s] failed: %s" % 
                            (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Set-stat on [%s] failed. There was an "
                            "unspecified error." % (file_path))
Exemplo n.º 28
0
def _sftp_readlink(sftp_session_int, file_path):
    target = c_sftp_readlink(sftp_session_int, c_char_p(bytify(file_path)))

    if target is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Read of link [%s] failed: %s" % 
                            (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Read of link [%s] failed. There was an "
                            "unspecified error." % (file_path))

    return target
Exemplo n.º 29
0
def _sftp_rmdir(sftp_session_int, path):
    logging.debug("Deleting directory: %s" % (path))

    result = c_sftp_rmdir(sftp_session_int, c_char_p(bytify(path)))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("RMDIR of [%s] failed: %s" %
                            (path, sftp_get_error_string(type_)))
        else:
            raise SftpError("RMDIR of [%s] failed. There was an unspecified "
                            "error." % (path))
Exemplo n.º 30
0
def _sftp_lstat(sftp_session_int, file_path):
    attr = c_sftp_lstat(sftp_session_int, c_char_p(bytify(file_path)))

    if attr is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("LSTAT of [%s] failed: %s" % 
                            (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("LSTAT of [%s] failed. There was an unspecified "
                            "error." % (file_path))

    return EntryAttributes(attr)
Exemplo n.º 31
0
def _sftp_unlink(sftp_session_int, file_path):
    logging.debug("Deleting file: %s" % (file_path))

    result = c_sftp_unlink(sftp_session_int, c_char_p(bytify(file_path)))
    # TODO: This seems to be a large integer. What is it?
    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Unlink of [%s] failed: %s" %
                            (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Unlink of [%s] failed. There was an unspecified "
                            "error." % (file_path))
Exemplo n.º 32
0
def _sftp_unlink(sftp_session_int, file_path):
    logging.debug("Deleting file: %s" % (file_path))

    result = c_sftp_unlink(sftp_session_int, 
                           c_char_p(bytify(file_path)))
# TODO: This seems to be a large integer. What is it?
    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Unlink of [%s] failed: %s" % 
                            (file_path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Unlink of [%s] failed. There was an unspecified "
                            "error." % (file_path))
Exemplo n.º 33
0
def _sftp_chown(sftp_session_int, file_path, uid, gid):
    result = c_sftp_chown(sftp_session_int, c_char_p(bytify(file_path)),
                          c_int(uid), c_int(gid))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError(
                "CHOWN of [%s] for UID (%d) and GID (%d) failed: "
                "%s" % (file_path, uid, gid, sftp_get_error_string(type_)))
        else:
            raise SftpError("CHOWN of [%s] for UID (%d) and GID (%d) failed. "
                            "There was an unspecified error." %
                            (file_path, mode))
Exemplo n.º 34
0
def _ssh_channel_request_exec(ssh_channel_int, cmd):
    logging.debug("Requesting channel exec.")

    result = c_ssh_channel_request_exec(ssh_channel_int, 
                                        c_char_p(bytify(cmd)))
    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        ssh_session_int = _ssh_channel_get_session(ssh_channel_int)
        error = ssh_get_error(ssh_session_int)

        raise SshError("Could not execute shell request on channel: %s" % 
                       (error))
    logging.debug("Channel-exec successful.")
Exemplo n.º 35
0
def _sftp_opendir(sftp_session_int, path):
    logging.debug("Opening directory: %s" % (path))

    sd = c_sftp_opendir(sftp_session_int, bytify(path))
    if sd is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Could not open directory [%s]: %s" % 
                            (path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Could not open directory [%s]. There was an "
                            "unspecified error." % (path))

    logging.debug("Directory resource ID is (%d)." % (sd))
    return sd
Exemplo n.º 36
0
def ssh_pki_import_privkey_file(file_path, pass_phrase=None):
    assert issubclass(file_path.__class__, str)

    logging.debug("Importing private-key from [%s]." % (file_path))

    key = c_ssh_key()
    # TODO: This needs to be freed. Use our key class.

    file_path = bytify(file_path)
    if pass_phrase is not None:
        assert issubclass(pass_phrase.__class__, str)
        pass_phrase = bytify(pass_phrase)

    result = c_ssh_pki_import_privkey_file(c_char_p(file_path),
                                           c_char_p(pass_phrase), None, None,
                                           byref(key))

    if result == SSH_EOF:
        raise SshError("Key file [%s] does not exist or could not be read." %
                       (file_path))
    elif result != SSH_OK:
        raise SshError("Could not import key.")

    return key
Exemplo n.º 37
0
def _sftp_opendir(sftp_session_int, path):
    logging.debug("Opening directory: %s" % (path))

    sd = c_sftp_opendir(sftp_session_int, bytify(path))
    if sd is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Could not open directory [%s]: %s" %
                            (path, sftp_get_error_string(type_)))
        else:
            raise SftpError("Could not open directory [%s]. There was an "
                            "unspecified error." % (path))

    logging.debug("Directory resource ID is (%d)." % (sd))
    return sd
Exemplo n.º 38
0
def _sftp_open(sftp_session_int, filepath, access_type, mode):
    logging.debug("Opening file: %s" % (filepath))

    sf = c_sftp_open(sftp_session_int, bytify(filepath), access_type, mode)

    if sf is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Could not open file [%s]: %s" %
                            (filepath, sftp_get_error_string(type_)))
        else:
            raise SftpError("Could not open file [%s]. There was an "
                            "unspecified error." % (filepath))

    logging.debug("File [%s] opened as [%s]." % (filepath, sf))
    return sf
Exemplo n.º 39
0
def _sftp_chown(sftp_session_int, file_path, uid, gid):
    result = c_sftp_chown(sftp_session_int, 
                          c_char_p(bytify(file_path)), 
                          c_int(uid), 
                          c_int(gid))

    if result < 0:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("CHOWN of [%s] for UID (%d) and GID (%d) failed: "
                            "%s" % 
                            (file_path, uid, gid, 
                             sftp_get_error_string(type_)))
        else:
            raise SftpError("CHOWN of [%s] for UID (%d) and GID (%d) failed. "
                            "There was an unspecified error." % 
                            (file_path, mode))
Exemplo n.º 40
0
def _ssh_forward_listen(ssh_session, address, port):
    if address is not None:
        assert issubclass(address.__class__, str)
        address = bytify(address)

    bound_port = c_int()
    # BUG: Currently always returns SSH_AGAIN in 0.6.0 . Registered as bug #126.
    result = c_ssh_forward_listen(ssh_session, address, port,
                                  byref(bound_port))

    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        error = ssh_get_error(ssh_session)
        raise SshError("Forward-listen failed: %s" % (error))

    return bound_port.value
Exemplo n.º 41
0
def _sftp_utimes(sftp_session_int, file_path, atime_epoch, mtime_epoch):
    atime = CTimeval()
    mtime = CTimeval()

    atime.tv_sec = int(atime_epoch)
    atime.tv_usec = 0

    mtime.tv_sec = int(mtime_epoch)
    mtime.tv_usec = 0

    times = (CTimeval * 2)(atime, mtime)

    result = c_sftp_utimes(sftp_session_int, c_char_p(bytify(file_path)),
                           byref(times))

    if result < 0:
        raise SftpError("Times updated of [%s] failed." % (file_path))
Exemplo n.º 42
0
def _sftp_utimes(sftp_session_int, file_path, atime_epoch, mtime_epoch):
    atime = CTimeval()
    mtime = CTimeval()

    atime.tv_sec = int(atime_epoch)
    atime.tv_usec = 0

    mtime.tv_sec = int(mtime_epoch)
    mtime.tv_usec = 0

    times = (CTimeval * 2)(atime, mtime)

    result = c_sftp_utimes(sftp_session_int, 
                           c_char_p(bytify(file_path)), 
                           byref(times))

    if result < 0:
        raise SftpError("Times updated of [%s] failed." % (file_path))
Exemplo n.º 43
0
def _sftp_open(sftp_session_int, filepath, access_type, mode):
    logging.debug("Opening file: %s" % (filepath))

    sf = c_sftp_open(sftp_session_int, 
                     bytify(filepath), 
                     access_type, 
                     mode)

    if sf is None:
        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("Could not open file [%s]: %s" % 
                            (filepath, sftp_get_error_string(type_)))
        else:
            raise SftpError("Could not open file [%s]. There was an "
                            "unspecified error." % (filepath))

    logging.debug("File [%s] opened as [%s]." % (filepath, sf))
    return sf
Exemplo n.º 44
0
def _sftp_listdir(sftp_session_int, 
                  path, 
                  get_directories=True, 
                  get_files=True):
    logging.debug("Listing directory: %s" % (path))

    with SftpDirectory(sftp_session_int, bytify(path)) as sd_:
        while 1:
            attributes = _sftp_readdir(sftp_session_int, sd_)
            if attributes is None:
                break

            if attributes.is_directory is True and get_directories is True or \
               attributes.is_directory is False and get_files is True:
                yield attributes

        if not _sftp_dir_eof(sd_):
            raise SftpError("We're done iterating the directory, but it's not "
                            "at EOF.")
Exemplo n.º 45
0
def _ssh_forward_listen(ssh_session, address, port):
    if address is not None:
        assert issubclass(address.__class__, str)
        address = bytify(address)

    bound_port = c_int()
# BUG: Currently always returns SSH_AGAIN in 0.6.0 . Registered as bug #126.
    result = c_ssh_forward_listen(ssh_session, 
                                  address, 
                                  port, 
                                  byref(bound_port))

    if result == SSH_AGAIN:
        raise SshNonblockingTryAgainException()
    elif result != SSH_OK:
        error = ssh_get_error(ssh_session)
        raise SshError("Forward-listen failed: %s" % (error))

    return bound_port.value
Exemplo n.º 46
0
def _sftp_listdir(sftp_session_int,
                  path,
                  get_directories=True,
                  get_files=True):
    logging.debug("Listing directory: %s" % (path))

    with SftpDirectory(sftp_session_int, bytify(path)) as sd_:
        while 1:
            attributes = _sftp_readdir(sftp_session_int, sd_)
            if attributes is None:
                break

            if attributes.is_directory is True and get_directories is True or \
               attributes.is_directory is False and get_files is True:
                yield attributes

        if not _sftp_dir_eof(sd_):
            raise SftpError("We're done iterating the directory, but it's not "
                            "at EOF.")
Exemplo n.º 47
0
def _sftp_mkdir(sftp_session_int, path, mode, check_exists_on_fail=True):
    logging.debug("Creating directory: %s" % (path))

    result = c_sftp_mkdir(sftp_session_int, c_char_p(bytify(path)),
                          c_int(mode))

    if result < 0:
        if check_exists_on_fail is not False:
            if _sftp_exists(sftp_session_int, path) is True:
                raise SftpAlreadyExistsError("Path already exists: %s" %
                                             (path))

        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("MKDIR of [%s] for mode [%o] failed: %s" %
                            (path, mode, sftp_get_error_string(type_)))
        else:
            raise SftpError("MKDIR of [%s] for mode [%o] failed. There was " %
                            "an unspecified error." % (path, mode))
Exemplo n.º 48
0
def _sftp_mkdir(sftp_session_int, path, mode, check_exists_on_fail=True):
    logging.debug("Creating directory: %s" % (path))

    result = c_sftp_mkdir(sftp_session_int, 
                          c_char_p(bytify(path)), 
                          c_int(mode))

    if result < 0:
        if check_exists_on_fail is not False:
            if _sftp_exists(sftp_session_int, path) is True:
                raise SftpAlreadyExistsError("Path already exists: %s" % 
                                             (path))

        type_ = sftp_get_error(sftp_session_int)
        if type_ >= 0:
            raise SftpError("MKDIR of [%s] for mode [%o] failed: %s" %
                            (path, mode, sftp_get_error_string(type_)))
        else:
            raise SftpError("MKDIR of [%s] for mode [%o] failed. There was " %
                            "an unspecified error." % (path, mode))
Exemplo n.º 49
0
def _sftp_utimes_dt(sftp_session_int, file_path, atime_dt, mtime_dt):
    _sftp_utimes(sftp_session_int, bytify(file_path),
                 mktime(atime_dt.timetuple()), mktime(mtime_dt.timetuple()))
Exemplo n.º 50
0
def _ssh_print_hexa(title, hash_, hlen):
    assert issubclass(title.__class__, str)

    c_ssh_print_hexa(c_char_p(bytify(title)), hash_, c_int(hlen))
Exemplo n.º 51
0
def _sftp_utimes_dt(sftp_session_int, file_path, atime_dt, mtime_dt):
    _sftp_utimes(sftp_session_int, 
                 bytify(file_path), 
                 mktime(atime_dt.timetuple()), 
                 mktime(mtime_dt.timetuple()))
Exemplo n.º 52
0
 def data_cb(buffer_):
     received.extend(bytify(buffer_))
Exemplo n.º 53
0
def _ssh_print_hexa(title, hash_, hlen):
    assert issubclass(title.__class__, str)

    c_ssh_print_hexa(c_char_p(bytify(title)), hash_, c_int(hlen))
Exemplo n.º 54
0
 def welcome_received_cb(data):
     welcome.extend(bytify(data))
Exemplo n.º 55
0
 def welcome_received_cb(data):
     welcome.extend(bytify(data))
Exemplo n.º 56
0
 def data_cb(buffer_):
     received.extend(bytify(buffer_))