expect 普通用户自动输入密码到root下,执行命令

案例:当前服务器取消了直接使用root登录服务器,只能使用普通用户先登录,然后再su - root 执行root下的命令。

shell脚本如下:

#!/usr/bin/expect -f
set password {root_password}
spawn su - root
expect "密码:"
send "$password\r"
#send "sh ceshi.sh > temp.log &\r"
#send "timeout 120 tail -f temp.log\r"
send "pwd\r"
send "exit\r"
interact

exit 0

猜你喜欢

转载自blog.51cto.com/10921657/2118938