1
1
from ftplib import FTP
2
2
from dataclasses import dataclass
3
- from paramiko import SSHClient
3
+ from paramiko import SSHClient , AutoAddPolicy
4
4
from scp import SCPClient
5
5
6
6
class FtpHelper (object ):
@@ -12,7 +12,7 @@ def __init__(self, tgt: str, user: str, password : str):
12
12
self .username = user
13
13
self .password = password
14
14
15
- def getFile (self , tgt_path , dest_path ) -> str :
15
+ def get_file (self , tgt_path , dest_path ) -> str :
16
16
try :
17
17
with FTP (self .tgt_address , self .username , self .password ) as f :
18
18
with open (dest_path , "wb" ) as file :
@@ -22,7 +22,7 @@ def getFile(self, tgt_path, dest_path) -> str:
22
22
except Exception as e :
23
23
raise e
24
24
25
- def uploadFile (self , src_path , tgt_path ):
25
+ def upload_file (self , src_path , tgt_path ):
26
26
try :
27
27
with FTP (self .tgt_address , self .username , self .password ) as f :
28
28
with open (src_path , "rb" ) as file :
@@ -32,27 +32,32 @@ def uploadFile(self, src_path, tgt_path):
32
32
33
33
class ScpHelper (object ):
34
34
"""
35
- Class used to wrap SSH and SCP file transfer for sbRIO 9603 controllers. Call using 'with' statements, or call open
35
+ Class used to wrap SSH and SCP file transfer for sbRIO 9603 controllers.
36
+ Call using 'with' statements, or call open
36
37
"""
37
38
def __init__ (self , tgt : str , user : str , password : str ):
38
39
self .tgt_address = tgt
39
40
self .username = user
40
41
self .password = password
41
42
42
- def __enter__ (self ):
43
+ def __enter__ (self , * args , ** kwargs ):
43
44
try :
44
45
self .open ()
45
46
except Exception as e :
46
47
print (e )
48
+ return self
47
49
48
- def __exit__ (self ):
50
+ def __exit__ (self , * args , ** kwargs ):
49
51
self .close ()
50
52
51
53
def open (self ):
52
54
"""Open """
53
55
self .ssh = SSHClient ()
54
56
self .ssh .load_system_host_keys ()
55
- self .ssh .connect (self .tgt_address )
57
+ self .ssh .set_missing_host_key_policy (AutoAddPolicy ())
58
+ self .ssh .connect (self .tgt_address ,
59
+ username = self .username ,
60
+ password = self .password )
56
61
# SCPCLient takes a paramiko transport as an argument
57
62
self .scp = SCPClient (self .ssh .get_transport ())
58
63
@@ -61,15 +66,15 @@ def close(self):
61
66
self .scp .close ()
62
67
self .ssh .close ()
63
68
64
- def getFile (self , tgt_path , dest_path ) -> str :
69
+ def get_file (self , tgt_path , dest_path ) -> str :
65
70
try :
66
71
with self .scp as s :
67
72
s .get (remote_path = tgt_path , local_path = dest_path )
68
73
return dest_path
69
74
except Exception as e :
70
75
print (e )
71
76
72
- def uploadFile (self , src_path , tgt_path ):
77
+ def upload_file (self , src_path , tgt_path ):
73
78
try :
74
79
with self .scp as s :
75
80
s .put (files = src_path , remote_path = tgt_path )
0 commit comments