My problem is about executing sudo over SSH. I have tried various things, but non works.

Here is my script:

ssh my-host /bin/bash << EOF
# some commands...

sudo -u my-user -s -- << EOFF
# other commands...
EOFF

EOF

Like that, I get

sudo: no tty present and no askpass program specified

So I tried with option -t for ssh

ssh -t my-host /bin/bash << EOF

Still get the same message. So I tried with double -t -t:

ssh -t -t my-host /bin/bash << EOF

Now I get to the password prompt, except I can see my password as I type it and pressing enter has no effect.

Additional constraint: I have limited rights on my-host. I cannot sudo, except to become user my-user.

Any idea? Thanks in advance

  • Why not just ssh my-user@my-host? – Alexey Ten Jun 10 '16 at 14:14
  • Because for some reason this does not work either, although I set up my ssh key and put all the right modes for .ssh and .ssh/authorized_keys. I guess the system admin deactivated it somehow. – Catherine Gasnier Jun 10 '16 at 14:47
  • that is probably the weirdest attempt to misuse heredoc I have ever seen. Use the keys. If you can't, use expect (a lot questions around here), which gives you much better control of what is going on in your code. – Jakuje Jun 10 '16 at 16:03
  • When you use a here-doc, there's no way for the remote terminal control to affect how you interact locally. The here-doc is read on the client machine before anything is sent to the server. – Barmar Jun 14 '16 at 21:56
  • Assuming your script changes rarely, you should probably just install it on the remote system and enable your account to run it as root with NOPASSWD in that system's /etc/sudoers. That way, you can just ssh my-host sudo /path/to/that/script and not have to worry about interactivity at all. – Adam Katz Aug 29 '16 at 22:29

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.