首页 > 利用expect 切换账户后如何继续执行shell

利用expect 切换账户后如何继续执行shell

利用expect登录root账户,结果shell也不能继续执行了

#!/bin/sh
expect -c "
spawn su root
expect \"Password:\"
send \"*******\r\"
interact
"
其他shell代码...

其他shell代码...这里不能执行


我是这么做的,先登陆远程服务器,然后执行删除和重命名操作。

#!/usr/bin/expect -f
set timeout 600
spawn ssh root@xx.xx.xx.xx
expect "*"
send "cd /usr/share/nginx/html\r"
send "rm -rf tkb\r"
send "mv site tkb\r"
interact

其他shell 可以使用在expect中send发送 send \"echo 123;\r\"


切换用户后环境变量已经变了,之前用户的命令是不会生效的,建议使用su -root -c '命令集..'来执行

【热门文章】
【热门文章】