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

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

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

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

阅读 9.1k
3 个回答

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

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

#!/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
新手上路,请多包涵

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题