I have an interesting problem - I need to connect to an FTP/SFTP server from Python3
code, but the password contains \n
symbol inside. When I paste the password in the FTP/SFTP bash client, it works. But if I use pysftp
or ftplib
, authentification fails.
Of course, I can use pexpect
to emulate all things in the bash environment, but maybe there is a way to do it in a more pythonic style?
I use Ubuntu 18.10
, ftp v. 0.17-34
, pysftp 0.2.9
or paramiko 2.4.2
.
That is what I try to do
import paramiko
transport = paramiko.Transport('servername', 22)
password = "why\ninside"
username = anyuser
transport.connect(username=username, password=password)
That is what I get
---------------------------------------------------------------------------
AuthenticationException Traceback (most recent call last)
<ipython-input-7-2bf86c34be75> in <module>()
----> 1 transport.connect(username=username, password=password)
/home/vasily/.local/lib/python3.6/site-packages/paramiko/transport.py in connect(self, hostkey, username, password, pkey, gss_host, gss_auth, gss_kex, gss_deleg_creds, gss_trust_dns)
1261 else:
1262 self._log(DEBUG, "Attempting password auth...")
-> 1263 self.auth_password(username, password)
1264
1265 return
/home/vasily/.local/lib/python3.6/site-packages/paramiko/transport.py in auth_password(self, username, password, event, fallback)
1434 return []
1435 try:
-> 1436 return self.auth_handler.wait_for_response(my_event)
1437 except BadAuthenticationType as e:
1438 # if password auth isn't allowed, but keyboard-interactive *is*,
/home/vasily/.local/lib/python3.6/site-packages/paramiko/auth_handler.py in wait_for_response(self, event)
248 if issubclass(e.__class__, PartialAuthentication):
249 return e.allowed_types
--> 250 raise e
251 return []
252
AuthenticationException: Authentication failed.
But if I use pexpect
and send that password in FTP or SFTP, it goes.