1、安装expect
#expect su实现输入su命令之后自动填写密码的功能
yum install expect -y #CentOS
#sudo apt-get install expect #Ubuntu
2、编写expect脚本:
vim test1.sh
chmod +x test1.sh
执行:./test1.sh
注意:这里执行expect程序时不能按照习惯来用sh ***.sh,会提示找不到命令,因为expect用的不是bash所以会报错。执行的时候直接./test1.sh即可。