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

分發系統介紹

  • 分發系統-expect講解(也就是一個分發的腳本)
  • 場景:
    • 業務愈來愈大,網站app,後端,編程語言是php,因此就須要配置lamp或者lnmp,最好還須要吧代碼上傳到服務器上;可是由於業務增長,代碼增長,多臺機器,就會很是麻煩;這是隻須要一個分發系統,就能夠把每次更新的代碼發佈到須要更新的服務器上
  • expect,是一種腳本語言;經過他能夠實現傳輸,輸入命令(上線代碼)
  • 首先要準備一臺模板機器,機器的IP,對應用戶的密碼,經過rsync同步代碼,還能夠經過expect去執行某些命令

expect腳本遠程登陸

  • yum install -y expectphp

  • 自動遠程登陸 vim 1.expect編程

[root@yong-01 sbin]# vim 1.expect

#! /usr/bin/expect
set host "192.168.180.135"
set passwd "20655739"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}  //
"password:" { send "$passwd\r" }
}
interact  //腳本結束
  • 在expect 定義變量,用set
  • 這個文件是就保證登陸信息的,清空的話,從新遠程登陸ssh 會有提示 /root/.ssh/known_hosts
    exp_continue 表示繼續 \r 表示換行 interact 繼續停留在這臺機器,不退出

加入執行權限vim

[root@yong-01 sbin]# ./1.expect
-bash: ./1.expect: 權限不夠
[root@yong-01 sbin]# chmod a+x 1.expect 
[root@yong-01 sbin]# ll 1.expect 
-rwxr-xr-x 1 root root 191 7月  19 22:45 1.expect
  • 成功登陸
[root@yong-01 sbin]# ./1.expect
spawn ssh root@192.168.180.135
The authenticity of host '192.168.180.135 (192.168.180.135)' can't be established.
ECDSA key fingerprint is SHA256:oYqovve1b2BwHDBYcFasCiiFzZTHJvKDbTGZAjmlMXc.
ECDSA key fingerprint is MD5:3d:f8:af:0d:85:48:db:2a:46:0e:68:5f:eb:43:3e:43.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.180.135' (ECDSA) to the list of known hosts.
Last login: Thu Jul 19 21:43:59 2018
[root@yong-02 ~]#

expect腳本遠程執行命令

  • 動遠程登陸後,執行命令並退出 vim 2.expect
[root@yong-01 sbin]# vim 2.expect

#!/usr/bin/expect
set user "root"
set passwd "20655739"
spawn ssh $user@192.168.180.135

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"
  • expect "]" *「」 通配]右邊全部字符,windows

  • send 執行命令後端

  • A 機器執行腳本bash

[root@yong-01 sbin]# ll 2.expect 
-rw-r--r-- 1 root root 283 7月  19 22:49 2.expect
[root@yong-01 sbin]# chmod a+x 2.expect 
[root@yong-01 sbin]# ./2.expect 
spawn ssh root@192.168.180.135
Last login: Thu Jul 19 22:45:41 2018 from 192.168.180.134
[root@yong-02 ~]# touch /tmp/12.txt
[root@yong-02 ~]# echo 1212 > /tmp/12.txt
[root@yong-02 ~]# [root@yong-01 sbin]#
  • B機器查看腳本運行的命令是否成功
[root@yong-02 ~]# ll /tmp/12.txt 
-rw-r--r-- 1 root root 5 7月  19 22:50 /tmp/12.txt
[root@yong-02 ~]# cat /tmp/12.txt 
1212

expect腳本傳遞參數

  • 傳遞參數 vim 3.expect 
root@yong-01 sbin]# vim 3.expect

#!/usr/bin/expect
set user [lindex $argv 0]   //第一個參數
set host [lindex $argv 1]   //第二個參數
set passwd "20655739"
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"
  • 加執行權限 chmod a+x 3.expect
  • ./3.expect [第一個參數] [第二個參數] [第三個參數]
[root@yong-01 sbin]# chmod a+x 3.expect 
[root@yong-01 sbin]# ./3.expect root 192.168.180.135 "ls;w"
spawn ssh root@192.168.180.135
Last login: Thu Jul 19 22:51:33 2018 from 192.168.180.134
[root@yong-02 ~]# ls;w
anaconda-ks.cfg  cn_windows_7_ultimate_with_sp1_x64_dvd_618537.iso  multi-user.target  rescue.target
a.txt            c.txt                                              poweroff.target    zabbix-release-3.2-1.el7.noarch.rpm
b.txt            graphical.target                                   reboot.target      這裏須要指定一個用戶名的密碼文件
 22:56:20 up 29 min,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.180.134  22:56    4.00s  0.03s  0.00s w
  • 支持多條參數
[root@yong-01 sbin]# ./3.expect root 192.168.180.135 "ls;vmstat 1"
spawn ssh root@192.168.180.135
Last login: Thu Jul 19 22:56:10 2018 from 192.168.180.134
[root@yong-02 ~]# ls;vmstat 1
anaconda-ks.cfg  cn_windows_7_ultimate_with_sp1_x64_dvd_618537.iso  multi-user.target  rescue.target
a.txt            c.txt                                              poweroff.target    zabbix-release-3.2-1.el7.noarch.rpm
b.txt            graphical.target                                   reboot.target      這裏須要指定一個用戶名的密碼文件
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 1119512   2112 190328    0    0    95    50  122  163  0  1 99  0  0
 0  0      0 1119516   2112 190360    0    0     0     0   87   95  0  1 99  0  0
 0  0      0 1119516   2112 190364    0    0     4    10  108  120  0  0 100  0  0
 0  0      0 1119516   2112 190364    0    0     0     0   87   92  0  0 100  0  0
 0  0      0 1119516   2112 190364    0    0     0    99   81   95  0  0 100  0  0
 0  0      0 1119516   2112 190364    0    0     0    10  113  125  0  1 99  0  0
  • 由於vmstat 1 是持續運行的。因此腳本最後的exit 就沒有辦法執行,只能手動終止
相關文章
相關標籤/搜索