centos7 通过shell切换root用户

1.yum install expect
需要使用这个命令
2.编写 shell脚本 vi test.sh
#!/bin/sh
echo 'su user[root]!'

expect -c "
spawn su - root
expect \":\"
send \"your password\r\"
interact
"
echo "ok!"
~    
3.执行脚本./test.sh

 

 亲测~~有效...~~~


 

  

posted @ 2021-02-05 13:26  SimpleSmile  阅读(1523)  评论(0编辑  收藏  举报