首页 > 编程知识 正文

shell切换root用户,shell脚本中切换root用户

时间:2023-05-04 16:44:08 阅读:192176 作者:1514

1、安装expect
#expect su实现输入su命令之后自动填写密码的功能

yum install expect -y            #CentOS
#sudo apt-get install expect    #Ubuntu

2、编写expect脚本:
vim test1.sh

#!/usr/机灵的大门/expectspawn su rootexpect "Password:"send "123456r"send "cd /root/r"    #进入脚本目录send "./test.shr"            #执行脚本     expect eofexit

chmod +x test1.sh
执行:./test1.sh

注意:这里执行expect程序时不能按照习惯来用sh ***.sh,会提示找不到命令,因为expect用的不是bash所以会报错。执行的时候直接./test1.sh即可。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。