-2

I am using pexpect to upload a file to SFTP server. For accesssing the server first time, I get message as The authenticity of host..can't be established.Are you sure you want to continue connecting (yes/no)?

to which I want the user to interact and answer yes/no. However, when user says yes, I want the interactive mode to be off and then take the password from my script instead of prompting user. Is this possible using pexpect ?

p = spawn('XXXXXXXX')
password = 'XXXXXXXXx'
out=p.expect(['(?i)password:', '(?i)Are you sure you want to continue connecting (yes/no)?']

if out == 1:
   p.interact()
   
# Script should continue to send password and upload file

But this goes into interactive mode and doesn't come back to script

Pls. note - I run this python script in my terminal.

If not pexpect, pls.propose a solution not based on paramiko, pysftp

Simplecode
  • 559
  • 7
  • 19
  • I want to upload file to my SFTP server. However, I am not allowed to use paramiko/pysftp. Can just use pure python or pexpect – Simplecode Dec 17 '20 at 11:09
  • It's a requirement that user should verify. Also, adding to "known_hosts" would work for me, but if someone else runs this script on their machine, they should ideally verify – Simplecode Dec 17 '20 at 11:16
  • All this pexpect code is in my script that's all !! – Simplecode Dec 17 '20 at 11:25

1 Answers1

0

Check the pexpect documentation:

interact(escape_character='\x1d', input_filter=None, output_filter=None)[source]

This gives control of the child process to the interactive user (the human at the keyboard). Keystrokes are sent to the child process, and the stdout and stderr output of the child process is printed. This simply echos the child stdout and child stderr to the real stdout and it echos the real stdin to the child stdin. When the user types the escape_character this method will return None.

You can pass an escape character to p.interact(), after which point interactive mode will be disabled. If all the user needs to type is yes, then you could set the escape character to a (or some other letter that doesn't appear in "yes" or "no"), like this:

p.interact('a')
joedeandev
  • 626
  • 1
  • 6
  • 15
  • thanks for answering this. I did try this approach, however I got stuck at following1. If user presses 'yes' and the escape character (I kept the enter key for now), the user does come out of the interactive mode but the "yes" doesn't seem to be captured as I don't see the sftp host entry in the "known_hosts". Need to check why pressing "yes" and the "escape character" does not add the host to "known_hosts". why that input is not taken properly. – Simplecode Dec 21 '20 at 13:31
  • 2. I kept the escape character as "a" (as per your answer). In this case, when user says "yes" and I don't press the "a", the entry gets added in the "known_hosts" and it prompts me for password (where if I press "a", I come out of the interactive. However, I do not want the user to see that password prompt. – Simplecode Dec 21 '20 at 13:32