-2

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.

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
V.Kuimov
  • 1
  • 4
  • Try escaping it? – rdas Apr 18 '19 at 07:17
  • Maybe try double escaping the newline character `\\n` ... – l'L'l Apr 18 '19 at 07:20
  • escaping by what way? It is not completely clear for me:) – V.Kuimov Apr 18 '19 at 07:29
  • double slash is not the way – V.Kuimov Apr 18 '19 at 07:31
  • I cannot imaging it works in a command-line ftp/sftp client. They will stop reading the password, once they encounter the `\n` character. Don't you actually have two passwords (multi factor authentication)? – Martin Prikryl Apr 18 '19 at 08:04
  • 1
    A password in FTP is according to the standard (RFC 959) a string consisting of ASCII characters, explicitly excluding and . There is no standard on how passwords which are different should be encoded. If you think that `ftp` on terminal does this correctly then please do a packet capture (using tcpdump or wireshark) to determine how the password is transferred in the PASS command. Note the sftp is irrelevant here since this is not FTP but file transfer over SSH. – Steffen Ullrich Apr 18 '19 at 09:03
  • @MartinPrikryl, anyway it works:) I tried to paste the first part first, but it says the password wrong. – V.Kuimov Apr 18 '19 at 11:21
  • @MartinPrikryl but it seems to be you are right. I used Wireshark and noticed that only first half is accepted. – V.Kuimov Apr 18 '19 at 11:35
  • @MartinPrikryl write your idea as an answer please, I will mark it as a solution. – V.Kuimov Apr 18 '19 at 11:44

1 Answers1

-1

Use this;

password = r"why\ninside"
nuriselcuk
  • 515
  • 1
  • 4
  • 17