Skip to content

Instantly share code, notes, and snippets.

@zillion45
Last active June 4, 2023 15:28
Show Gist options
  • Save zillion45/be2e110587fddb9770fc076325c896f7 to your computer and use it in GitHub Desktop.
Save zillion45/be2e110587fddb9770fc076325c896f7 to your computer and use it in GitHub Desktop.
paramiko sftp example
import getpass
import sys
import paramiko
LOCAL= ''
REMOTE = ''
def copy_file(host, port, username, password, src, dest):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
t = paramiko.Transport((host, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
print "Copying file %s to %s" % (src, dest)
sftp.put(src, dest)
sftp.close()
t.close()
if __name__ == '__main__':
username = raw_input("Enter the username: ")
password = getpass.getpass("Enter the password: ")
if len(sys.argv) > 1:
host = sys.argv[1]
else:
host = raw_input("Enter the hostname: ")
port = 22
src = LOCAL
dest = REMOTE
copy_file(host, port, username, password, src, dest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment