Skip to content

Commit cd75d96

Browse files
author
Pan
committed
Updated session, channel, agent and pkey to accept any string type arguments.
Added scp function signatures to sftp. Updated sftp test to clean up test file.
1 parent 6f9a742 commit cd75d96

File tree

12 files changed

+2439
-1506
lines changed

12 files changed

+2439
-1506
lines changed

embedded_server/openssh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _wait_for_port(self):
4141
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
4242
while sock.connect_ex(('127.0.0.1', self.port)) != 0:
4343
sleep(.1)
44-
sleep(.1)
44+
sleep(.2)
4545
del sock
4646

4747
def stop(self):

ssh2/agent.c

Lines changed: 186 additions & 188 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ssh2/agent.pyx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cimport c_ssh2
1818
from pkey cimport PublicKey, PyPublicKey
1919
from exceptions cimport AgentConnectError, AgentListIdentitiesError, \
2020
AgentGetIdentityError, AgentAuthenticationError
21+
from utils cimport to_bytes
2122

2223

2324
cdef int auth_identity(const char *username,
@@ -64,15 +65,16 @@ cdef class Agent:
6465
clear_agent(self._agent)
6566

6667
def list_identities(self):
67-
"""This method is a no-op - use get_identities to list and retrieve
68+
"""This method is a no-op - use
69+
:py:func:`ssh2.agent.Agent.get_identities` to list and retrieve
6870
identities
6971
"""
7072
pass
7173

72-
def get_identities(self, const char *username):
74+
def get_identities(self):
7375
"""List and get identities from agent
7476
75-
:rtype: list(:py:class:`PublicKey`)
77+
:rtype: list(:py:class:`ssh2.pkey.PublicKey`)
7678
"""
7779
cdef int rc
7880
cdef list identities = []
@@ -91,19 +93,25 @@ cdef class Agent:
9193
prev = identity
9294
return identities
9395

94-
def userauth(self, const char *username,
96+
def userauth(self, username,
9597
PublicKey pkey):
9698
"""Perform user authentication with specific public key
9799
98100
:param username: User name to authenticate as
99101
:type username: str
100102
:param pkey: Public key to authenticate with
101103
:type pkey: py:class:`PublicKey`
104+
105+
:raises: :py:class:`ssh2.exceptions.AgentAuthenticationError` on errors
106+
authenticating.
107+
108+
:rtype: int
102109
"""
103110
cdef int rc
111+
cdef char *_username = to_bytes(username)
104112
with nogil:
105113
rc = c_ssh2.libssh2_agent_userauth(
106-
self._agent, username, pkey._pkey)
114+
self._agent, _username, pkey._pkey)
107115
if rc != 0 and rc != c_ssh2._LIBSSH2_ERROR_EAGAIN:
108116
with gil:
109117
raise AgentAuthenticationError(

ssh2/channel.c

Lines changed: 573 additions & 517 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ssh2/channel.pyx

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ cimport error_codes
2222

2323
from session cimport Session
2424
from exceptions cimport ChannelError
25-
# from utils cimport to_bytes
25+
from utils cimport to_bytes
2626

2727

2828
cdef object PyChannel(c_ssh2.LIBSSH2_CHANNEL *channel, Session session):
@@ -39,6 +39,7 @@ cdef class Channel:
3939

4040
def __dealloc__(self):
4141
with nogil:
42+
c_ssh2.libssh2_channel_close(self._channel)
4243
c_ssh2.libssh2_channel_free(self._channel)
4344

4445
def pty(self, term="vt100"):
@@ -54,27 +55,39 @@ cdef class Channel:
5455
rc)
5556
return rc
5657

57-
def execute(self, const char *command):
58+
def execute(self, command):
59+
"""Execute command.
60+
61+
:param command: Command to execute
62+
:type command: str
63+
64+
:raises: :py:class:`ssh2.exceptions.ChannelError` on errors executing
65+
command
66+
67+
:rtype: int
68+
"""
5869
cdef int rc
70+
cdef char *_command = to_bytes(command)
5971
with nogil:
6072
rc = c_ssh2.libssh2_channel_exec(
61-
self._channel, command)
73+
self._channel, _command)
6274
if rc != 0 and rc != c_ssh2._LIBSSH2_ERROR_EAGAIN:
6375
with gil:
6476
raise ChannelError(
6577
"Error executing command %s - error code %s",
6678
command, rc)
6779
return rc
6880

69-
def subsystem(self, const char *subsystem):
81+
def subsystem(self, subsystem):
7082
"""Request subsystem from channel
7183
7284
:param subsystem: Name of subsystem
7385
:type subsystem: str"""
7486
cdef int rc
87+
cdef char *_subsystem = to_bytes(subsystem)
7588
with nogil:
7689
rc = c_ssh2.libssh2_channel_subsystem(
77-
self._channel, subsystem)
90+
self._channel, _subsystem)
7891
if rc != 0 and rc != c_ssh2._LIBSSH2_ERROR_EAGAIN:
7992
with gil:
8093
raise ChannelError(
@@ -233,11 +246,13 @@ cdef class Channel:
233246
py_langtag = langtag[:py_langlen]
234247
return rc, py_exitsignal, py_errmsg, py_langtag
235248

236-
def setenv(self, const char *varname, const char *value):
249+
def setenv(self, varname, value):
237250
cdef int rc
251+
cdef char *_varname = to_bytes(varname)
252+
cdef char *_value = to_bytes(value)
238253
with nogil:
239254
rc = c_ssh2.libssh2_channel_setenv(
240-
self._channel, varname, value)
255+
self._channel, _varname, _value)
241256
return rc
242257

243258
def window_read_ex(self, unsigned long read_avail,
@@ -267,29 +282,29 @@ cdef class Channel:
267282
rc = c_ssh2.libssh2_channel_window_write(self._channel)
268283
return rc
269284

270-
def write(self, bytes buf):
285+
def write(self, buf):
271286
"""Write buffer to stdin"""
272-
cdef const char *_buf = buf
273-
cdef size_t buflen = len(buf)
287+
cdef const char *_buf = to_bytes(buf)
288+
cdef size_t buflen = len(_buf)
274289
cdef ssize_t rc
275290
with nogil:
276291
rc = c_ssh2.libssh2_channel_write(self._channel, _buf, buflen)
277292
return rc
278293

279-
def write_ex(self, int stream_id, bytes buf):
294+
def write_ex(self, int stream_id, buf):
280295
"""Write buffer to specified stream id"""
281-
cdef const char *_buf = buf
282-
cdef size_t buflen = len(buf)
296+
cdef const char *_buf = to_bytes(buf)
297+
cdef size_t buflen = len(_buf)
283298
cdef ssize_t rc
284299
with nogil:
285300
rc = c_ssh2.libssh2_channel_write_ex(
286301
self._channel, stream_id, _buf, buflen)
287302
return rc
288303

289-
def write_stderr(self, bytes buf):
304+
def write_stderr(self, buf):
290305
"""Write buffer to stderr"""
291-
cdef const char *_buf = buf
292-
cdef size_t buflen = len(buf)
306+
cdef const char *_buf = to_bytes(buf)
307+
cdef size_t buflen = len(_buf)
293308
cdef ssize_t rc
294309
with nogil:
295310
rc = c_ssh2.libssh2_channel_write_stderr(

0 commit comments

Comments
 (0)