linux expect脚本文件分发系统

本文介绍如何利用rsync和expect脚本实现文件的批量分发及远程命令的批量执行,通过具体示例展示了如何配置相关脚本来提高工作效率。
部署运行你感兴趣的模型镜像

(以下示例思路来自猿课阿铭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

您可能感兴趣的与本文相关的镜像

Anything-LLM

Anything-LLM

AI应用

AnythingLLM是一个全栈应用程序,可以使用商用或开源的LLM/嵌入器/语义向量数据库模型,帮助用户在本地或云端搭建个性化的聊天机器人系统,且无需复杂设置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值