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
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))
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)
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))
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))
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))
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)
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))
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))
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))
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)
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))
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))
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))
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.")
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)
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
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))
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))
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
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
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))
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
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))
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.")
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))
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()))
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))
def data_cb(buffer_): received.extend(bytify(buffer_))
def welcome_received_cb(data): welcome.extend(bytify(data))