diff --git a/docs/changelog-fragments/479.feature.rst b/docs/changelog-fragments/479.feature.rst new file mode 100644 index 000000000..41a315471 --- /dev/null +++ b/docs/changelog-fragments/479.feature.rst @@ -0,0 +1 @@ +Implemented reading connection details from a config file -- by :user:`donnerhacke`. diff --git a/src/pylibsshext/includes/libssh.pxd b/src/pylibsshext/includes/libssh.pxd index 8862220d5..bf90aed0c 100644 --- a/src/pylibsshext/includes/libssh.pxd +++ b/src/pylibsshext/includes/libssh.pxd @@ -163,6 +163,7 @@ cdef extern from "libssh/libssh.h" nogil: int ssh_options_get(ssh_session session, ssh_options_e type, char **value) int ssh_options_get_port(ssh_session session, unsigned int * port_target) int ssh_options_set(ssh_session session, ssh_options_e type, const void *value) + int ssh_options_parse_config(ssh_session session, const char *filename) int ssh_get_server_publickey(ssh_session session, ssh_key *key) void ssh_key_free(ssh_key key) diff --git a/src/pylibsshext/session.pyx b/src/pylibsshext/session.pyx index 294827c80..77f899a18 100644 --- a/src/pylibsshext/session.pyx +++ b/src/pylibsshext/session.pyx @@ -243,6 +243,16 @@ cdef class Session(object): if (key in OPTS_MAP or key in OPTS_DIR_MAP) and (kwargs[key] is not None): self.set_ssh_options(key, kwargs[key]) + if 'config_file' in kwargs: + file_name = kwargs['config_file'] + rc = libssh.ssh_options_parse_config(self._libssh_session, file_name.encode()) + if rc != libssh.SSH_OK or self._get_session_error_str() != "": + libssh.ssh_disconnect(self._libssh_session) + raise LibsshSessionException( + "parsing ssh config failed: %s: %s" % + (file_name, self._get_session_error_str()) + ) + if libssh.ssh_connect(self._libssh_session) != libssh.SSH_OK: libssh.ssh_disconnect(self._libssh_session) raise LibsshSessionException("ssh connect failed: %s" % self._get_session_error_str())