1#!/usr/bin/env python 2 3# Copyright (C) 2003-2007 Robey Pointer <robeypointer@gmail.com> 4# 5# This file is part of paramiko. 6# 7# Paramiko is free software; you can redistribute it and/or modify it under the 8# terms of the GNU Lesser General Public License as published by the Free 9# Software Foundation; either version 2.1 of the License, or (at your option) 10# any later version. 11# 12# Paramiko is distributed in the hope that it will be useful, but WITHOUT ANY 13# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 14# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 15# details. 16# 17# You should have received a copy of the GNU Lesser General Public License 18# along with Paramiko; if not, write to the Free Software Foundation, Inc., 19# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 20 21# based on code provided by raymond mosteller (thanks!) 22 23import base64 24import getpass 25import os 26import socket 27import sys 28import traceback 29 30import paramiko 31from paramiko.py3compat import input 32 33 34# setup logging 35paramiko.util.log_to_file("demo_sftp.log") 36 37# Paramiko client configuration 38UseGSSAPI = True # enable GSS-API / SSPI authentication 39DoGSSAPIKeyExchange = True 40Port = 22 41 42# get hostname 43username = "" 44if len(sys.argv) > 1: 45 hostname = sys.argv[1] 46 if hostname.find("@") >= 0: 47 username, hostname = hostname.split("@") 48else: 49 hostname = input("Hostname: ") 50if len(hostname) == 0: 51 print("*** Hostname required.") 52 sys.exit(1) 53 54if hostname.find(":") >= 0: 55 hostname, portstr = hostname.split(":") 56 Port = int(portstr) 57 58 59# get username 60if username == "": 61 default_username = getpass.getuser() 62 username = input("Username [%s]: " % default_username) 63 if len(username) == 0: 64 username = default_username 65if not UseGSSAPI: 66 password = getpass.getpass("Password for %s@%s: " % (username, hostname)) 67else: 68 password = None 69 70 71# get host key, if we know one 72hostkeytype = None 73hostkey = None 74try: 75 host_keys = paramiko.util.load_host_keys( 76 os.path.expanduser("~/.ssh/known_hosts") 77 ) 78except IOError: 79 try: 80 # try ~/ssh/ too, because windows can't have a folder named ~/.ssh/ 81 host_keys = paramiko.util.load_host_keys( 82 os.path.expanduser("~/ssh/known_hosts") 83 ) 84 except IOError: 85 print("*** Unable to open host keys file") 86 host_keys = {} 87 88if hostname in host_keys: 89 hostkeytype = host_keys[hostname].keys()[0] 90 hostkey = host_keys[hostname][hostkeytype] 91 print("Using host key of type %s" % hostkeytype) 92 93 94# now, connect and use paramiko Transport to negotiate SSH2 across the connection 95try: 96 t = paramiko.Transport((hostname, Port)) 97 t.connect( 98 hostkey, 99 username, 100 password, 101 gss_host=socket.getfqdn(hostname), 102 gss_auth=UseGSSAPI, 103 gss_kex=DoGSSAPIKeyExchange, 104 ) 105 sftp = paramiko.SFTPClient.from_transport(t) 106 107 # dirlist on remote host 108 dirlist = sftp.listdir(".") 109 print("Dirlist: %s" % dirlist) 110 111 # copy this demo onto the server 112 try: 113 sftp.mkdir("demo_sftp_folder") 114 except IOError: 115 print("(assuming demo_sftp_folder/ already exists)") 116 with sftp.open("demo_sftp_folder/README", "w") as f: 117 f.write("This was created by demo_sftp.py.\n") 118 with open("demo_sftp.py", "r") as f: 119 data = f.read() 120 sftp.open("demo_sftp_folder/demo_sftp.py", "w").write(data) 121 print("created demo_sftp_folder/ on the server") 122 123 # copy the README back here 124 with sftp.open("demo_sftp_folder/README", "r") as f: 125 data = f.read() 126 with open("README_demo_sftp", "w") as f: 127 f.write(data) 128 print("copied README back here") 129 130 # BETTER: use the get() and put() methods 131 sftp.put("demo_sftp.py", "demo_sftp_folder/demo_sftp.py") 132 sftp.get("demo_sftp_folder/README", "README_demo_sftp") 133 134 t.close() 135 136except Exception as e: 137 print("*** Caught exception: %s: %s" % (e.__class__, e)) 138 traceback.print_exc() 139 try: 140 t.close() 141 except: 142 pass 143 sys.exit(1) 144