/
Testy.py
114 lines (95 loc) · 4.26 KB
/
Testy.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import paramiko
def create_sftp_client(host, port, username, password, keyfilepath, keyfiletype):
"""
create_sftp_client(host, port, username, password, keyfilepath, keyfiletype) -> SFTPClient
Creates a SFTP client connected to the supplied host on the supplied port authenticating as the user with
supplied username and supplied password or with the private key in a file with the supplied path.
If a private key is used for authentication, the type of the keyfile needs to be specified as DSA or RSA.
:rtype: SFTPClient object.
"""
sftp = None
key = None
transport = None
try:
if keyfilepath is not None:
# Get private key used to authenticate user.
if keyfiletype == 'DSA':
# The private key is a DSA type key.
key = paramiko.DSSKey.from_private_key_file(keyfilepath)
else:
# The private key is a RSA type key.
key = paramiko.RSAKey.from_private_key(keyfilepath)
# Create Transport object using supplied method of authentication.
transport = paramiko.Transport((host, port))
transport.connect(None, username, password, key)
sftp = paramiko.SFTPClient.from_transport(transport)
return sftp
except Exception as e:
print('An error occurred creating SFTP client: %s: %s' % (e.__class__, e))
if sftp is not None:
sftp.close()
if transport is not None:
transport.close()
pass
def create_sftp_client2(host, port, username, password, keyfilepath, keyfiletype):
"""
create_sftp_client(host, port, username, password, keyfilepath, keyfiletype) -> SFTPClient
Creates a SFTP client connected to the supplied host on the supplied port authenticating as the user with
supplied username and supplied password or with the private key in a file with the supplied path.
If a private key is used for authentication, the type of the keyfile needs to be specified as DSA or RSA.
:rtype: SFTPClient object.
"""
ssh = None
sftp = None
key = None
try:
if keyfilepath is not None:
# Get private key used to authenticate user.
if keyfiletype == 'DSA':
# The private key is a DSA type key.
key = paramiko.DSSKey.from_private_key_file(keyfilepath)
else:
# The private key is a RSA type key.
key = paramiko.RSAKey.from_private_key(keyfilepath)
# Connect SSH client accepting all host keys.
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password, key)
# Using the SSH client, create a SFTP client.
sftp = ssh.open_sftp()
# Keep a reference to the SSH client in the SFTP client as to prevent the former from
# being garbage collected and the connection from being closed.
sftp.sshclient = ssh
return sftp
except Exception as e:
print('An error occurred creating SFTP client: %s: %s' % (e.__class__, e))
if sftp is not None:
sftp.close()
if ssh is not None:
ssh.close()
pass
host = '10.224.20.12'
port = 22
username = 'sftpuser'
keyfile_path = None
password = 'start123'
sftpclient = create_sftp_client(host, port, username, password, keyfile_path, 'DSA')
# List files in the default directory on the remote computer.
dirlist = sftpclient.listdir('./shared/')
for row in dirlist:
print(row)
dirlist2 = sftpclient.listdir('./shared/Semiconductors/')
for row in dirlist2:
print(row)
'''
dirlist3 = sftpclient.listdir('./shared/chemistry/pdf')
for row in dirlist3:
print(row)
'''
# Retrieve a file with the name 'remote_file.txt' on the remote computer and store it in a file named 'downloaded_file.txt'
# next to this SFT client program.
#sftpclient.get('./shared/testzdj.png', 'zdj1.png')
# Upload a file that locally has the name 'testfile.txt' to a file on the remote computer that will have the name 'remote_testfile.txt'.
sftpclient.put('test23.txt', './shared/test23.txt')
# We're done with the SFTPClient.
sftpclient.close()