20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行
- 20.27 分發系統介紹
- 20.28 expect腳本遠程登錄
- 20.29 expect腳本遠程執行命令
- 20.30 expect腳本傳遞參數
20.27 分發系統介紹
- 分發系統,什么叫分發系統,首先說一個場景,以后在工作中會遇到的場景
- 場景:業務越來越大,網站app,后端,編程語言是php,所以就需要配置lamp或者lnmp,最后還需要把你們的代碼上傳到服務器上;但是在平時工作中,因為業務增加,代碼增加,一臺機器還好點,甚至在服務器上直接改,但是這樣不規范,多臺機器,就會非常麻煩;這是只需要一個分發系統,就可以把每次更新的代碼發布到需要更新的服務器上去。
- 核心的東西是expect,是一種腳本語言;通過他可以實現傳輸文件,還可以實現遠程輸入命令(上線代碼),不需要輸入密碼
- 首先要準備一臺模板機器這臺機器上代碼是最新的代碼,上線的代碼,機器的IP,對應用戶的密碼,通過rsync同步代碼,還可以通過expect去執行某些命令,這樣的一個過程
20.28 expect腳本遠程登錄
- 首先檢查下機器里面有沒有安裝expect
- 如果沒有需要安裝下,因為當中有epel源(國外源,所以下載會很慢)解決辦法:修改epel.repo文件名,改為epel.repo1 再yum install -y expect 即可。[root@aming-01 sbin]# cd /etc/yum.repos.d/ [root@aming-01 yum.repos.d]# ls CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo zabbix.repo CentOS-CR.repo CentOS-Media.repo epel.repo CentOS-Debuginfo.repo CentOS-Sources.repo epel-testing.repo [root@aming-01 yum.repos.d]# mv epel.repo epel.repo1
- 安裝expect包 [root@aming-01 yum.repos.d]# cd /usr/local/sbin [root@aming-01 sbin]# yum install -y expect 已加載插件:fastestmirror Determining fastest mirrors * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.163.com 正在解決依賴關系 正在檢查事務 軟件包 expect.x86_64.0.5.45-14.el7_1 將被 安裝 正在處理依賴關系 libtcl8.5.so()(64bit),它被軟件包 expect-5.45-14.el7_1.x86_64 需要 正在檢查事務 軟件包 tcl.x86_64.1.8.5.13-8.el7 將被 安裝 解決依賴關系完成
依賴關系解決
==========================================================================================
Package 架構 版本 源 大小
正在安裝:
expect x86_64 5.45-14.el7_1 base 262 k
為依賴而安裝:
tcl x86_64 1:8.5.13-8.el7 base 1.9 M
事務概要
安裝 1 軟件包 (+1 依賴軟件包)
總下載量:2.1 M
安裝大小:4.9 M
Downloading packages:
(1/2): expect-5.45-14.el7_1.x86_64.rpm | 262 kB 00:00:02?
(2/2): tcl-8.5.13-8.el7.x86_64.rpm | 1.9 MB 00:00:06
總計 324 kB/s | 2.1 MB 00:00:06?
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : 1:tcl-8.5.13-8.el7.x86_64 1/2?
正在安裝 : expect-5.45-14.el7_1.x86_64 2/2?
驗證中 : 1:tcl-8.5.13-8.el7.x86_64 1/2?
驗證中 : expect-5.45-14.el7_1.x86_64 2/2
已安裝:
expect.x86_64 0:5.45-14.el7_1
作為依賴被安裝:
tcl.x86_64 1:8.5.13-8.el7
完畢!
[root@aming-01 sbin]#
[root@aming-01 sbin]# vi 1.expect
#! /usr/bin/expect
set host "192.168.202.132"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact
~
~?
~?
:wq
[root@aming-01 sbin]# vi 1.expect
[root@aming-01 sbin]# ssh 192.168.202.132
The authenticity of host '192.168.202.132 (192.168.202.132)' can't be established.
ECDSA key fingerprint is 00:22:2a:58:d5:c3:79:dc:8b:d7:b1:91:5c:ec:27:83.
Are you sure you want to continue connecting (yes/no)? no
Host key verification failed.
[root@aming-01 sbin]#
[root@aming-01 sbin]# vi 1.expect
[root@aming-01 sbin]# ./1.expect
spawn ssh root@192.168.202.132
The authenticity of host '192.168.202.132 (192.168.202.132)' can't be established.
ECDSA key fingerprint is 00:22:2a:58:d5:c3:79:dc:8b:d7:b1:91:5c:ec:27:83.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.202.132' (ECDSA) to the list of known hosts.
root@192.168.202.132's password:?
Last login: Tue Nov 28 21:09:45 2017 from 192.168.202.1
[root@aming-02 ~]#
[root@aming-02 ~]# 登出
Connection to 192.168.202.132 closed.
[root@aming-01 sbin]#
[root@aming-01 sbin]# vi 2.expect
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.202.132
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"
~?
:wq
[root@aming-01 sbin]# chmod a+x 2.expect
[root@aming-01 sbin]# ./2.expect
spawn ssh root@192.168.202.132
root@192.168.202.132's password:?
Last login: Tue Nov 28 21:43:52 2017 from 192.168.202.130
[root@aming-02 ~]# touch /tmp/12.txt
[root@aming-02 ~]# echo 1212 > /tmp/12.txt
[root@aming-02 ~]# [root@aming-01 sbin]#
[root@aming-02 ~]# [root@aming-01 sbin]# ./1.expect
spawn ssh root@192.168.202.132
root@192.168.202.132's password: 123456
Last login: Tue Nov 28 22:09:17 2017 from 192.168.202.130
[root@aming-02 ~]#
[root@aming-02 ~]# ls -l /tmp/12.txt
-rw-r--r-- 1 root root 5 11月 28 22:09 /tmp/12.txt
[root@aming-02 ~]#?
[root@aming-02 ~]# cat /tmp/12.txt
1212
[root@aming-02 ~]#
[root@aming-02 ~]# logout
Connection to 192.168.202.132 closed.
[root@aming-01 sbin]# cat 1.expect
#! /usr/bin/expect
set host "192.168.202.132"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no:" { send "yes\r"; exp_continue}
}
interact
[root@aming-01 sbin]# cat 2.expect
#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.202.132
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"
[root@aming-01 sbin]#
- 腳本里的interact停留在遠程的機器上不需要退出來,如果不加interact就會退出來# 20.30 expect腳本傳遞參數 - 傳遞參數[root@aming-01 sbin]# vi 3.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
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"
~
~?
~?
:wq
[root@aming-01 sbin]# vi 3.expect
[root@aming-01 sbin]# chmod a+x 3.expect
[root@aming-01 sbin]# ./3.expect root 192.168.202.132 ls
spawn ssh root@192.168.202.132
root@192.168.202.132's password:?
Last login: Tue Nov 28 22:15:08 2017 from 192.168.202.130
[root@aming-02 ~]# ls
anaconda-ks.cfg zabbix-release-3.2-1.el7.noarch.rpm
[root@aming-02 ~]# [root@aming-01 sbin]#
[root@aming-02 ~]# [root@aming-01 sbin]# ./3.expect root 192.168.202.132 "ls;w;vmstat 1"
spawn ssh root@192.168.202.132
root@192.168.202.132's password:?
Last login: Tue Nov 28 22:31:05 2017 from 192.168.202.130
[root@aming-02 ~]# ls;w;vmstat 1
anaconda-ks.cfg zabbix-release-3.2-1.el7.noarch.rpm
22:34:10 up 1:26, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 21:09 1:24m 0.01s 0.01s -bash
root pts/0 192.168.202.1 21:09 1:24m 0.01s 0.01s -bash
root pts/1 192.168.202.130 22:34 0.00s 0.01s 0.01s 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 284016 876 169392 0 0 27 3 65 134 0 0 99 0 0
0 0 0 284016 876 169392 0 0 0 0 60 123 0 0 100 0 0
0 0 0 284016 876 169392 0 0 0 0 69 135 0 1 99 0 0
0 0 0 284016 876 169392 0 0 0 0 62 117 0 0 100 0 0
0 0 0 284016 876 169392 0 0 0 0 54 110 1 0 99 0 0
0 0 0 284016 876 169392 0 0 0 0 60 118 0 1 99 0 0
0 0 0 284016 876 169392 0 0 0 0 49 106 0 0 100 0 0
0 0 0 284016 876 169392 0 0 0 0 55 114 0 0 100 0 0
0 0 0 284016 876 169392 0 0 0 0 56 114 0 0 100 0 0
2 0 0 284016 876 169392 0 0 0 0 54 111 0 0 100 0 0
[root@aming-01 sbin]#
[root@aming-01 sbin]# ./1.expect
spawn ssh root@192.168.202.132
root@192.168.202.132's password: 123456
Last login: Tue Nov 28 22:34:10 2017 from 192.168.202.130
[root@aming-02 ~]# ps aux |grep vmstat
root 3771 0.0 0.0 112680 976 pts/1 R+ 22:36 0:00 grep --color=auto vmsta
[root@aming-02 ~]#
總結
以上是生活随笔為你收集整理的20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AI 学习之路——轻松初探 Python
- 下一篇: 30.IntellJ Idea 导入已存