forked from brandon-rhodes/fopnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp_get.py
36 lines (30 loc) · 1.42 KB
/
sftp_get.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter16/sftp_get.py
# Fetching files with SFTP
import argparse, functools, paramiko
class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):
def missing_host_key(self, client, hostname, key):
return
def main(hostname, username, filenames):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(AllowAnythingPolicy())
client.connect(hostname, username=username) # password='')
def print_status(filename, bytes_so_far, bytes_total):
percent = 100. * bytes_so_far / bytes_total
print('Transfer of %r is at %d/%d bytes (%.1f%%)' % (
filename, bytes_so_far, bytes_total, percent))
sftp = client.open_sftp()
for filename in filenames:
if filename.endswith('.copy'):
continue
callback = functools.partial(print_status, filename)
sftp.get(filename, filename + '.copy', callback=callback)
client.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Copy files over SSH')
parser.add_argument('hostname', help='Remote machine name')
parser.add_argument('username', help='Username on the remote machine')
parser.add_argument('filename', nargs='+', help='Filenames to fetch')
args = parser.parse_args()
main(args.hostname, args.username, args.filename)