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:
python examples/publickey_fromfile.py ~/.ssh/id_rsa 'echo me'
mepython 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
mepython 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.006304Do not use the same filename for source and destination when connecting to localhost if you want to keep your file intact.
python examples/sftp_read.py ~/<remote file>
Starting read for remote file <remote file>
Finished file read in 0:00:00.045763Note there is no error checking and file is assumed to exist. The script will hang if it does not.
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.056730Authentication with wrong password raises AuthenticationError exception.
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>')python examples/agent_auth.py 'echo me'
me