應用場景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左右