20.27 分發系統介紹 20.28 expect腳本遠程登陸 20.29 expect腳本遠程執行命令 20.30 expect腳本傳遞參數

20.27分發系統介紹 分發系統,用在什麼場景 php

例如公司業務作的愈來愈大,公司的網站或App,它們的後端和服務端所用的編程語言爲PHP,因此說要想運行這個環境,PHP的代碼,須要配置LAMP或LNMP的環境,還須要把代碼上傳到服務器上去,說白了就是一個網站. 在日常的工做中,業務不斷在迭代,有新的功能出現,那麼這時候就須要去更改代碼,如果一兩臺機器的話還好一點,能夠直接在服務器上更改,可是這樣的話不規範. 如果十幾臺甚至是上百臺機器的話,存貯都是一站點,好比說有一個接口,App訪問量很大,App須要調用服務端的接口,那服務器接口有50臺機器去承載,這時候就須要弄一個分發系統,可以讓咱們把天天或每段時間更新的代碼,分別的發佈到這50臺機器上去.shell


用shell 編程可以實現上線的工具 編程

說明:所說的分發系統,也就是上線的shell腳本vim

 

核心的東西叫作expect, expect也能夠說是一種腳本語言,和shell很是的像,咱們能夠用expect去實現傳輸文件;還能夠實現遠程執行命令,不須要咱們去輸入密碼; 所謂上線代碼,就是把開發的語言or代碼發佈到線上環境上去. 在這裏的分發系統裏,首先要準備一臺模版的機器,這臺機器上的代碼是最新的代碼,是準備要上線的代碼. 再就是例如要給50臺機器上線,得知道這50臺機器的IP地址,還有這50臺機器對應的用戶密碼也得知道. 最後就是用expect腳本,藉助於rsnc把這些代碼推送到這50臺機器.後端

20.28expect腳本遠程登陸 
yum安裝expect 
[[email protected] ~]# yum install -y expect 寫expect腳本--自動遠程登陸 
#自動遠程登陸腳本
[[email protected] test]# vim 1.expect

#! /usr/bin/expect
#host 是變量,後面跟登陸的IP
set host "192.168.2.116"
#passwd是變量,後面跟IP的密碼
set passwd "123456" 
#登陸語句 
spawn ssh [email protected]$host
#當首次登陸的時候,會提示是否要繼續登陸 
expect {
"yes/no" {send"yes/r";exp_continue}
"password:"{send "$passwd/r"}
}
interact註釋:/r -->表示後回車的意思
interact -->表示須要停留在遠程的機器上,若是不加這個參數那麼就會退出遠程的腳本
或者使用expect eof --> 表示登陸到遠程機器上,會停留一兩秒鐘,而後自動退出來# 添加執行權限
[[email protected] test]# chmod a+x 1.expect
#執行
[[email protected] test]# ./1.expect 
spawn ssh [email protected]
The authenticity of host '192.168.2.116 (192.168.2.116)' can't be established.
ECDSA key fingerprint is bd:fd:56:cf:07:80:0d:a6:9c:38:00:be:33:1f:f5:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.116' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Wed Sep 20 10:25:06 2017 from 192.168.2.102
[[email protected] ~]# 
說明:成功登陸到192.168.2.116機器 
20.29expect腳本遠程執行命令 
自動遠程登陸後--執行命令並退出 
[[email protected] test]# vim 2.expect
#! /usr/bin/expect
#user是變量
set user "root"
#passwd是變量,後面跟user的密碼
set passwd "123456"
#登陸語句
spawn ssh [email protected]
#當首次登陸的時候,會提示是否要繼續登陸 
expect {
"yes/no" {send "yes/r"; exp_continue}
"password:" {send "$passwd/r"}
}
expect "]*"
send "touch /tmp/66.txt/r"
expect "]*"
send "echo This is test > /tmp/66.txt/r"
expect "]*"
send "exit/r"註釋: expect "]*" 當root用戶登陸的時候 後半個括號後面跟的是# ([[email protected] ~]#)
普通用戶登陸的時候,後半個括號後面跟的是~.
expect "]*" 這裏用*號,表示通配,無論是root用戶仍是普通用戶,都將執行下面的命令#添加執行權限
[[email protected] test]# chmod a+x 2.expect 
#執行
[[email protected] test]# ./2.expect 
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 10:30:43 2017 from 192.168.2.115
[[email protected] ~]# touch /tmp/66.txt
[[email protected] ~]# echo This is test > /tmp/66.txt
[[email protected] ~]# [[email protected] test]# 
說明:登陸到root-02 執行了兩條命令,而後退出來#能夠執行1.expect登陸到root-02,而後查看一下是否建立了66.txt
[[email protected] test]# ./1.expect 
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 11:08:09 2017 from 192.168.2.115
[[email protected] ~]# ls /tmp/66.txt 
/tmp/66.txt
[[email protected] ~]# cat /tmp/66.txt 
This is test
說明:root-02 /tmp/下有66.txt,而且也有「This is test」,說明執行成功了. 
20.30expect腳本傳遞參數 
傳遞參數 
[[email protected] test]# vim 3.expect
#! /usr/bin/expect
#user第一個參數
set user [lindex $argv 0]
#host第二個參數
set host [lindex $argv 1]
set passwd "123456"
#cm第三個參數,也就是要執行的命令
set cm [lindex $argv 2]
spawn ssh [email protected]
expect {
"yes/no" {send "yes/r"}
"password:" {send "$passwd/r"}
}
expect "]*"
send "$cm/r"
expect "]*"
send "exit/r"#添加執行權限
[[email protected] test]# chmod a+x 3.expect #執行
[[email protected] test]# ./3.expect root 192.168.2.116 ls
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 11:12:05 2017 from 192.168.2.115
[[email protected] ~]# ls
anaconda-ks.cfg
[[email protected] ~]# [[email protected] test]# 
說明:root是第一個參數; 192.168.2.116是第二個參數; ls是第三個參數;#若須要執行多條命令的時候,須要用冒號以及分號,把命令括起來
[[email protected] test]# ./3.expect root 192.168.2.116 "ls;touch 1.txt;echo "this is test" > 1.txt"
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 11:39:34 2017 from 192.168.2.115
[[email protected] ~]# ls;touch 1.txt;echo this
1.txtanaconda-ks.cfg
this
說明:touch 1.txt,並寫入this is test
#執行1.expect登陸root-02,查看一下是否touch 1.txt 以及寫入的內容
[[email protected] test]# ./1.expect
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Sep 20 11:40:25 2017 from 192.168.2.115
[[email protected] ~]# cat 1.txt 
this is test
[[email protected] ~]# ls
1.txtanaconda-ks.cfg
說明:很明顯執行成功了服務器

以上是雲棲社區小編爲您精心準備的的內容,在雲棲社區的博客、問答、公衆號、人物、課程等欄目也有的相關內容,歡迎繼續使用右上角搜索按鈕進行搜索登陸 , 腳本 , 命令 , 參數 , 遠程 , 傳遞 , expect , 分發 , 執行 介紹 ,以便於您獲取更多的相關知識。ssh

相關文章
相關標籤/搜索