码迷,mamicode.com
首页 > 其他好文 > 详细

分发系统-expect讲解,expect脚本远程登录,expect脚本远程执行命令,expect脚本

时间:2018-07-22 23:40:34      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:can   chm   tty   spawn   man   网站   perm   rsync   配置   

分发系统-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 root@192.168.21.129
    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.
    root@192.168.21.129‘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 $user@192.168.21.129
    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 root@192.168.21.129
    root@192.168.21.129‘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 root@192.168.21.129
    root@192.168.21.129‘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 root@192.168.21.129
    root@192.168.21.129‘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脚本

标签:can   chm   tty   spawn   man   网站   perm   rsync   配置   

原文地址:http://blog.51cto.com/akui2521/2148777

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!