在shell脚本中传递su密码 [英] Passing su password in shell script
本文介绍了在shell脚本中传递su密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 su(没有 sudo 和 except)在 shell 脚本中传递密码?.我试过 echo "password" |su root -c .但是没有用.
How can password be passed in a shell script using su(without sudo and except)?. I have tried echo "password" | su root -c .But it didnt work.
推荐答案
最好的方法是使用 sudo
,但既然你不想要最好的解决方案,你可以使用script
代替:
The best way of doing this is with sudo
, but since you don't want the best solution, you can you can use script
instead:
{ sleep 3; echo "yourpassword"; } | script -q -c 'su -c whoami' /dev/null
这将打印 root
,whoami
的输出.
This will print root
, the output of whoami
.
在尝试调整它以运行您自己的命令之前,请确保逐字尝试此命令(替换密码),因为调整它很困难且容易出错.
Please make sure to try this command verbatim (with password replaced) before trying to adapt it to run your own commands, since adapting it is difficult and error prone.
这篇关于在shell脚本中传递su密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文