- 在一台机器上把文件同步到多台机器上
- 自动同步文件
[[email?protected] sbin]# vim 4.expect
#!/usr/bin/expect
set passwd "s5381561"
spawn rsync -av [email?protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yesr"}
"password:" { send "$passwdr" }
}
expect eof
[[email?protected] sbin]# chmod a+x 4.expect
[[email?protected] sbin]# ./4.expect
spawn rsync -av [email?protected]:/tmp/12.txt /tmp/
[email?protected]‘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
[[email?protected] sbin]# cat /tmp/12.txt
1212
- expect eof :只有spawn执行的命令结果才会被expect捕捉到,因为spawn会启动一个进程,只有这个进程的相关信息才会被捕捉到,主要包括:标准输入的提示信息,eof和timeout。set timeout 定义超时时间(单位为 秒) -1 为永远不超时
expect脚本指定host和要同步的文件
- 指定host和要同步的文件
[[email?protected] sbin]# vim 5.expect
#!/usr/bin/expect
set passwd "s5381561"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file [email?protected]$host:$file
expect {
"yes/no" { send "yesr"}
"password:" { send "$passwdr" }
}
expect eof
[[email?protected] sbin]# chmod a+x 5.expect
[[email?protected] sbin]# ./5.expect 192.168.21.129 /root/akui.txt
spawn rsync -av /root/akui.txt [email?protected]:/root/akui.txt
[email?protected]‘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 / [email?protected]:/
- 使用rsync 的 --files-from参数,可以实现调用文件里面的列表,进行多个文件远程传输,进而实现文件分发
- rsync.expect 内容
[[email?protected] 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 / [email?protected]$host:/
expect {
"yes/no" { send "yesr"}
"password:" { send "$passwdr" }
}
expect eof
- 创建需要同步IP地址的列表文件
[[email?protected] sbin]# vim /tmp/ip.list
192.168.21.129
127.0.0.1
- 创建需要同步IP地址的列表文件
[[email?protected] sbin]# vim /tmp/file.list
/root/222/1111/11.txt
/root/1.txt
- rsync.sh 内容
[[email?protected] 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 "$passwdr" } >‘‘”和“定义密码< set passwd "123123a" >”的命令了
- 测试
[email?protected] sbin]# sh rsync.sh
192.168.21.129
spawn rsync -avR --files-from=/tmp/file.list / [email?protected]:/
[email?protected]‘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 / [email?protected]:/
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.
[email?protected]‘s password: [[email?protected] sbin]#
批量远程执行命令
- exe.expect 内容
[[email?protected] sbin]# vim exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "s5381561"
set cm [lindex $argv 1]
spawn ssh [email?protected]$host
expect {
"yes/no" { send "yesr"}
"password:" { send "$passwdr" }
}
expect "]*"
send "$cmr"
expect "]*"
send "exitr"
- exe.sh 内容
[[email?protected] sbin]# vim exe.sh
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
echo $ip
./exe.expect $ip "w;ls /tmp"
done
- 测试
[[email?protected] sbin]# chmod a+x exe.expect
[[email?protected] sbin]# sh exe.sh
192.168.21.129
spawn ssh [email?protected]
[email?protected]‘s password:
Last login: Sun Jul 22 22:19:51 2018 from 192.168.21.128
[[email?protected] ~]# w;ls /tmp
23:34:18 up 1:56,2 users,load average: 0.08,0.03,0.05
USER TTY FROM [email?protected] 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
[[email?protected] ~]# 127.0.0.1
spawn ssh [email?protected]
[email?protected]‘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
[[email?protected] ~]# w;ls /tmp
23:34:18 up 2:05,load average: 0.12,0.05,0.05
USER TTY FROM [email?protected] 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多线程