@@ -136,21 +136,27 @@ def _setup_ssh(self):
136
136
if self .totp_secret :
137
137
ts .auth_interactive (self .username , self .inter_handler )
138
138
else :
139
- default_path = os .path .join (os .environ ["HOME" ], ".ssh" , "id_rsa" )
140
- path = ""
139
+ key_path = os .path .join (os .path .expanduser ("~" ), ".ssh" , "id_rsa" )
141
140
if self .key_filename :
142
- path = os .path .abspath (self .key_filename )
141
+ key_path = os .path .abspath (self .key_filename )
142
+ key = None
143
+ if os .path .exists (key_path ):
144
+ try :
145
+ key = paramiko .RSAKey .from_private_key_file (key_path )
146
+ except paramiko .PasswordRequiredException :
147
+ key = paramiko .RSAKey .from_private_key_file (key_path , self .passphrase )
148
+ if key :
149
+ try :
150
+ ts .auth_publickey (self .username , key )
151
+ except paramiko .ssh_exception .AuthenticationException :
152
+ if self .password :
153
+ ts .auth_password (self .username , self .password )
154
+ else :
155
+ raise RuntimeError ("Authentication failed, try to provide password" )
156
+ elif self .password :
157
+ ts .auth_password (self .username , self .password )
143
158
else :
144
- path = default_path
145
- try :
146
- key = paramiko .RSAKey .from_private_key_file (path )
147
- except paramiko .PasswordRequiredException :
148
- key = paramiko .RSAKey .from_private_key_file (path , self .passphrase )
149
- try :
150
- ts .auth_publickey (self .username , key )
151
- except paramiko .ssh_exception .AuthenticationException :
152
- if self .password :
153
- ts .auth_password (self .username , self .password )
159
+ raise RuntimeError ("Please provide at least one form of authentication" )
154
160
assert (ts .is_active ())
155
161
#Opening a session creates a channel along the socket to the server
156
162
ts .open_session (timeout = self .timeout )
0 commit comments