日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行

發布時間:2025/7/14 windows 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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]#

- 安裝好expect包之后就可以去寫expect腳本了, - 自動遠程登錄后

[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

- 登錄192.168.202.132 也就是aming-02機器 - 第一次登錄會提示是否要繼續登錄,因為這個機器,在本機的known_host文件里面不存在,登錄一個陌生的機器,會提示你一下,是否要建立連接 - 這個文件是就保證登錄信息的,清空的話,重新遠程登錄ssh 會有提示是否要建立連接 /root/.ssh/known_hosts

[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 ~]#

- 就等登錄一臺機器,這是第一個expect腳本 - 登錄機器之后還要退出來,執行一條命令再退出來,當然可以# 20.29 expect腳本遠程執行命令 - 剛才我們的第一個腳本,只是登錄, - 現在我們來寫第二個腳本,不僅僅要遠程登錄,還要執行一條命令,最后再退出出來 - 自動遠程登錄后,執行命令并退出

[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]#

- 現在到aming-02上面去

[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 ~]#

- 看下有沒有12.txt

[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

- 下面來執行下這個腳本 - ./3.expect root 192.168.202.132 ls 執行腳本 用戶名 主機名 執行的命令

[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]#

- 先登錄這個機器,然后執行ls命令 - 假如我想執行多條命令ls;w;vmstat 1; 最后退出出來,需要用雙引號引起來 - expect有一個默認超時時間,所以這里使用vmstat 1 不合適

[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]#

- 再去另一臺機器上看下vmstat 1 這個命令有沒有繼續在運行

[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 ~]#

- 沒有了,因為之前vmstat 1 在一個終端上執行得,最后那個終端都退出來了,相當于我們中斷了,ctrl c了。 版權聲明:原創作品,如需轉載,請注明出處。否則將追究法律責任 本文轉自 ? ch71smas ?51CTO博客,原文鏈接:http://blog.51cto.com/ch71smas/2045765

總結

以上是生活随笔為你收集整理的20.27 分发系统介绍 20.28 expect脚本远程登录 20.29 expect脚本远程执行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。