1、安装软件 yum install expect -y 2、查看expect命令位置 expect命令的位置也可能是在 /usr/local/bin/expect,使用前用户应确认工具是否存在(确认方法是在root用户下运行“which expect”命令) 3、expect非交互拷贝秘钥 注意这里的spawn ssh -l,因为这个脚本属于登录脚本,所以需要加ssh -l,其他的几个修改密码的脚本不要加 cat > expect.sh<<EOF 或者 expect.py 都可以 #!/usr/bin/expect set timeout 30 spawn ssh -l ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.92 expect "password:" send "123 " interact EOF chmod +x expect.sh ./expect.sh 或者: cat > expect.py<<EOF #!/usr/bin/expect set timeout 30 spawn ssh -l ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.92 expect "password:" send "123 " interact EOF
chmod +x expect.py
./expect.py 注意:都需要使用./expect.py来执行,如果使用sh /expect.py执行就会报错;使用python expect.py根本不能执行 执行结果: [root@test1 ~]# ./expect.py spawn ssh -l ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.92 root@192.168.0.92's password: Last login: Wed Jan 30 22:10:15 2019 from test1 [root@test2 ~]# 4、自动登录harbor [root@kube-node2 ~]# cat a.sh #!/usr/bin/expect set timeout 30 spawn docker login 192.168.0.73:5000 expect "Username (admin):" send "admin " expect "Password:" send "Harbor12345 " interact chmod +x a.sh 执行的时候必须用 ./a.sh 用 sh a.sh 就报错 下面执行的前提是设置使用sudo需要输入密码,前提设置 参照使用sudo篇。目的是为了实现expect功能。如果使用sudo 还是设置成使用sudo不需要输入密码好 5、自动切换su并且执行ls命令 [root@oracledb ~]# cat login.sh #!/usr/bin/expect set timeout 5 spawn su oracle -c "sudo ls" expect "password for oracle:" send "123 " interact chmod +x login.sh 执行结果: [root@oracledb ~]# ./login.sh #会卡顿一下,不影响结果 spawn su oracle -c sudo ls [sudo] password for oracle: 2.txt a.sh deplomet.yaml login.sh orcale.txt pdksh-5.2.14-37.el5.x86_64.rpm python3 3.txt c.sh d.sh orcale output.sh pip_output.sh Python-3.6.5.tgz anaconda-ks.cfg cut_new.sh d.txt orcale_output.sh output.txt pip_output.txt user.txt a.py cut.sh lnmp.sh orcale.sh –p pip.sh 6、自动创建普通用户;创建密码;切换账号;执行命令 [root@oracledb ~]# cat c.sh #!/usr/bin/expect set timeout 5 spawn passwd mysql expect "New password:" send "123 " expect "Retype new password:" send "123 " spawn su mysql -c "sudo ls" expect "password for mysql:" send "123 " interact chmod +x c.sh 执行的时候必须用 ./a.sh 用 sh a.sh 就报错 执行结果: [root@oracledb ~]# ./c.sh spawn passwd mysql Changing password for user mysql. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: spawn su mysql -c sudo ls [sudo] password for mysql: anaconda-ks.cfg c.tct lnmp.sh pdksh-5.2.14-30-mdv2011.0.x86_64.rpm a.sh d.sh local.repo pdksh-5.2.14-37.el5.x86_64.rpm a.txt d.txt menue.sh Python-3.6.5 b.sh glibc-static-2.17-55.el7.x86_64.rpm myf python.sh compat-libstdc++-33-3.2.3-69.el6.x86_64.rpm linux.x64_11gR2_database_1of2.zip mysql.sh soft c.sh linux.x64_11gR2_database_2of2.zip –p 报错: [root@oracledb ~]# su oracle -c "ls" ls: cannot open directory .: Permission denied 解决: sed = /etc/sudoers | sed -i '92a oracle ALL=(ALL) ALL' 报错: sudo: no tty present and no askpass program specified 解决: 参照:http://blog.51cto.com/nosmoking/1595241 1. 注释Defaults requiretty Defaults requiretty修改为 #Defaults requiretty, 表示不需要控制终端。没有就不用注释 否则会出现sudo: sorry, you must have a tty to run sudo 2. Defaults !visiblepw 改成 Defaults visiblepw 否则会出现 sudo: no tty present and no askpass program specified grep -n "Defaults" /etc/sudoers && sed -i '55 s/!//g' /etc/sudoers 参照文档 http://www.linuxdiyf.com/linux/18381.html https://www.cnblogs.com/clnchanpin/p/6898340.html