-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added public key, agent, password auth, sftp read/write and non-block…
…ing execute and sftp read example scripts. Added example directory readme. Updated utils.wait_socket for updated API. Updated changelog. Added examples readme. Updated compiler flags in setup.py.
- Loading branch information
Showing
16 changed files
with
2,044 additions
and
1,086 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
Examples | ||
========== | ||
|
||
In this directory can be found example scripts using ``ssh2-python`` for various operations. | ||
|
||
To try them out, install the library and run the scripts like so: | ||
|
||
Pkey from file | ||
--------------- | ||
|
||
.. code-block:: shell | ||
python examples/publickey_fromfile.py ~/.ssh/id_rsa 'echo me' | ||
me | ||
Non-blocking execute | ||
---------------------- | ||
|
||
.. code-block:: shell | ||
python examples/nonblocking_execute.py 'echo me' | ||
Would block, waiting for socket to be ready | ||
Would block, waiting for socket to be ready | ||
Waiting for command to finish | ||
Waiting for command to finish | ||
me | ||
SFTP write | ||
----------- | ||
|
||
.. code-block:: shell | ||
python examples/sftp_write.py ~/<my source file> ~/<my dest file> | ||
Starting copy of local file <source file> to remote localhost:<dest file> | ||
Finished writing remote file in 0:00:00.006304 | ||
Do *not* use the same filename for source and destination when connecting to localhost if you want to keep your file intact. | ||
|
||
SFTP read | ||
----------- | ||
|
||
.. code-block:: shell | ||
python examples/sftp_read.py ~/<remote file> | ||
Starting read for remote file <remote file> | ||
Finished file read in 0:00:00.045763 | ||
Non-blocking SFTP read | ||
----------------------- | ||
|
||
Note there is no error checking and file is assumed to exist. The script will hang if it does not. | ||
|
||
.. code-block:: shell | ||
python examples/nonblocking_sftp_read.py <remote file> | ||
Would block on sftp init, waiting for socket to be ready | ||
<..> | ||
Would block on sftp init, waiting for socket to be ready | ||
Starting read for remote file <remote file> | ||
Would block on handle open | ||
Would block on read, waiting.. | ||
Finished file read in 0:00:00.056730 | ||
Password authentication | ||
------------------------- | ||
|
||
Authentication with wrong password raises ``AuthenticationError`` exception. | ||
|
||
.. code-block:: shell | ||
python examples/password_auth.py 'asdfadf' 'echo me' | ||
Traceback (most recent call last): | ||
File "examples/password_auth.py", line 45, in <module> | ||
main() | ||
File "examples/password_auth.py", line 35, in main | ||
s.userauth_password(args.user, args.password) | ||
File "ssh2/session.pyx", line 250, in ssh2.session.Session.userauth_password | ||
raise AuthenticationError( | ||
ssh2.exceptions.AuthenticationError: ('Error authenticating user %s with password', '<user>') | ||
SSH Agent authentication | ||
-------------------------- | ||
.. code-block:: shell | ||
python examples/agent_auth.py 'echo me' | ||
me |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Example script for authentication with SSH Agent""" | ||
|
||
from __future__ import print_function | ||
|
||
import argparse | ||
import socket | ||
import os | ||
import pwd | ||
|
||
from ssh2.session import Session | ||
|
||
|
||
USERNAME = pwd.getpwuid(os.geteuid()).pw_name | ||
|
||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument('cmd', help="Command to run") | ||
parser.add_argument('--host', dest='host', | ||
default='localhost', | ||
help='Host to connect to') | ||
parser.add_argument('--port', dest='port', default=22, | ||
help="Port to connect on", type=int) | ||
parser.add_argument('-u', dest='user', default=USERNAME, | ||
help="User name to authenticate as") | ||
|
||
|
||
def main(): | ||
args = parser.parse_args() | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.connect((args.host, args.port)) | ||
s = Session() | ||
s.handshake(sock) | ||
s.agent_auth(args.user) | ||
chan = s.open_session() | ||
chan.execute(args.cmd) | ||
size, data = chan.read() | ||
while size > 0: | ||
print(data) | ||
size, data = chan.read() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"""Example script for non-blocking execute. | ||
Note that `ssh2.utils.wait_socket` is not a co-operative routine and will block | ||
the main thread for up to <timeout> (default 1sec). Use for testing purposes | ||
only.""" | ||
|
||
from __future__ import print_function | ||
|
||
import argparse | ||
import socket | ||
import os | ||
import pwd | ||
from select import select | ||
|
||
from ssh2.session import Session | ||
# from ssh2.utils import wait_socket | ||
from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN | ||
from ssh2.session import LIBSSH2_SESSION_BLOCK_INBOUND, \ | ||
LIBSSH2_SESSION_BLOCK_OUTBOUND | ||
|
||
|
||
USERNAME = pwd.getpwuid(os.geteuid()).pw_name | ||
|
||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument('cmd', help="Command to run") | ||
parser.add_argument('--host', dest='host', | ||
default='localhost', | ||
help='Host to connect to') | ||
parser.add_argument('--port', dest='port', default=22, | ||
help="Port to connect on", type=int) | ||
parser.add_argument('-u', dest='user', default=USERNAME, | ||
help="User name to authenticate as") | ||
|
||
|
||
def wait_socket(_socket, session, timeout=1): | ||
"""Helper function for testing non-blocking mode. | ||
This function blocks the calling thread for <timeout> seconds - | ||
to be used only for testing purposes. | ||
Also available at `ssh2.utils.wait_socket` | ||
""" | ||
directions = session.block_directions() | ||
if directions == 0: | ||
return 0 | ||
readfds = [_socket] \ | ||
if (directions & LIBSSH2_SESSION_BLOCK_INBOUND) else () | ||
writefds = [_socket] \ | ||
if (directions & LIBSSH2_SESSION_BLOCK_OUTBOUND) else () | ||
return select(readfds, writefds, (), timeout) | ||
|
||
|
||
def main(): | ||
args = parser.parse_args() | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.connect((args.host, args.port)) | ||
s = Session() | ||
s.handshake(sock) | ||
# Agent connections cannot be used as non-blocking | ||
s.agent_auth(args.user) | ||
# Now we can set non-blocking mode | ||
s.set_blocking(False) | ||
chan = s.open_session() | ||
while chan is None: | ||
print("Would block on session open, waiting for socket to be ready") | ||
wait_socket(sock, s) | ||
chan = s.open_session() | ||
while chan.execute(args.cmd) == LIBSSH2_ERROR_EAGAIN: | ||
print("Would block on channel execute, waiting for socket to be ready") | ||
wait_socket(sock, s) | ||
while chan.wait_eof() == LIBSSH2_ERROR_EAGAIN: | ||
print("Waiting for command to finish") | ||
wait_socket(sock, s) | ||
size, data = chan.read() | ||
while size == LIBSSH2_ERROR_EAGAIN: | ||
print("Waiting to read data from channel") | ||
wait_socket(sock, s) | ||
size, data = chan.read() | ||
while size > 0: | ||
print(data) | ||
size, data = chan.read() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.