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

expect脚本同步文件

  • 在一台机器上把文件同步到多台机器上
  • 自动同步文件 vim 4.expect 
[root@yong-01 sbin]# vim 4.expect

#!/usr/bin/expect
set passwd "20655739"
spawn rsync -av root@192.168.181.135:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send $passwd\r}
}
expect eof


查看同步过程
[root@yong-01 sbin]# chmod a+x 4.expect 
[root@yong-01 sbin]# ./4.expect 
spawn rsync -av root@192.168.181.135:/tmp/12.txt /tmp/
The authenticity of host '192.168.181.135 (192.168.181.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.181.135' (ECDSA) to the list of known hosts.
receiving incremental file list
12.txt

sent 30 bytes  received 84 bytes  228.00 bytes/sec
total size is 5  speedup is 0.04
[root@yong-01 sbin]# ls /tmp/12.txt 
/tmp/12.txt
[root@yong-01 sbin]# ll /tmp/12.txt 
-rw-r--r-- 1 root root 5 7月  21 00:48 /tmp/12.txt
  • expect eof :只有spawn执行的命令结果才会被expect捕捉到,由于spawn会启动一个进程,只有这个进程的相关信息才会被捕捉到,主要包括:标准输入的提示信息,eof和timeout。

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

  • set timeout 定义超时时间(单位为 秒) -1 为永远不超时
  • 指定host和要同步的文件 vim 5.expect
[root@yong-01 sbin]# vim 5.expect 

#!/usr/bin/expect
set passwd "20655739"
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
  • 变量定义的文件地址,使用时,必须写绝对路径
  • 本机文件同步到远程机器上去: ./5.expect 192.168.181.135 "/tmp/12.txt" 
  • [root@yong-01 sbin]# ./5.expect 192.168.181.135 "/tmp/12.txt"
    spawn rsync -av /tmp/12.txt root@192.168.181.135:/tmp/12.txt
    sending incremental file list
    12.txt
    
    sent 79 bytes  received 31 bytes  220.00 bytes/sec
    total size is 5  speedup is 0.05
    expect: spawn id exp6 not open
        while executing
    "expect eof"
        (file "./5.expect" line 10)

     

构建文件分发系统

  • 需求背景
    • 对于大公司而言,确定时不时会有网站或者配置文件更新,并且使用的机器确定也是好多台,少则几台,多则几十甚至上百台。因此,自动同步文件是相当重要的。
  • 实现思路
    • 首先要有一台模板机器,把要分发的文件准备好,而后只要使用expect脚本批量把须要同步的文件分发到目标机器便可。
  • 核心命令
    • rsync -av --files-from=list.txt / root@host:/
    • 使用rsync 的 --files参数,能够实现调用文件里面的列表,进行多个文件远程传输,进而实现文件分发
    • 文件分发系统的实现

一、rsync.expect 内容shell

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

#!/usr/bin/expect
set passwd "20655739"
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 建立路径
  • 由于实现分发系统,确定是由于须要分发的机器数量过大,因此,定义好了 文件 的 list 列表文件之后, 还须要配置 ip 的列表文件
  • 建立须要同步文件的列表文件
  • 建立须要同步IP地址的列表文件
  1. rsync.sh 内容
#!/bin/bash 
for ip in `cat /tmp/ip.list`
do
    echo $ip   
    ./rsync.expect $ip /tmp/file.list
done

执行过程
[root@yong-01 sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ echo 192.168.181.135
192.168.181.135
+ ./rsync.expect 192.168.181.135 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.181.135:/
building file list ... done
root/

sent 114 bytes  received 15 bytes  258.00 bytes/sec
total size is 1051  speedup is 8.15
expect: spawn id exp6 not open
    while executing
"expect eof"
    (file "./rsync.expect" line 10)
+ for ip in '`cat /tmp/ip.list`'
+ echo 192.168.181.138
192.168.181.138
+ ./rsync.expect 192.168.181.138 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.181.138:/
root@192.168.181.138's password: 
building file list ... done
root/
root/shell/
root/shell/case.sh
tmp/
tmp/12.txt

sent 1257 bytes  received 59 bytes  2632.00 bytes/sec
total size is 1051  speedup is 0.80
  • 这个sh 的目的,就是遍历一下 ip列表文件中的 ip地址vim

  • 最重要的,expect脚本 必须加入执行权限bash

  • 文件不存在,会报错ssh

  • 分发系统还有一个重要的关键是,确保同步的机器的密码一致,不然将不能实现同步;因此这就存在一个弊端,一旦脚本暴露,将会让别人知道如何登录你机器;固然也有对应的解决办法,那就是使用密钥认证,这样的话,天然在命令行业省去“输入密码< password:" { send "$passwd\r" } >''”和“定义密码< set passwd "123123a" >”的命令了网站

批量远程执行命令

  • exe.expect 内容
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "20655739"
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 内容
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done
相关文章
相关标签/搜索