简体   繁体   English

sudo su用户名,密码和命令

[英]sudo su with user name, password and command

I am trying to do something like 我正在尝试做类似的事情

echo 'my_password' | sudo -S su -c some-user ./some_command

I have looked at 我看过

How can I apply password to sudo in one line command and execute su root? 如何在一行命令中将密码应用于sudo并执行su root?

and

run a sudo command, specifying the password on the same line 运行sudo命令,在同一行上指定密码

But neither of these really answer my question above. 但是这些都不是真的回答我上面的问题。

I am still getting the password prompt for my user. 我仍然收到用户的密码提示。

I've got two answers for you. 我有两个答案给你。

The first answer is "don't do it". 第一个答案是“不要这样做”。 There is, almost certainly, a better way. 几乎可以肯定,有更好的方法。 You can specify to sudo that certain users can perform certain commands without entering a password. 您可以指定sudo以便某些用户无需输入密码即可执行某些命令。 In all likelihood, that is what you want to do. 这很可能就是您想要做的。

Having failed to convince you, however, I will let you in on a little secret. 但是,由于无法说服您,我将让您保密。 sshpass works on sudo , so: sshpass适用于sudo ,因此:

sshpass -p 'my password' sudo -S su -c some-user ./some_command

Of course, while there, we can cut the su middle man: 当然,在那里,我们可以切割su中间人:

sshpass -p 'my password' sudo -S -u some-user ./some_command

You can use expect. 您可以使用期望。 First install the expect rpm then run command look like this: 首先安装Expect rpm,然后运行命令,如下所示:

expect -c "spawn sudo /tmp/test.sh; sleep 3; expect -re \"password\"; send \"mypassword\r\n\";set timeout 5;expect -re \"time=\";"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM