当一个bash脚本运行自动输入密码 [英] Automatically input a password when a bash script is run
问题描述
举例来说,如果说我有一个脚本说:
For example, say if I have a script saying:
#!/bin/bash
sudo setpci -s 00:02.0 F4.B=00
我如何把root口令到脚本,以便它接受它作为密码时,它读取并执行sudo的线(所以我不必手动输入)?
How do I put the root password into the script so that it accepts it as the password when it reads and executes the sudo line (so I don't have to type it manually)?
推荐答案
产卵的预计会话中的bash脚本是通常你是如何自动化交互提示。
Spawning an expect session within your bash script is typically how you automate interactive prompts.
例如
expect -c "
spawn sudo setpci -s 00:02.0 F4.B=00
expect -nocase \"password:\" {send \"$PASS\r\"; interact}
"
请注意,这不是在这种情况下推荐的方法,因为它是一个大安全漏洞来存储你的root密码在bash脚本。
Note that this isn't the recommended approach in this case as it is a huge security hole to store your root password in a bash script.
正确的解决办法是修改的/ etc / sudoers文件/
不提示您为二进制的密码。
The correct solution would be to edit your /etc/sudoers/
to not prompt you for a password for that binary.
#in /etc/sudoers
neohexane ALL=(ALL) NOPASSWD : /usr/bin/setpci
这篇关于当一个bash脚本运行自动输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!