summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSebastian LaVine <mail@smlavine.com>2024-06-19 11:05:58 -0400
committerSebastian LaVine <mail@smlavine.com>2024-06-19 11:05:58 -0400
commit5e850d743c4d754f191332f0c4d9d1d9713702e6 (patch)
tree77126303f2c4722aa06d3bfe27aeeef386f8f63b
parent51eb55debf2ebfe56f38378005439a029a48225f (diff)
Fix and improve demo programs
This commit does a few things that make the demos more usable. Imports from paramiko.py3compat have been removed as that module was removed in commit 5c4b311b (2023-01-09), making it possible to run these demos with modern versions of the library. GSS support was made system-conditional in all demo files, as it was previously in demo_simple.py. Unused imports were removed. Docstrings were added to files that did not have them.
-rwxr-xr-xdemos/demo.py22
-rwxr-xr-xdemos/demo_keygen.py7
-rwxr-xr-x[-rw-r--r--]demos/demo_server.py18
-rwxr-xr-x[-rw-r--r--]demos/demo_sftp.py16
-rwxr-xr-x[-rw-r--r--]demos/demo_simple.py12
-rwxr-xr-x[-rw-r--r--]demos/forward.py2
-rw-r--r--demos/interactive.py2
-rwxr-xr-xdemos/rforward.py1
8 files changed, 51 insertions, 29 deletions
diff --git a/demos/demo.py b/demos/demo.py
index 5252db7c..cc45f7a5 100755
--- a/demos/demo.py
+++ b/demos/demo.py
@@ -18,17 +18,17 @@
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+"""
+Demonstrates how to open a shell at an SSH server, including how to support
+SSH agents and different types of keys.
+"""
-import base64
from binascii import hexlify
import getpass
import os
-import select
import socket
import sys
-import time
import traceback
-from paramiko.py3compat import input
import paramiko
@@ -62,7 +62,8 @@ def agent_auth(transport, username):
def manual_auth(username, hostname):
default_auth = "p"
auth = input(
- "Auth by (p)assword, (r)sa key, or (d)ss key? [%s] " % default_auth
+ "Auth by (p)assword, (r)sa key, (d)ss key, or (e)25519 key? [%s] "
+ % default_auth
)
if len(auth) == 0:
auth = default_auth
@@ -89,6 +90,17 @@ def manual_auth(username, hostname):
password = getpass.getpass("DSS key password: ")
key = paramiko.DSSKey.from_private_key_file(path, password)
t.auth_publickey(username, key)
+ elif auth == "e":
+ default_path = os.path.join(os.environ["HOME"], ".ssh", "id_ed25519")
+ path = input("ed25519 key [%s]: " % default_path)
+ if len(path) == 0:
+ path = default_path
+ try:
+ key = paramiko.Ed25519Key.from_private_key_file(path)
+ except paramiko.PasswordRequiredException:
+ password = getpass.getpass("ed25519 key password: ")
+ key = paramiko.Ed25519Key.from_private_key_file(path, password)
+ t.auth_publickey(username, key)
else:
pw = getpass.getpass("Password for %s@%s: " % (username, hostname))
t.auth_password(username, pw)
diff --git a/demos/demo_keygen.py b/demos/demo_keygen.py
index 12637ed0..f9286f6a 100755
--- a/demos/demo_keygen.py
+++ b/demos/demo_keygen.py
@@ -18,6 +18,9 @@
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+"""
+Demonstrates Paramiko's native keygen capabilities.
+"""
import sys
from binascii import hexlify
@@ -26,7 +29,7 @@ from optparse import OptionParser
from paramiko import DSSKey
from paramiko import RSAKey
from paramiko.ssh_exception import SSHException
-from paramiko.py3compat import u
+from paramiko.util import u
usage = """
%prog [-v] [-b bits] -t type [-N new_passphrase] [-f output_keyfile]"""
@@ -167,7 +170,7 @@ if __name__ == "__main__":
"Fingerprint: %d %s %s.pub (%s)"
% (
bits,
- ":".join([hash[i : 2 + i] for i in range(0, len(hash), 2)]),
+ ":".join([hash[i: 2 + i] for i in range(0, len(hash), 2)]),
filename,
ktype.upper(),
)
diff --git a/demos/demo_server.py b/demos/demo_server.py
index 6cb2dc51..9678eb28 100644..100755
--- a/demos/demo_server.py
+++ b/demos/demo_server.py
@@ -18,17 +18,23 @@
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-import base64
+"""
+Demonstrates implementation of an SSHv2 server with the ServerInterface
+and Transport primitives.
+
+Listens on port 2200. Accepts connection from username "robey" and either
+password "foo" or user_rsa_key.
+"""
+
+from base64 import decodebytes
from binascii import hexlify
-import os
import socket
import sys
import threading
import traceback
import paramiko
-from paramiko.py3compat import b, u, decodebytes
-
+from paramiko.util import u
# setup logging
paramiko.util.log_to_file("demo_server.log")
@@ -113,7 +119,9 @@ class Server(paramiko.ServerInterface):
return True
-DoGSSAPIKeyExchange = True
+DoGSSAPIKeyExchange = (
+ paramiko.GSS_AUTH_AVAILABLE
+) # enable "gssapi-kex" key exchange, if supported by your python installation
# now connect
try:
diff --git a/demos/demo_sftp.py b/demos/demo_sftp.py
index dbcb2cb7..8b20bdad 100644..100755
--- a/demos/demo_sftp.py
+++ b/demos/demo_sftp.py
@@ -20,7 +20,10 @@
# based on code provided by raymond mosteller (thanks!)
-import base64
+"""
+Demonstrates basic SFTP transfer across a connection.
+"""
+
import getpass
import os
import socket
@@ -28,15 +31,16 @@ import sys
import traceback
import paramiko
-from paramiko.py3compat import input
-
-
# setup logging
paramiko.util.log_to_file("demo_sftp.log")
# Paramiko client configuration
-UseGSSAPI = True # enable GSS-API / SSPI authentication
-DoGSSAPIKeyExchange = True
+UseGSSAPI = (
+ paramiko.GSS_AUTH_AVAILABLE
+) # enable "gssapi-with-mic" authentication, if supported by your python installation
+DoGSSAPIKeyExchange = (
+ paramiko.GSS_AUTH_AVAILABLE
+) # enable "gssapi-kex" key exchange, if supported by your python installation
Port = 22
# get hostname
diff --git a/demos/demo_simple.py b/demos/demo_simple.py
index bd932c3e..1550e1bc 100644..100755
--- a/demos/demo_simple.py
+++ b/demos/demo_simple.py
@@ -18,14 +18,14 @@
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+"""
+Demonstrates how to open a shell at an SSH server, using basic password
+authentication or GSSAPI.
+"""
-import base64
import getpass
-import os
-import socket
import sys
import traceback
-from paramiko.py3compat import input
import paramiko
@@ -44,8 +44,6 @@ UseGSSAPI = (
DoGSSAPIKeyExchange = (
paramiko.GSS_AUTH_AVAILABLE
) # enable "gssapi-kex" key exchange, if supported by your python installation
-# UseGSSAPI = False
-# DoGSSAPIKeyExchange = False
port = 22
# get hostname
@@ -93,7 +91,7 @@ try:
gss_kex=DoGSSAPIKeyExchange,
)
except Exception:
- # traceback.print_exc()
+ traceback.print_exc()
password = getpass.getpass(
"Password for %s@%s: " % (username, hostname)
)
diff --git a/demos/forward.py b/demos/forward.py
index 869e3906..5b5e26a2 100644..100755
--- a/demos/forward.py
+++ b/demos/forward.py
@@ -27,8 +27,6 @@ connection to a destination reachable from the SSH server machine.
"""
import getpass
-import os
-import socket
import select
try:
diff --git a/demos/interactive.py b/demos/interactive.py
index 16eae0e7..7a3721d3 100644
--- a/demos/interactive.py
+++ b/demos/interactive.py
@@ -19,7 +19,7 @@
import socket
import sys
-from paramiko.py3compat import u
+from paramiko.util import u
# windows does not have termios...
try:
diff --git a/demos/rforward.py b/demos/rforward.py
index 200634ab..8bf37d38 100755
--- a/demos/rforward.py
+++ b/demos/rforward.py
@@ -27,7 +27,6 @@ connection to a destination reachable from the local machine.
"""
import getpass
-import os
import socket
import select
import sys