linux expect脚本文件分发系统
来源:互联网 发布:在淘宝怎么买电棍 编辑:程序博客网 时间:2024/05/22 18:55
(以下示例思路来自猿课阿铭linux)
远程分发文件基础知识
scp 文件 目标IP:绝对路径
scp 1.txt 192.168.153.134:/tmp/
远程分发目录
scp -r
rsync 增量备份(本地远程都可用)
rsync -av 文件目录 目标IP:目标目录
构建文件分发系统
使用expect脚本批量把需要同步的文件分发到目标机器
核心命令 :rsync -av –files-from=list.txt /root@hostname:/
实现:
cat rsync.expect
#!/usr/bin/expect
set passwd "*******"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av --files-from=$file /root@$host:/
expect {
"yes/no" { send"yes\r"}
"password:" { send "$passwd\r"}
}
expect eof
cat ip.list
192.168.153.134
192.168.153.135
192.168.153.136
192.168.153.137
cat rsync.sh
#!/bin/bash
for ip in `cat ip.list`
do
echo$ip
./rsync.expect$ip list.txt
done
cat list.txt
/root/1.expect
/tmp/1.txt
/tmp/a.txt
/tmp/A.txt
/tmp/b.txt
/tmp/B.txt
命令批量执行脚本
cat exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "********"
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"
cat exe.sh
#!/bin/bash
for ip in `cat ip.list`
do
echo$ip
./exe.expect$ip 'free;ls /tmp'
done
cat ip.list
192.168.153.134
192.168.153.135
192.168.153.136
192.168.153.137