-
Notifications
You must be signed in to change notification settings - Fork 2
/
verify_sftp_connection.py
58 lines (45 loc) · 1.75 KB
/
verify_sftp_connection.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
"""
brew install corkscrew
"""
import io
import logging
from paramiko import RSAKey
import fsspec
import paramiko
logging.basicConfig()
logging.getLogger("paramiko").setLevel(logging.DEBUG) # for example
def with_proxy():
# pkey = RSAKey.from_private_key(io.StringIO(credentials['private_key_ascii']))
pkey = paramiko.Ed25519Key.from_private_key(io.StringIO(credentials['private_key_ascii']))
pcommand = f'corkscrew {credentials["proxy_host"]} {credentials["proxy_port"]} %s %d' % (credentials['host'], credentials['port'])
proxy = paramiko.proxy.ProxyCommand(pcommand)
authentication_kwargs = dict()
authentication_kwargs["pkey"] = pkey
authentication_kwargs["sock"] = proxy
fs = fsspec.filesystem(
"sftp", host=credentials['host'], port=credentials['port'], username=credentials['username'], **authentication_kwargs
)
if fs:
print("fs found:")
files = fs.ls("/", detail=True)
print("Files in the remote directory:")
k = []
for file in files:
k.append(file['name'])
print(sorted(k))
def without_proxy():
pkey = RSAKey.from_private_key(io.StringIO(credentials['private_key_ascii']))
authentication_kwargs = dict()
authentication_kwargs["pkey"] = pkey
fs = fsspec.filesystem(
"sftp", host=credentials['host'], port=credentials['port'], username=credentials['username'], **authentication_kwargs
)
if fs:
print("fs found:")
files = fs.ls("/", detail=True)
print("Files in the remote directory:")
for file in files:
print(file['name'])
if __name__ == '__main__':
credentials = {"host": "sftp.bloomberg.com", "port": 22, "username": "", "proxy_host": "", "proxy_port": 8080}
with_proxy()