배포 시스템 소개,expect 스크립트 원격 로그인,expect 스크립트 원격 실행 명령,expect 스크립트 전달 매개 변수

20.27 배포 시스템 소개


응용 장면
업무가 커질수록 서버 수요가 많아지기 때문에 몇 대의 서버가 괜찮다.십여 대, 수십 대가 될 때, 작업량은 매우 크다.그리고 규범에 맞지 않아 템플릿을 각 기계에 나누어 주어야 한다.
소스 오픈 소프트웨어,expect 스크립트 언어로 배포 시스템의 기능을 실현할 수 있습니다.

20.28 expect 스크립트 원격 로그인

  • yum install -y expect
  • 자동으로 원격 로그인하고 명령을 실행합니다
  • #!/usr/bin/expect
    set host "192.168.21.130"
    set passwd "rootroot"
    spawn ssh root@$host
    expect {
        "yes/no" {send "yes\r"; exp_continue}
        "assword:" {send "$passwd\r"}
    }
    interact # 
    #   expect eof
    

    작업 수행
    [root@qingyun-02 sbin]# vim 1.expect
    
    # 
    [root@qingyun-02 sbin]# vi /root/.ssh/known_hosts
    
    # 
    [root@qingyun-02 sbin]# chmod a+x 1.expect 
    
    # 
    [root@qingyun-02 sbin]# ./1.expect
    spawn ssh [email protected]
    The authenticity of host '192.168.21.130 (192.168.21.130)' can't be established.
    ECDSA key fingerprint is SHA256:e6Fx3oJ8GcMbFnmTV7JIcvZ3sG6W6yrfvdKccXk+c7c.
    ECDSA key fingerprint is MD5:15:57:6c:19:21:a2:e4:3e:df:19:27:13:c2:2e:8e:ba.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.21.130' (ECDSA) to the list of known hosts.
    [email protected]'s password: 
    Last login: Wed Feb 28 08:57:58 2018 from 192.168.21.1
    

    20.29 expect 스크립트 원격 실행 명령

  • 자동 원격 로그인 후 명령을 실행하고 종료합니다
  • # 
    
    #!/usr/bin/expect
    set user "root"
    set passwd "rootroot"
    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@qingyun-02 sbin]# chmod a+x 2.expect 
    [root@qingyun-02 sbin]# ./2.expect
    spawn ssh [email protected]
    [email protected]'s password: 
    Last login: Wed Feb 28 09:30:05 2018 from 192.168.21.132
    [root@qingyun-01 ~]# touch /tmp/12.txt
    [root@qingyun-01 ~]# echo 1212 > /tmp/12.txt
    [root@qingyun-01 ~]# [root@qingyun-02 sbin]# 
    
    # 
    [root@qingyun-02 sbin]# ./1.expect 
    spawn ssh [email protected]
    [email protected]'s password: 
    Last login: Wed Feb 28 09:36:13 2018 from 192.168.21.132
    [root@qingyun-01 ~]# ls /tmp/12.txt
    /tmp/12.txt
    [root@qingyun-01 ~]# cat /tmp/12.txt
    1212
    

    20.30 expect 스크립트 전달 매개 변수

  • 전달 매개 변수
  • [root@qingyun-02 sbin]# vim 3.expect
    
    #!/usr/bin/expect
    
    set user [lindex $argv 0]
    set host [lindex $argv 1]
    set passwd "rootroot"
    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@qingyun-02 sbin]# chmod a+x 3.expect
    [root@qingyun-02 sbin]# ./3.expect root 192.168.21.130 ls
    spawn ssh [email protected]
    [email protected]'s password: 
    Last login: Wed Feb 28 09:39:16 2018 from 192.168.21.132
    [root@qingyun-01 ~]# ls
    anaconda-ks.cfg  shell
    [root@qingyun-01 ~]# [root@qingyun-02 sbin]# 
    
    [root@qingyun-02 sbin]# ./3.expect root 192.168.21.130 "ls;w;vmstat 1"
    #     
    spawn ssh [email protected]
    [email protected]'s password: 
    Last login: Wed Feb 28 09:46:40 2018 from 192.168.21.132
    [root@qingyun-01 ~]# ls;w;vmstat 1
    anaconda-ks.cfg  shell
     09:49:34 up  1:00,  2 users,  load average: 0.00, 0.01, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     08:57   12:46   0.03s  0.03s -bash
    root     pts/1    192.168.21.132   09:49    0.00s  0.00s  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
     3  0      0 206004   2108 197024    0    0    46    11   93  134  0  0 99  1  0
     0  0      0 206020   2108 197040    0    0     0     0   74  105  0  0 100  0  0
     0  0      0 206020   2108 197040    0    0     0     0   76  100  0  0 100  0  0
     0  0      0 206020   2108 197040    0    0     0     9   87  126  0  0 100  0  0
     0  0      0 206020   2108 197040    0    0     0     0   75  101  0  0 100  0  0
     0  0      0 206020   2108 197040    0    0     0    98   82  115  0  1 99  0  0
     0  0      0 206020   2108 197040    0    0     0     5   89  121  0  0 100  0  0
     0  0      0 206020   2108 197040    0    0     0     0   75  108  0  0 100  0  0
     0  0      0 206012   2108 197048    0    0     0     0   92  119  1  1 98  0  0
     0  0      0 206012   2108 197048    0    0     0     0   88  118  0  0 100  0  0
    
    #expect , 10s 
    

    좋은 웹페이지 즐겨찾기