2018-4-25 18周1次課 分發系統-expect講解(上)
由於業務叠代更新,需要更改代碼,如果機器很多,那麽久需要一個分發系統,可以把每段時間更新的代碼分別發布到機器上去
分發系統就是上線的shell腳本,核心為expect
expect是一種腳本語言,和shell很像,可以用它去實現傳輸文件和遠程執行命令,不需要去輸入密碼
20.28 expect腳本遠程登錄
[root@localhost ~]# yum install -y expect
(過程省略)
·自動遠程登錄
expect中定義變量和shell中的區別
shell中是:變量=xxx
expect中是:set 變量 "xxx"
spawn ssh root@$host 登錄的語句
yes/no 是指第一次遠程登錄時,會提示是否連接,如下圖
當提示為 yes/no 時,怎麽做。send "yes\r"; exp_continue
當提示為 password 時,怎麽做。 send "$passwd\r" (填寫關鍵詞接口,也可以是assword)
interact 表示結束,停留在遠程的機器上(如果不加這一行,那麽登錄後馬上回退出)
(如果不用interact,而是expect eof ,那麽會停留在機器上幾秒,然後退出)
[root@localhost sbin]# chmod a+x 1.expect ##給執行權限 [root@localhost sbin]# ./1.expect spawn ssh [email protected] The authenticity of host '192.168.65.129 (192.168.65.129)' can't be established. ECDSA key fingerprint is SHA256:PXl0tJsOm3464x52jTxX7L+ToKm1Hb6AUMq6lh4ASX0. ECDSA key fingerprint is MD5:2e:9a:f9:25:b3:80:43:05:a4:3d:3c:9b:48:6e:a0:0e. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.65.129' (ECDSA) to the list of known hosts. [email protected]'s password: Last login: Tue Apr 24 22:19:31 2018 from 192.168.65.1 [root@localhost ~]# ifconfig
已經成功登錄到了129這臺機器
20.29 expect腳本遠程執行命令
·自動遠程登陸後,執行命令並退出
前一半和之前的登錄腳本基本一致,只不過這次set的變量為user
expect "]*" ##遇到 "]*" ,則執行 send "touch /tmp/12.txt\r"
expect "]*" ##再遇到 "]*" ,則 send "echo 1212 > /tmp/12.txt\r"
expect "]*" ##然後 send "exit\r"
因為命令行在未執行命令時都是 ]# 或 ]$ 再最後,因此,這樣可以一步一步地執行命令
[root@localhost sbin]# chmod a+x 2.expect [root@localhost sbin]# ./2.expect spawn ssh [email protected] [email protected]'s password: Last login: Tue Apr 24 22:31:51 2018 from 192.168.65.128 [root@localhost ~]# touch /tmp/12.txt [root@localhost ~]# echo 1212 > /tmp/12.txt [root@localhost ~]# [root@localhost sbin]# ssh 192.168.65.129 [email protected]'s password: Last login: Tue Apr 24 22:56:18 2018 from 192.168.65.128 [root@localhost ~]# ll /tmp/12.txt ##在129上可以看到文本存在 -rw-r--r-- 1 root root 5 4月 24 22:56 /tmp/12.txt [root@localhost ~]# cat /tmp/12.txt ##內容為1212 1212
20.30 expect腳本傳遞參數
·傳遞參數
set user [lindex $argv 0],set host [lindex $argv 1],set cm [lindex $argv 2]分別表示第1,2,3個參數
設置用戶名,主機,命令(cm)
執行過程和之前腳本一致,只不過在執行命令時,需要向在命令行寫出3個參數傳遞到腳本中
[root@localhost sbin]# chmod a+x 3.expect [root@localhost sbin]# ./3.expect root 192.168.65.129 ls spawn ssh [email protected] [email protected]'s password: Last login: Tue Apr 24 22:57:58 2018 from 192.168.65.128 [root@localhost ~]# ls 1.txt aminglinux a.txt b.txt sed test test1.txt test2.txt test3.txt x.txt 2.txt anaconda-ks.cfg awk grep ssl.conf test1 test2 test3 test.txt zabbix-release-3.2-1.el7.noarch.rpm
(root為參數1,ip為參數2,命令ls為參數3)
如果想要傳遞多個命令,需要用雙引號括起來,並且命令之間用分號隔開
[root@localhost sbin]# ./3.expect root 192.168.65.129 "ls;w;vmstat 1" spawn ssh [email protected] [email protected]'s password: Last login: Tue Apr 24 23:22:08 2018 from 192.168.65.128 [root@localhost ~]# ls;w;vmstat 1 1.txt aminglinux a.txt b.txt sed test test1.txt test2.txt test3.txt x.txt 2.txt anaconda-ks.cfg awk grep ssl.conf test1 test2 test3 test.txt zabbix-release-3.2-1.el7.noarch.rpm 23:22:30 up 1:04, 2 users, load average: 0.02, 0.03, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.65.1 22:19 50:30 0.04s 0.04s -bash root pts/1 192.168.65.128 23:22 0.00s 0.03s 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 2 0 0 187692 2076 203060 0 0 44 68 129 160 0 1 99 0 0 1 0 0 187584 2076 203060 0 0 0 8 137 156 0 0 100 0 0 0 0 0 187584 2076 203060 0 0 0 548 123 142 0 1 99 0 0 0 0 0 187584 2076 203060 0 0 0 5 142 161 0 0 100 0 0 0 0 0 187584 2076 203060 0 0 0 104 121 151 0 0 100 0 0 0 0 0 187584 2076 203060 0 0 0 11 136 167 0 0 100 0 0 0 0 0 187584 2076 203060 0 0 0 6 124 144 0 0 100 0 0 0 0 0 187336 2076 203060 0 0 0 12 159 178 0 1 98 1 0 0 0 0 187368 2076 203060 0 0 0 196 141 165 0 0 100 0 0 0 0 0 187368 2076 203060 0 0 0 5 159 173 1 1 98 0 0
(因為expect有超時時間,所有10秒後會退出)
2018-4-25 18周1次課 分發系統-expect講解(上)