Linux:子账户自动切换到root账户

忘记了是看的哪个大神的博客学到的 了

#!/usr/bin/expect
# 需要等待控制台捕获输出
set timeout 1

# spawn开启进程,也可以是ssh $user@$host {your_command}
# 只有先进入expect环境后才可执行spawn
spawn su root

# 判断上述进程(su root)的输出结果中是否有“password”的字符串(不区分大小写)。
# 若有则立即返回,否则就等待一段时间后返回,等待时长就是开头设置的1秒
expect "*password:"
# 向上面的进程(su root)发送字符串中的密码, 并且自动敲Enter健(\r)
send "xxxxxxxxx\r"
interact

猜你喜欢

转载自blog.csdn.net/shunzi2016/article/details/108417358