0

I am trying to ssh into a remote machine and run a script. This script has an interactive confirmation: "Do you want to run it? {Y/N} " kind of thing. How can I say "Y" to this directly through the ssh command?

out=$(sshpass -p "abc" ssh -o StrictHostKeyChecking=no -l root x.x.x.x "./abc.py")

vedang09
  • 59
  • 1
  • 7
  • If that's literally the only string... `out=$(sshpass -p "abc" ssh -o StrictHostKeyChecking=no -l root x.x.x.x './abc.py << – Charles Duffy Jan 11 '18 at 04:55
  • Pretty much all canonical Unix tools let you override these confirmations with flags or similar. Case in point: you're doing it with `ssh`'s host key check right here. Check with `./abc.py` whether it offers such a feature before you try to emulate a human typing in responses. – that other guy Jan 11 '18 at 05:19

2 Answers2

0

Not sure if this works with sshpass but it definitely works with naked SSH either with key or password based authentication. If you are in a shell, you can always provide an input as stdout to the the script using pipes like echo $INPUT | ./input-script.py|sh as below -

$ ssh -i UAT.pem -o StrictHostKeyChecking=no -l ec2-user 13.222.237.508 "echo hello | ./1.py"
Please enter something: 

you entered hello

Python script -

#!/usr/bin/env python
var = raw_input("Please enter something: ")
print "\n"
print "you entered", var

Hope this works for you as well!

vivekyad4v
  • 13,321
  • 4
  • 55
  • 63
-1

We should be able to use 'expect' scripting for your problem. You can follow below link for reference - https://www.thegeekstuff.com/2010/10/expect-examples/.

  • 1
    Yes, `expect` *can* be used, but a good answer on StackOverflow is expected to be self-contained -- to actually solve the OP's problem *on its own*, even if any links contained therein go bad. Consequently, this would be much stronger if it actually showed an example of `expect` being used for the job at hand. – Charles Duffy Jan 11 '18 at 04:54