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

简介:

expect脚本同步文件

[root@centos7-3 shell]# vi 1.expect 

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

[root@centos7-3 shell]# chmod +x 1.expect

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

说明: expect eof的作用是等待脚本中的命令执行完后再退出。

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

[root@centos7-3 shell]# vi 2.expect 

#!/usr/bin/expect
set passwd "123456"
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@centos7-3 shell]# chmod a+x 2.expect 
[root@centos7-3 shell]# touch /tmp/3.txt
[root@centos7-3 shell]# ./2.expect 192.168.1.83 "/tmp/3.txt"
spawn rsync -av /tmp/3.txt root@192.168.1.83:/tmp/3.txt
root@192.168.1.83's password: 
sending incremental file list
3.txt

sent 69 bytes  received 31 bytes  66.67 bytes/sec

注: 本脚本只能同步一个文件。

构建文件分发系统

首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可(把多个文件分发到多台机器时需要创建文件、IP列表,即本文中的list.txt、iplist.txt)。
创建 分发系统
创建一个文件列表文件备用:

[root@test ~]# vim /tmp/list.txt

/tmp/12.txt
/tmp/3.txt
#该文件下可以添加多个文件

注意:此处要保证客户端有同样的目录。

创建一个IP列表文件备用:

[root@test ~]# vim /tmp/iplist.txt

192.168.1.83
#该文件下可以指定多个IP
创建rsync.expect脚本:
[root@test ~]# vim rsync.expect

#!/usr/bin/expect
set passwd "123456"
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

[root@test ~]# chmod a+x rsync.expect
[root@test ~]# vim rsync.sh

#!/bin/bash
for ip in `cat /tmp/iplist.txt`
do
    ./rsync.expect $ip /tmp/list.txt
done

执行:
sh -x rsync.sh
++ cat /tmp/iplist.txt
+ for ip in '`cat /tmp/iplist.txt`'
+ ./rsync.expect 192.168.1.48 /tmp/list.txt
spawn rsync -avR --files-from=/tmp/list.txt / root@192.168.1.48:/
building file list ... done
tmp/
tmp/12.txt
tmp/3.txt

sent 163 bytes  received 53 bytes  432.00 bytes/sec
total size is 13  speedup is 0.06

批量远程执行命令

创建exe.expect
[root@test ~]# vim exe.expect

#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
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"

[root@test ~]# chmod a+x exe.expect

[root@test ~]# vim exe.sh

#!/bin/bash
for ip in `cat /tmp/iplist.txt`
do
  ./exe.expect $ip "hostname"
done
执行:

sh exe.sh













本文转自方向对了,就不怕路远了!51CTO博客,原文链接:http://blog.51cto.com/jacksoner/2074185 ,如需转载请自行联系原作者




相关文章
|
7月前
|
安全
集群同步文件分发脚本编写
集群同步文件分发脚本编写
55 0
|
9月前
|
网络安全 Go Cloud Native
SSH连接服务器后执行多条命令 |Go主题月
大家平时有没有遇到自己连接云服务器,ssh 连接上去之后,发现自己的一些小工具用不了 例如go build无法使用 ,由于我们安装配置golang 环境的时候,是在文件/etc/profile中写了配置,因此需要source 一下/etc/profile
119 0
SSH连接服务器后执行多条命令 |Go主题月
|
Shell 网络安全 数据安全/隐私保护
expect - 自动交互脚本
1. expect参数 expect教程中文版 expect说明 2. 启用选项 -c :执行脚本前先执行的命令,可多次使用。 -d :debug模式,可以在运行时输出一些诊断信息,与在脚本开始处使用 exp_internal 1 相似。
1389 0