@@ -50,8 +50,40 @@ def __init__(self, host, **ssh_kwargs):
50
50
def _connect (self ):
51
51
logger .debug ("Connecting to SFTP server %s" , self .host )
52
52
self .client = paramiko .SSHClient ()
53
- self .client .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
54
- self .client .connect (self .host , ** self .ssh_kwargs )
53
+ self .config = paramiko .SSHConfig .from_path (os .path .expanduser ("~/.ssh/config" )).lookup (self .host )
54
+ if "hostname" in self .config :
55
+ self .host = self .config ["hostname" ]
56
+
57
+ # Can be yes, no, or ask (case-insensitive). Ask is not supported. RejectPolicy is the default.
58
+ if "StrictHostKeyChecking" .lower () in self .config and self .config ["stricthostkeychecking" ].lower () == "no" :
59
+ self .client .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
60
+
61
+ known_host_keys_file = "~/.ssh/known_hosts"
62
+ if "UserKnownHostsFile" .lower () in self .config :
63
+ known_host_keys_file = self .config ["UserKnownHostsFile" .lower ()]
64
+ try :
65
+ self .client .load_system_host_keys (os .path .expanduser (known_host_keys_file ))
66
+ except (FileNotFoundError , PermissionError ):
67
+ pass
68
+
69
+ # Dictionary to map ssh_config keys to paramiko.Client.connect argument names.
70
+ key_mappings = {
71
+ "Port" : ("port" , lambda x : int (x )),
72
+ "User" : ("username" , lambda x : x ),
73
+ "Compression" : ("compress" , lambda x : x .lower () == "yes" ),
74
+ "IdentityFile" : ("key_filename" , lambda x : x ),
75
+ "ConnectTimeout" : ("timeout" , lambda x : float (x )),
76
+ }
77
+ connect_options = self .ssh_kwargs .copy ()
78
+ for config_key , value in key_mappings .items ():
79
+ argument_name , converter = value
80
+ if config_key .lower () in self .config and argument_name not in connect_options :
81
+ connect_options [argument_name ] = converter (self .config [config_key .lower ()])
82
+
83
+ if "key_filename" in connect_options :
84
+ connect_options ["look_for_keys" ] = False
85
+
86
+ self .client .connect (self .host , ** connect_options )
55
87
self .ftp = self .client .open_sftp ()
56
88
57
89
@classmethod
0 commit comments