Unix - run the command as another user

I am new to Unix. I am trying to run a shell script and command as another user.

For example: I am registered as user1 and want to execute the script as user2. I do not need a password hint, and I want it to be entered automatically. I know about public key authentication, but I'm trying to find if there is something like:

sudo -u user2 script.sh

I'm trying to do this, so I don't need a password hint. I want this to be handled in the sudo command itself.

please, help

+5
source share
2 answers

You can add the NOPASSWD option to the file /etc/sudoers. But maybe this is not good for you for security reasons.

user    user2 = NOPASSWD: /usr/local/bin/script.sh

, : sudo, script, pexpect expect, . , script.

expect - :

#!/usr/bin/expect
set password "megapassword"
spawn /bin/sudo -u user1 /usr/local/bin/script.sh
expect "password for user:"
send "$password\r"
expect eof

500 .

pexpect ( python) :

import pexpect
password = "megapassword"
p = pexpect.spawn("sudo -u user1 /usr/local/bin/script.sh")
i = p.expect([".ssword:*", pexpect.EOF])
p.sendline(password)

cron crontab user1, . .

user1$ crontab -e
+4

NOPASSWD /etc/sudoers, , user2 crontab .

0

All Articles