一、分發系統介紹:expect,也就是一個分發腳本:php
場景:業務會愈來愈大,網站app,後端,服務端所使用的編程語言爲php,要想運行這個環境,就須要配置LAMP或者LNMP,最後還須要把代碼上傳到服務器上去,業務不斷的增長,一兩臺機器還好好說,要是幾十臺,上百臺設備,這時候就須要用到了分發系統了,把天天更新的代碼發佈到每一臺機器上去:shell
expect是一種腳本語言,經過它能夠實現傳輸,上線代碼,能夠實現遠程執行命令,不須要輸入密碼:編程
首先準備一臺模板機器(最新的代碼待上線),而後給下面分發代碼的多臺臺機器上線,機器的IP,對應用戶的密碼,然經過expect,藉助rsync來上傳代碼,能夠用expect去登陸執行某些命令:vim
1:expect腳本安裝:後端
yum install -y expect (安裝包和mkpasswd是一塊兒的)安全
示例1:自動遠程登陸,並執行一些命令:bash
[root@localhost_01 sbin]# vim 1.expect #! /usr/bin/expect set host "192.168.149.129" set passwd "nihao123!" set port "-p 56888" spawn ssh $port root@$host expect { "yes/no" { send "yes\r"; exp_continue} "password:" { send "$passwd\r" } } ##expect eof 表示登陸遠程機器後,暫停一兩秒鐘後退出: interact #腳本結束
註釋:在expect中定義變量須要set host才能夠:服務器
註釋:在文件裏 expect { }這裏是保證登陸信息,第一次登陸時候,則須要yes記錄登陸信息,下一次則不須要了,須要清空 >/root/.ssh/known_hosts才能夠:app
exp_continue表示繼續/r換行, interact表示繼續停留在這臺機器不退出:若是不加的話則登陸後立刻退出:ssh
[root@localhost_01 sbin]# ssh -p 56888 192.168.149.129 root@192.168.149.129's password: #由於咱們之前登陸過,全部不詢問爲了安全性,是否須要還繼續連接: [root@localhost_01 sbin]# > /root/.ssh/ authorized_keys authorized_keys_xshell id_rsa.pub authorized_keys.bakup id_rsa known_hosts [root@localhost_01 sbin]# > /root/.ssh/known_hosts #清空這個文件裏面的信息: [root@localhost_01 sbin]# ssh -p 56888 192.168.149.129 #再次登陸時則會詢問: The authenticity of host '[192.168.149.129]:56888 ([192.168.149.129]:56888)' can't be established. ECDSA key fingerprint is SHA256:mtOEV1y+2ErXFWqQRL0rYCQGGuVE7z4n1is+2dPQc7E. ECDSA key fingerprint is MD5:d9:f7:03:de:c4:f5:40:89:be:3c:25:6d:70:6a:49:a5. Are you sure you want to continue connecting (yes/no)? yes
加入執行權限後成功登陸: chmod a+x 1.expect
[root@localhost_01 sbin]# ./1.expect -bash: ./1.expect: 權限不夠 [root@localhost_01 sbin]# chmod a+x 1.expect [root@localhost_01 sbin]# ./1.expect spawn ssh -p 56888 root@192.168.149.129 The authenticity of host '[192.168.149.129]:56888 ([192.168.149.129]:56888)' can't be established. ECDSA key fingerprint is SHA256:mtOEV1y+2ErXFWqQRL0rYCQGGuVE7z4n1is+2dPQc7E. ECDSA key fingerprint is MD5:d9:f7:03:de:c4:f5:40:89:be:3c:25:6d:70:6a:49:a5. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[192.168.149.129]:56888' (ECDSA) to the list of known hosts. root@192.168.149.129's password: Last login: Thu Oct 4 00:33:35 2018 from 192.168.149.135 [root@localhost_02 ~]#
2:expect遠程執行命令:
遠程執行完命令,並退出:
[root@localhost_01 sbin]# vim 2.expect #!/usr/bin/expect set user "root" set passwd "nihao123!" set port "-p 56888" spawn ssh $port $user@192.168.180.135 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"
註釋:expect "]*" 星號表示通配,匹配右邊的全部字符(無論是root用戶普通用戶):
註釋:send 表示執行這個命令:
3:須要加執行權限並執行這個命令:
[root@localhost_01 sbin]# chmod a+x 2.expect [root@localhost_01 sbin]# ll 2.expect -rwxr-xr-x 1 root root 307 10月 4 16:09 2.expect [root@localhost_01 sbin]# ./2.expect spawn ssh -p 56888 root@192.168.149.129 root@192.168.149.129's password: Last login: Thu Oct 4 06:00:22 2018 from 192.168.149.130 [root@localhost_02 ~]# touch /tmp/12.txt [root@localhost_02 ~]# echo 1212 > /tmp/12.txt
而後在129這臺機器上查看是否建立成功:
[root@localhost_02 ~]# ls /tmp/12.txt /tmp/12.txt [root@localhost_02 ~]# cat /tmp/12.txt 1212
四、expect腳本傳遞參數:
[root@localhost_01 sbin]# vim 3.expect [root@localhost_01 sbin]# cat 3.expect #!/usr/bin/expect set port [lindex $argv 0] #第一個參數,若是端口仍是默認的22,則不須要定義次參數,依次往下定義便可: set user [lindex $argv 1] #第二個參數 set host [lindex $argv 2] #第三個參數 set passwd "nihao123!" set cm [lindex $argv 3] #第四參數 spawn ssh -p $port $user@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r"
1:增長執行權限: chmod +x 3.expect
[root@localhost_01 sbin]# chmod a+x 3.expect [root@localhost_01 sbin]# ll 3.expect -rwxr-xr-x 1 root root 369 10月 4 16:37 3.expect
2:執行此腳本:
./3.expect 第一個參數(port) 第二個參數(user) 第三個參數(host) 第四個參數(command)
[root@localhost_01 sbin]# ./3.expect 56888 root 192.168.149.129 ls spawn ssh -p 56888 root@192.168.149.129 root@192.168.149.129's password: Last login: Thu Oct 4 06:24:37 2018 from 192.168.149.130 [root@localhost_02 ~]# ls 1 anaconda-ks.cfg bash CentOS7-Base-163.repo link shell test test.txt
一樣,也支持多條參數: 多條命令最後用雙引號括住,而後用分號分割則能夠:
[root@localhost_01 sbin]# ./3.expect 56888 root 192.168.149.129 "ls;w" spawn ssh -p 56888 root@192.168.149.129 root@192.168.149.129's password: Last login: Thu Oct 4 06:32:46 2018 from 192.168.149.130 [root@localhost_02 ~]# ls;w 1 anaconda-ks.cfg bash CentOS7-Base-163.repo link shell test test.txt 06:32:57 up 19:42, 2 users, load average: 0.42, 0.23, 0.16 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.149.130 06:32 1.00s 0.02s 0.01s w root pts/1 192.168.149.135 00:33 26:57 0.03s 0.03s -bash
註釋:若是是執行多條命令的話,也能夠傳遞多個參數,若是要是把多個命令當成一個參數來傳遞的話,則須要用雙引號引發來,而且用分號分割才能夠:
一樣:當咱們傳遞第四個參數時: vmstat模式超時時間是10秒:
[root@localhost_01 sbin]# ./3.expect 56888 root 192.168.149.129 "w;vmstat 1" spawn ssh -p 56888 root@192.168.149.129 root@192.168.149.129's password: Last failed login: Thu Oct 4 06:56:10 CST 2018 from 192.168.149.130 on ssh:notty There were 2 failed login attempts since the last successful login. Last login: Thu Oct 4 06:37:26 2018 from 192.168.149.130 [root@localhost_02 ~]# w;vmstat 1 07:07:33 up 20:17, 2 users, load average: 0.01, 0.19, 0.20 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.149.130 07:07 0.00s 0.03s 0.03s w root pts/1 192.168.149.135 00:33 1:01m 0.03s 0.03s -bash 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 1026092 2076 240768 0 0 3 42 61 101 0 0 99 1 0 0 0 0 1026108 2076 240820 0 0 0 0 37 78 0 1 99 0 0 0 0 0 1026108 2076 240820 0 0 0 0 42 84 0 0 100 0 0 0 0 0 1026108 2076 240820 0 0 0 0 42 82 0 0 100 0 0 0 0 0 1026108 2076 240820 0 0 0 10 54 90 0 0 100 0 0 0 0 0 1026108 2076 240820 0 0 0 0 37 78 0 0 100 0 0 0 0 0 1026108 2076 240820 0 0 0 103 43 104 0 1 99 0 0 0 0 0 1026108 2076 240820 0 0 0 0 45 86 0 0 100 0 0 0 0 0 1026108 2076 240820 0 0 0 0 37 74 0 0 100 0 0 0 0 0 1026108 2076 240820 0 0 0 0 34 72 0 0 100 0 0 0 0 0 1026108 2076 240820 0 0 0 0 33 70 0 0 100 0 0 0 0 0 1026108 2076 240820 0 0 0 0 35 80 0 0 100 0 0 十秒後自動超時:
註釋:也能夠設置永不超時: 須要在執行命令(也就是第四個參數)後面添加 set timeout -1
expect "]*" send "$cm\r" #set timeout -1 #表示永不超時: #set timeout 5 #也能夠手動指定具體的秒數: 本次5ms: expect "]*" send "exit\r"