应用场景shell
当业务越作越大,服务器需求愈来愈多,几台服务器的话还好一点;当十几、几十台的时候,工做量就很是大!而且不规范,须要一个模板机分发到各个机器上去。vim
能够用开源的软件,expect脚本语言,进行实现分发系统的功能。bash
yum install -y expect服务器
#!/usr/bin/expect set host "192.168.21.130" set passwd "rootroot" spawn ssh root@$host expect { "yes/no" {send "yes\r"; exp_continue} "assword:" {send "$passwd\r"} } interact #表示停留在机器上 #若是须要退出 能够expect eof
执行操做ssh
[root@qingyun-02 sbin]# vim 1.expect #清空远程登陆的记录 [root@qingyun-02 sbin]# vi /root/.ssh/known_hosts #增长执行权限 [root@qingyun-02 sbin]# chmod a+x 1.expect #执行 [root@qingyun-02 sbin]# ./1.expect spawn ssh root@192.168.21.130 The authenticity of host '192.168.21.130 (192.168.21.130)' can't be established. ECDSA key fingerprint is SHA256:e6Fx3oJ8GcMbFnmTV7JIcvZ3sG6W6yrfvdKccXk+c7c. ECDSA key fingerprint is MD5:15:57:6c:19:21:a2:e4:3e:df:19:27:13:c2:2e:8e:ba. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.21.130' (ECDSA) to the list of known hosts. root@192.168.21.130's password: Last login: Wed Feb 28 08:57:58 2018 from 192.168.21.1
#脚本代码 #!/usr/bin/expect set user "root" set passwd "rootroot" spawn ssh $user@192.168.21.130 expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } } expect "]*" send "touch /tmp/12.txt\r" expect "]*" send "echo 1212 > /tmp/12.txt\r" expect "]*" send "exit\r"
执行ide
[root@qingyun-02 sbin]# chmod a+x 2.expect [root@qingyun-02 sbin]# ./2.expect spawn ssh root@192.168.21.130 root@192.168.21.130's password: Last login: Wed Feb 28 09:30:05 2018 from 192.168.21.132 [root@qingyun-01 ~]# touch /tmp/12.txt [root@qingyun-01 ~]# echo 1212 > /tmp/12.txt [root@qingyun-01 ~]# [root@qingyun-02 sbin]# #查看远程端机上结果 [root@qingyun-02 sbin]# ./1.expect spawn ssh root@192.168.21.130 root@192.168.21.130's password: Last login: Wed Feb 28 09:36:13 2018 from 192.168.21.132 [root@qingyun-01 ~]# ls /tmp/12.txt /tmp/12.txt [root@qingyun-01 ~]# cat /tmp/12.txt 1212
[root@qingyun-02 sbin]# vim 3.expect #!/usr/bin/expect set user [lindex $argv 0] set host [lindex $argv 1] set passwd "rootroot" set cm [lindex $argv 2] spawn ssh $user@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r"
执行spa
[root@qingyun-02 sbin]# chmod a+x 3.expect [root@qingyun-02 sbin]# ./3.expect root 192.168.21.130 ls spawn ssh root@192.168.21.130 root@192.168.21.130's password: Last login: Wed Feb 28 09:39:16 2018 from 192.168.21.132 [root@qingyun-01 ~]# ls anaconda-ks.cfg shell [root@qingyun-01 ~]# [root@qingyun-02 sbin]# [root@qingyun-02 sbin]# ./3.expect root 192.168.21.130 "ls;w;vmstat 1" #当有多个命令 须要用双引号 做为一个参数传进去 spawn ssh root@192.168.21.130 root@192.168.21.130's password: Last login: Wed Feb 28 09:46:40 2018 from 192.168.21.132 [root@qingyun-01 ~]# ls;w;vmstat 1 anaconda-ks.cfg shell 09:49:34 up 1:00, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.21.1 08:57 12:46 0.03s 0.03s -bash root pts/1 192.168.21.132 09:49 0.00s 0.00s 0.00s w procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 3 0 0 206004 2108 197024 0 0 46 11 93 134 0 0 99 1 0 0 0 0 206020 2108 197040 0 0 0 0 74 105 0 0 100 0 0 0 0 0 206020 2108 197040 0 0 0 0 76 100 0 0 100 0 0 0 0 0 206020 2108 197040 0 0 0 9 87 126 0 0 100 0 0 0 0 0 206020 2108 197040 0 0 0 0 75 101 0 0 100 0 0 0 0 0 206020 2108 197040 0 0 0 98 82 115 0 1 99 0 0 0 0 0 206020 2108 197040 0 0 0 5 89 121 0 0 100 0 0 0 0 0 206020 2108 197040 0 0 0 0 75 108 0 0 100 0 0 0 0 0 206012 2108 197048 0 0 0 0 92 119 1 1 98 0 0 0 0 0 206012 2108 197048 0 0 0 0 88 118 0 0 100 0 0 #expect会有超时时间,大概10s左右