su将密码传递给脚本 [英] su pass password to script

查看:324
本文介绍了su将密码传递给脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本将运行以下命令:

I am trying to write a script that will run the following commands:

sudo su
runmqsc_result=`su -c "runmqsc QMGR < /home/rob/query_queue.txt" -m "mqm"`

但是,我的问题是,这些命令是由sudoers文件中的用户作为Shell脚本的一部分运行的.但是,显然sudo su要求输入运行它的用户的密码.

My issue however, is that these commands are run as part of a shell script, by user that is in the sudoers file. However, obviously sudo su asks for the password of the user running it.

我需要做的是将密码传递给sudo su,以便脚本自动运行.我该怎么办?

What I need to do is to pass the password to sudo su so that the script will run automatically. How can I do this?

p.s:我无法更改运行"runmqsc"的权限...它必须以用户mqm的身份运行,而该用户需要从root用户切换到.

p.s: I can't change the permissions for running "runmqsc"...it HAS to be run as user mqm which needs to be switched to from the root user.

推荐答案

来自man sudo:

-S    The -S (stdin) option causes sudo to read the password from the standard
      input instead of the terminal device.  The password must be followed by a
      newline character.

因此,尽管违反了所有安全原则,但echo 'password' | sudo -S su [...]应该可以工作.

So, while it defies all security principles, echo 'password' | sudo -S su [...] should work.

或者,您可以使脚本只能由root用户写,然后将以下内容添加到/etc/sudoers中,以允许用户johndoe以root特权运行该脚本,而无需输入密码:

Alternatively, you could make your script writeable only by root and add the following to /etc/sudoers to allow the user johndoe to run it with root priviledges without having to enter his password:

johndoe ALL = NOPASSWD: /full/path/to/your/script

只能由root用户写入的部分对于防止johndoe修改脚本并以root用户身份执行任意命令很重要.

The part writeable only by root is important to prevent johndoe from modifying the script and executing arbitrary commands as root.

这篇关于su将密码传递给脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆