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

expect脚本同步文件,expect脚本指定host和要同步的文件,构建文件分发系统,批量远程执行

时间:2018-07-23 00:07:27      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:rem   思路   HERE   emd   定义   ast   命令行   tty   配置文件   

expect脚本同步文件
  • 在一台机器上把文件同步到多台机器上
  • 自动同步文件
    [root@akuilinux01 sbin]# vim 4.expect 
    #!/usr/bin/expect
    set passwd "s5381561"
    spawn rsync -av root@192.168.21.129:/tmp/12.txt /tmp/
    expect {
    "yes/no" { send "yes\r"}
    "password:" { send "$passwd\r" }
    }
    expect eof
    [root@akuilinux01 sbin]# chmod a+x 4.expect 
    [root@akuilinux01 sbin]# ./4.expect 
    spawn rsync -av root@192.168.21.129:/tmp/12.txt /tmp/
    root@192.168.21.129‘s password: 
    receiving incremental file list
    12.txt
    sent 43 bytes  received 97 bytes  280.00 bytes/sec
    total size is 5  speedup is 0.04
    [root@akuilinux01 sbin]# cat /tmp/12.txt 
    1212
  • expect eof :只有spawn执行的命令结果才会被expect捕捉到,因为spawn会启动一个进程,只有这个进程的相关信息才会被捕捉到,主要包括:标准输入的提示信息,eof和timeout。set timeout 定义超时时间(单位为 秒) -1 为永远不超时

    expect脚本指定host和要同步的文件

  • 指定host和要同步的文件
    [root@akuilinux01 sbin]# vim 5.expect
    #!/usr/bin/expect
    set passwd "s5381561"
    set host [lindex $argv 0]
    set file [lindex $argv 1]
    spawn rsync -av $file root@$host:$file
    expect {
    "yes/no" { send "yes\r"}
    "password:" { send "$passwd\r" }
    }
    expect eof
    [root@akuilinux01 sbin]# chmod a+x 5.expect 
    [root@akuilinux01 sbin]# ./5.expect 192.168.21.129 /root/akui.txt 
    spawn rsync -av /root/akui.txt root@192.168.21.129:/root/akui.txt
    root@192.168.21.129‘s password: 
    sending incremental file list
    akui.txt
    sent 84 bytes  received 35 bytes  238.00 bytes/sec
    total size is 0  speedup is 0.00
  • 指定目标ip和本机要同步的文件,必须写绝对路径

    构建文件分发系统

  • 需求背景:对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
  • 实现思路:首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
  • 核心命令:rsync -av --files-from=list.txt / root@host:/
  • 使用rsync 的 --files-from参数,可以实现调用文件里面的列表,进行多个文件远程传输,进而实现文件分发
  • rsync.expect 内容
    [root@akuilinux01 sbin]# vim rsync.expect
    #!/usr/bin/expect
    set passwd "s5381561"
    set host [lindex $argv 0]
    set file [lindex $argv 1]
    spawn rsync -avR --files-from=$file / root@$host:/   
    expect {
    "yes/no" { send "yes\r"}
    "password:" { send "$passwd\r" }
    }
    expect eof
    • -R可以递归创建目录
  • 创建需要同步IP地址的列表文件
    [root@akuilinux01 sbin]# vim /tmp/ip.list
    192.168.21.129
    127.0.0.1
  • 创建需要同步IP地址的列表文件
    [root@akuilinux01 sbin]# vim /tmp/file.list 
    /root/222/1111/11.txt
    /root/1.txt
  • rsync.sh 内容
    [root@akuilinux01 sbin]# vim rsync.sh
    #!/bin/bash
    for ip in `cat /tmp/ip.list`
    do
     echo $ip
     ./rsync.expect $ip /tmp/file.list
    done
  • 分发系统还有一个重要的关键是,确保同步的机器的密码一致,否则将不能实现同步;所以这就存在一个弊端,一旦脚本暴露,将会让别人知道如何登陆你机器;当然也有对应的解决办法,那就是使用密钥认证,这样的话,自然在命令行业省去“输入密码< password:" { send "$passwd\r" } >‘‘”和“定义密码< set passwd "123123a" >”的命令了
  • 测试
    root@akuilinux01 sbin]# sh rsync.sh 
    192.168.21.129
    spawn rsync -avR --files-from=/tmp/file.list / root@192.168.21.129:/
    root@192.168.21.129‘s password: 
    building file list ... done
    root/
    root/1.txt
    root/222/
    root/222/1111/
    root/222/1111/11.txt
    sent 299 bytes  received 63 bytes  724.00 bytes/sec
    total size is 72  speedup is 0.20
    127.0.0.1
    spawn rsync -avR --files-from=/tmp/file.list / root@127.0.0.1:/
    The authenticity of host ‘127.0.0.1 (127.0.0.1)‘ 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 ‘127.0.0.1‘ (ECDSA) to the list of known hosts.
    root@127.0.0.1‘s password: [root@akuilinux01 sbin]# 

    批量远程执行命令

  • exe.expect 内容
    [root@akuilinux01 sbin]# vim exe.expect
    #!/usr/bin/expect
    set host [lindex $argv 0]
    set passwd "s5381561"
    set cm [lindex $argv 1]
    spawn ssh root@$host
    expect {
    "yes/no" { send "yes\r"}
    "password:" { send "$passwd\r" }
    }
    expect "]*"
    send "$cm\r"
    expect "]*"
    send "exit\r"
  • exe.sh 内容
    [root@akuilinux01 sbin]# vim exe.sh
    #!/bin/bash
    for ip in `cat /tmp/ip.list`
    do
    echo $ip
    ./exe.expect $ip "w;ls /tmp"
    done
  • 测试
    [root@akuilinux01 sbin]# chmod a+x exe.expect 
    [root@akuilinux01 sbin]# sh exe.sh 
    192.168.21.129
    spawn ssh root@192.168.21.129
    root@192.168.21.129‘s password: 
    Last login: Sun Jul 22 22:19:51 2018 from 192.168.21.128
    [root@akuilinux02 ~]# w;ls /tmp
    23:34:18 up  1:56,  2 users,  load average: 0.08, 0.03, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     22:14   27:06   0.01s  0.01s -bash
    root     pts/1    192.168.21.128   23:34    0.00s  0.01s  0.00s w
    12.txt
    mysql.sock
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-chronyd.service-5DHBEF
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vgauthd.service-sG39NN
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vmtoolsd.service-CVypi4
    [root@akuilinux02 ~]# 127.0.0.1
    spawn ssh root@127.0.0.1
    root@127.0.0.1‘s password: 
    Last failed login: Sun Jul 22 23:28:56 CST 2018 from 127.0.0.1 on ssh:notty
    There were 2 failed login attempts since the last successful login.
    Last login: Sun Jul 22 21:35:11 2018 from 192.168.21.1
    [root@akuilinux01 ~]# w;ls /tmp
    23:34:18 up  2:05,  2 users,  load average: 0.12, 0.05, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     21:35    2.00s  0.25s  0.00s /usr/bin/expect ./exe.expect 127.
    root     pts/1    127.0.0.1        23:34    0.00s  0.00s  0.00s w
    12.txt
    akuilinux
    aming.sock
    file.list
    ip.list
    lalal
    mysql.sock
    systemd-private-09ebb5bc0a88443db92bc329d77b2efc-chronyd.service-WDveS2
    systemd-private-09ebb5bc0a88443db92bc329d77b2efc-httpd.service-Ok7bke
    systemd-private-09ebb5bc0a88443db92bc329d77b2efc-vgauthd.service-B8RbhF
    systemd-private-09ebb5bc0a88443db92bc329d77b2efc-vmtoolsd.service-WiU5il
    systemd-private-c27889f557f94d1b8cfed596ba761ba5-chronyd.service-kH7zvQ
    systemd-private-c27889f557f94d1b8cfed596ba761ba5-httpd.service-Cj8ABZ
    systemd-private-c27889f557f94d1b8cfed596ba761ba5-vgauthd.service-21n8Br
    systemd-private-c27889f557f94d1b8cfed596ba761ba5-vmtoolsd.service-8VTbyC
    www.sock

    扩展

  • shell多线程

expect脚本同步文件,expect脚本指定host和要同步的文件,构建文件分发系统,批量远程执行

标签:rem   思路   HERE   emd   定义   ast   命令行   tty   配置文件   

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

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