十七周三次課
阿新 • • 發佈:2018-07-13
tab syn cfg 系統 from inux frame tar 就是 20.27 分發系統介紹
腳本裏的interact停留在遠程的機器上不需要退出來,如果不加interact就會退出來
執行腳本之前需要加上權限
- 分發系統-expect講解(也就是一個分發的腳本)
- 場景:
- 業務越來越大,網站app,後端,編程語言是php,所以就需要配置lamp或者lnmp,最好還需要吧代碼上傳到服務器上;但是因為業務增加,代碼增加,多臺機器,就會非常麻煩;這是只需要一個分發系統,就可以把每次更新的代碼發布到需要更新的服務器上
- expect,是一種腳本語言;通過他可以實現傳輸,輸入命令(上線代碼)
-
首先要準備一臺模板機器,機器的IP,對應用戶的密碼,通過rsync同步代碼,還可以通過expect去執行某些命令
20.28 expect腳本遠程登錄
首先檢查下機器裏面有沒有安裝expect,如果系統沒有安裝expect:
yum install -y expect
自動遠程登錄# vim expect1.expect
#! /usr/bin/expect
set host "172.16.22.221"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
expect eof ##加這個停2s後退出
interact ##如果expect eof換成interact則不退出
腳本裏的interact停留在遠程的機器上不需要退出來,如果不加interact就會退出來
執行腳本之前需要加上權限
chmod a+x expect1.expect
./expect1.expect
20.29 expect腳本遠程執行命令
- 剛才我們的第一個腳本,只是登錄,
- 現在我們來寫第二個腳本,不僅僅要遠程登錄,還要執行一條命令,最後再退出出來
- 自動遠程登錄後,執行命令並退出
腳本內容[root@aiker script]# vim expect2.expect
#!/usr/bin/expect
set user "root"
set passwd "123456"
set host "localhost"
spawn ssh -p 12220 $user@$host
expect {
"yes/no"{send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/expect2.txt\r"
expect "]*"
send "echo 1232123 > /tmp/expect2.txt\r"
expect "]*"
send "exit\r"
**給腳本添加執行權限**
[root@aiker script]# chmod +x !$
chmod +x expect2.expect
**運行腳本**
[root@aiker script]# ./expect2.expect
spawn ssh -p 12220 root@localhost
Last login: Thu May 3 19:55:18 2018 from localhost
[root@test220 ~]# touch /tmp/expect2.txt
[root@test220 ~]# echo 1232123 > /tmp/expect2.txt
[root@test220 ~]# ll /tmp/expect2.txt
-rw-r--r-- 1 root root 8 May 4 01:28 /tmp/expect2.txt
[root@test220 ~]# cat !$
cat /tmp/expect2.txt
1232123
# 20.30 expect腳本傳遞參數
[root@aiker script]# vim expect3.expect
#!/usr/bin/expect
set user [lindex $argv 0] ##lindex用於傳遞參數;$argv 0參數0
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh -p 12220 $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
## 執行命令
**單個命令**
[root@aiker script]# ./expect3.expect root localhost "w"
spawn ssh -p 12220 root@localhost
Last login: Fri May 4 01:43:31 2018 from localhost
[root@test220 ~]# w
01:44:25 up 48 days, 5:40, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 localhost 01:44 1.00s 0.02s 0.00s w
**多個命令:使用“”,並使用;隔開**
[root@aiker script]# ./expect3.expect root localhost "w;ls;uname -a;ifconfig"
spawn ssh -p 12220 root@localhost
Last login: Fri May 4 01:44:15 2018 from localhost
[root@test220 ~]# w;ls;uname -a;ifconfig
01:47:05 up 48 days, 5:43, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 localhost 01:46 1.00s 0.02s 0.00s w
anaconda-ks.cfg catlina.txt done logs script tst.txt
apache-tomcat-8.5.29.tar.gz do iptables_bak.txt MariaDB.repo sed.sh tz.php
Linux test220 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.16.22.220 netmask 255.255.252.0 broadcast 172.16.23.255
inet6 fe80::250:56ff:fe9d:698c prefixlen 64 scopeid 0x20<link>
ether 00:50:56:9d:69:8c txqueuelen 1000 (Ethernet)
RX packets 12928347 bytes 3940610403 (3.6 GiB)
RX errors 73 dropped 3256 overruns 0 frame 0
TX packets 7095504 bytes 2055520014 (1.9 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 00:50:56:9d:6e:47 txqueuelen 1000 (Ethernet)
RX packets 9925 bytes 1006710 (983.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9123 bytes 804342 (785.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 21713721 bytes 15094753429 (14.0 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21713721 bytes 15094753429 (14.0 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
十七周三次課