I have local shell script. Now, I want to run this local script in remote machine. Of course I can use ssh, but as it(the local shell script) should be like an automated one, it shouldn't ask for password to enter manually. So I used expect to eliminate that problem, so that if I run the script it wont ask password. And my script looks like below.

spawn ssh -o GSSAPIAuthentication=no root@ 'bash -s' < /path/to/localscript
expect -timeout 7 "*(yes/no)?" { send "yes\r";exp_continue } 
expect -timeout 3 "*d: "
send "qbcrootpass\n"

But it is not working. Please note, in my scenario, I don't want to use public key authentication.

Please help me to execute a local script in a remote machine using using expect.

  • How would you authenticate with neither the public key not the password? – jobin Mar 25 '14 at 13:02
  • @Jobin The password is being sent by the expect script above. – Oli Mar 25 '14 at 13:13
  • I hope you are aware that this is much less safe than using a public key. Just in case you were doing this for security. – terdon Mar 25 '14 at 15:06
  • Hi terdon, I know this is less secure. But I need this in my scenario. – Uvais Ibrahim Mar 27 '14 at 6:48

That redirection is processed by your shell at the command line. Expect won't understand how to do it. This should work (untested):

set fh [open /path/to/localscript r]
set contents [read $fh]
close $fh

spawn ssh ... "bash -c '$contents'"
  • bah, there are quoting issues there ("there be dragons..."). Let us know how you get on. – glenn jackman Mar 25 '14 at 13:35
  • Your approach here might be a better one: stackoverflow.com/a/14044575/12870 – Oli Mar 26 '14 at 8:58
  • Hi glenn, now it just displays the external script while running this. – Uvais Ibrahim Mar 27 '14 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.