阅读:1927回复:2
普通用户su到root时传递密码的解决方案
原生的shell 脚本不可能,不要尝试。没意思
但是可以使用一个工具,expect 如果你没有 你需要安装 (本文是centos ,其他操作系统自己搞定) yum install expect 默认是装在 /usr/bin/expect 的。 然后用普通用户 创建一个shell脚本 #!/usr/bin/expect -f 别忘了这句, 默认是bash 是然并卵的 spawn su -l expect "Password:*" sleep 1 send "123\r" 这里要写你root的密码,不要问为什么。 expect "root]#" interact exit |
|
|
沙发#
发布于:2016-02-25 15:46
我想在进入root后在root中执行一个命令,要在哪儿接?
|
|
板凳#
发布于:2016-02-26 00:34
看明白了,send就可以了,主要是刚才的expect命令不太理解。 #!/usr/bin/expect -f spawn su -l expect "Password:*" sleep 1 send "nokia123\r" expect "root]#" sleep 1 send "ls\r" interact exit |
|