title: Two factor (key and keyboard-interactive) authentication to SFTP server using Python Paramiko
body: <p>I am trying to connect to a server via secure SFTP in Python but I keep getting authentication errors. I'm sure I have the right details as I can connect via WinSCP fine:</p>
  
  <p><a href="https://i.stack.imgur.com/Iqyss.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Iqyss.png" alt="enter image description here"></a></p>
  
  <p>I have also viewed the output of a Paramiko log file and it says the authentication of the password failed but I'm sure the password is right my code.</p>
  
  <p>Below is the code I am using which has been pieced together from various stackoverflow posts. Any ideas why it won't connect?</p>
  
  <pre><code>import paramiko
  import pysftp
  from base64 import decodebytes
  
  host = '&lt;my server address&gt;'
  port = 6671
  user = '&lt;my username&gt;'
  password = '&lt;my password&gt;'
  private_key = '&lt;location of my private key file&gt;'
  host_key_data = b"""&lt;contains my host key data&gt;"""
  host_key = paramiko.RSAKey(data=decodebytes(host_key_data))
  cnopts = pysftp.CnOpts()
  cnopts.hostkeys.add(host, 'ssh-rsa', host_key)
  pysftp.Connection(host, username=user, password=password, private_key=private_key, port=port, cnopts=cnopts)
  </code></pre>
  
  <p>Output of log file:</p>
  
  <pre class="lang-none prettyprint-override"><code>DEB [20190626-12:51:16.270] thr=4   paramiko.transport: Kex agreed: diffie-hellman-group-exchange-sha256
  DEB [20190626-12:51:16.271] thr=4   paramiko.transport: HostKey agreed: ssh-rsa
  DEB [20190626-12:51:16.272] thr=4   paramiko.transport: Cipher agreed: aes128-ctr
  DEB [20190626-12:51:16.273] thr=4   paramiko.transport: MAC agreed: hmac-sha1
  DEB [20190626-12:51:16.274] thr=4   paramiko.transport: Compression agreed: none
  DEB [20190626-12:51:16.774] thr=4   paramiko.transport: Got server p (1024 bits)
  DEB [20190626-12:51:17.029] thr=4   paramiko.transport: kex engine KexGexSHA256 specified hash_algo &lt;built-in function openssl_sha256&gt;
  DEB [20190626-12:51:17.030] thr=4   paramiko.transport: Switch to new keys ...
  DEB [20190626-12:51:17.032] thr=2   paramiko.transport: Host key verified (ssh-rsa)
  DEB [20190626-12:51:17.032] thr=2   paramiko.transport: Attempting password auth...
  DEB [20190626-12:51:17.531] thr=4   paramiko.transport: userauth is OK
  INF [20190626-12:51:17.532] thr=4   paramiko.transport: Auth banner: b'FactSet File Transfer System (FTS)\n'
  INF [20190626-12:51:17.767] thr=4   paramiko.transport: Authentication (password) failed.
  </code></pre>
  
  <p>VBA code using WinSCP .NET assembly, which works:</p>
  
  <pre class="lang-vb prettyprint-override"><code>' Setup session options
  Dim mySessionOptions As New SessionOptions
  With mySessionOptions
      .Protocol = Protocol_Sftp
      .HostName = "&lt;hostname&gt;"
      .PortNumber = 6671
      .UserName = "&lt;username&gt;"
      .Password = "&lt;password&gt;"
      .SshHostKeyFingerprint = "ssh-rsa 2048 fingerprintkey"
      .SshPrivateKeyPath = "location of private key file"
  End With  
  </code></pre>