20.31 expect脚本同步文件
脚本
#!/usr/bin/expect
#定义密码变量
set passwd "toor"
#利用rsync远程同步文件到本地机器
spawn rsync -av root@192.168.142.131:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
运行过程:
[root@localhost sbin]# ls /tmp/
mysql.sock systemd-private-53796ea7e4b64e7ea2e4a5f6c6322639-vgauthd.service-wxkS9c
systemd-private-53796ea7e4b64e7ea2e4a5f6c6322639-chronyd.service-HQ572G systemd-private-53796ea7e4b64e7ea2e4a5f6c6322639-vmtoolsd.service-U4qH3r
systemd-private-53796ea7e4b64e7ea2e4a5f6c6322639-httpd.service-bU3uZf
[root@localhost sbin]# ./4.expect
spawn rsync -av root@192.168.142.131:/tmp/12.txt /tmp/
root@192.168.142.131'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@localhost sbin]# ls /tmp/
12.txt systemd-private-53796ea7e4b64e7ea2e4a5f6c6322639-httpd.service-bU3uZf
mysql.sock systemd-private-53796ea7e4b64e7ea2e4a5f6c6322639-vgauthd.service-wxkS9c
systemd-private-53796ea7e4b64e7ea2e4a5f6c6322639-chronyd.service-HQ572G systemd-private-53796ea7e4b64e7ea2e4a5f6c6322639-vmtoolsd.service-U4qH3r
[root@localhost sbin]#
#可以看到本地tmp下有了12.txt文件
#同步的时候必须要有rsync软件
expect脚本指定host和要同步的文件
脚本
#!/usr/bin/expect
#定义密码变量
set passwd "123456"
#定义host参数
set host [lindex $argv 0]
#定义文件参数
set file [lindex $argv 1]
#利用rsync本机同步到另外一条机器
spawn rsync -av $file root@$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
执行过程:
[root@localhost sbin]# echo "123" > /tmp/etc
[root@localhost sbin]# cat /tmp/etc
123
[root@localhost sbin]# ./5.expect 192.168.142.131 /tmp/etc
spawn rsync -av /tmp/etc root@192.168.142.131:/tmp/etc
root@192.168.142.131's password:
sending incremental file list
etc
sent 93 bytes received 35 bytes 256.00 bytes/sec
total size is 4 speedup is 0.03
[root@localhost sbin]# ./1.expect
spawn ssh root@192.168.142.131
root@192.168.142.131's password:
Last login: Sun Jul 22 18:46:53 2018 from 192.168.142.1
[root@localhost ~]# cat /tmp/etc
123
[root@localhost ~]#
#首先创建并写入一个文件,然后同步到131机器,并查看
构建文件分发系统
expect脚本
#!/usr/bin/expect
#定义密码
set passwd "toor"
#定义host参数
set host [lindex $argv 0]
#定义文件
set file [lindex $argv 1]
#--files-from可以创建一个文本,并写入要同步文件的路径进行同步
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
########################################################################
#以下创建一个文本
touch /tmp/tongbu.txt
[root@localhost sbin]# cat /tmp/tongbu.txt
/tmp/1323.txt
/tmp/etc3
~
[root@localhost sbin]#
写一个shell脚本
#!/bin/bash
#定义ip变量为 cat ip.txt文本
for ip in `cat /tmp/ip.txt`
do
echo $ip
#运行命令 IP即第一个参数(调用文本) 同步文件路径(调用同步文本)
./6.expect $ip /tmp/tongbu.txt
done
#############################
定义ip文本
vim /tmp/ip.txt
192.168.142.131
127.0.0.1
批量远程执行命令
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"
shell脚本
#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done