0

I need to write a python script which must be able to connect to a remote server (to user1), execute some commands (this was easy), then it must switch to user2 through sudo su - user2, and execute some commands (the problem is here). How can make it switch to user2 without asking me the password (I can put the password somewhere in the script), and execute commands as user2.

Thanks,

smarber
  • 4,829
  • 7
  • 37
  • 78
  • Aside: Why "`sudo su - user2`"? Why not "`sudo -u user2 -s`"? – Robᵩ Feb 28 '13 at 14:23
  • Because I did not know sudo -u user2 -s :). Anyway, I tried: sudo('su -u user2 -s') **user user1 is not allowed to execute '/bin/bash -l -c su -u user2 -s' as root on new_hub.** – smarber Feb 28 '13 at 14:36

1 Answers1

1

Try -S (capital "S"). Quoting the sudo man page:

  -S          The -S (stdin) option causes sudo to read the password from
               the standard input instead of the terminal device.
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Got it. Only what I'm allowed to do as superuser is sudo su - user2, if I try sudo it won't pass. And doing sudo('su - user2') will try to execute this **sudo -S -p** which is not possible for me. fabric is not specific enough :) on some points – smarber Feb 28 '13 at 15:14