1. 程式人生 > >分發系統-expect講解,expect腳本遠程登錄,expect腳本遠程執行命令,expect腳本

分發系統-expect講解,expect腳本遠程登錄,expect腳本遠程執行命令,expect腳本

ssh 傳遞 memory stat 講解 mod 同步 print 業務

分發系統-expect講解
  • 業務越來越大,網站app,後端,編程語言是php,所以就需要配置lamp或者lnmp,最好還需要吧代碼上傳到服務器上;但是因為業務增加,代碼增加,多臺機器,就會非常麻煩;這是只需要一個分發系統,就可以把每次更新的代碼發布到需要更新的服務器上
  • expect,是一種腳本語言;通過他可以實現傳代碼上線,也可以登錄機器輸入命令
  • 首先要準備一臺模板機器,機器的IP,對應用戶的密碼,通過rsync同步代碼,還可以通過expect去執行某些命令

    expect腳本遠程登錄

  • yum install -y expect
  • 自動遠程登錄
    [root@akuilinux01 sbin]# vim 1.expect
    [root@akuilinux01 sbin]# vim 1.expect
    #! /usr/bin/expect
    set host "192.168.21.129"
    set passwd "s5381561"
    spawn ssh root@$host
    expect {
    "yes/no" { send "yes\r"; exp_continue}
    "password:" { send "$passwd\r" }
    }
    interact
    [root@akuilinux01 sbin]# chmod a+x 1.expect
    root@akuilinux01 sbin]# ./1.expect
    spawn ssh [email protected]
    The authenticity of host ‘192.168.21.129 (192.168.21.129)‘ can‘t be established.
    ECDSA key fingerprint is SHA256:r2y+hZVvvomXE4d3uRSM0cO+kMdqyWAOMqpTtA2qp6I.
    ECDSA key fingerprint is MD5:ee:da:3b:ea:27:56:38:82:bc:e8:73:10:18:a0:c2:bd.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added ‘192.168.21.129‘ (ECDSA) to the list of known hosts.
    [email protected]‘s password: 
    Last login: Wed Jul 11 21:59:23 2018 from 192.168.21.1
    [root@akuilinux02 ~]# 
  • 在expect 定義變量,用set
  • 這個文件是就保證登錄信息的,清空的話,重新遠程登錄ssh 會有提示 /root/.ssh/known_hosts
    exp_continue 表示繼續 \r 表示換行 interact 繼續停留在這臺機器,不退出

    expect腳本遠程執行命令

  • 遠程登錄後,執行命令並退出
    [root@akuilinux01 sbin]# vim 2.expect
    #!/usr/bin/expect
    set user "root"
    set passwd "s5381561"
    spawn ssh [email protected]
    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@akuilinux01 sbin]# chmod a+x 2.expect 
    [root@akuilinux01 sbin]# ./2.expect 
    spawn ssh [email protected]
    [email protected]‘s password: 
    Last login: Sun Jul 22 22:07:20 2018 from 192.168.21.128
    [root@akuilinux02 ~]# touch /tmp/12.txt
    [root@akuilinux02 ~]# echo 1212 > /tmp/12.txt
    [root@akuilinux02 ~]# [root@akuilinux01 sbin]# 
    [root@akuilinux02 ~]# ls /tmp/
    12.txt
    mysql.sock
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-chronyd.service-5DHBEF
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vgauthd.service-sG39NN
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vmtoolsd.service-CVypi4
    [root@akuilinux02 ~]# cat /tmp/12.txt 
    1212
  • expect "]*" : “” 通配]右邊所有字符
  • send 執行命令

    expect腳本傳遞參數

  • 傳遞參數
    [root@akuilinux01 sbin]# vim 3.expect
    #!/usr/bin/expect
    set user [lindex $argv 0]  #第一個參數賦值給user
    set host [lindex $argv 1]  #第二個參數
    set passwd "s5381561"
    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"
    [root@akuilinux01 sbin]# chmod a+x 3.expect
    [root@akuilinux01 sbin]# ./3.expect root 192.168.21.129 ls
    spawn ssh [email protected]
    [email protected]‘s password: 
    Last login: Sun Jul 22 22:14:12 2018 from 192.168.21.1
    [root@akuilinux02 ~]# ls
    111  anaconda-ks.cfg
    [root@akuilinux02 ~]# [root@akuilinux01 sbin]# 
    [root@akuilinux02 ~]# [root@akuilinux01 sbin]# ./3.expect root 192.168.21.129 "ls;w;vmstat 1"
    spawn ssh [email protected]
    [email protected]‘s password: 
    Last login: Sun Jul 22 22:19:35 2018 from 192.168.21.128
    [root@akuilinux02 ~]# ls;w;vmstat 1
    111  anaconda-ks.cfg
    22:19:51 up 41 min,  2 users,  load average: 0.00, 0.02, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     22:14   55.00s  0.01s  0.01s -bash
    root     pts/1    192.168.21.128   22:19    0.00s  0.01s  0.00s 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 1142532   2076 173112    0    0    15    14   51   44  0  0 99  0  0
    0  0      0 1142780   2076 173208    0    0     0     6  257  149  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0   294  188  181  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0     0  122  112  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0     0  116  105  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0     0  107   99  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0     0  160  136  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0     5  124  119  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0   168  157  157  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0     0  102   99  0  0 100  0  0
    0  0      0 1142780   2076 173208    0    0     0     0  119  113  0  0 100  0  0
    [root@akuilinux01 sbin]# 
  • 支持多條參數運行,vmstat 1命令10秒後自動退出

分發系統-expect講解,expect腳本遠程登錄,expect腳本遠程執行命令,expect腳本