linux 内网批量快速传输大文件 nc

传输内网宽带拉满

在这里插入图片描述

先运行接收端 开始监听

  • 接收端脚本 re.sh
#!/bin/bash
#Revision:        1.0
#Author:          author
#Email:           author@email.com
#Date:            2023/07/16

# 定义接收文件路径
received_file_path="/root/100.txt"
local_port="6789"

nohup nc -l -p "$local_port" > "$received_file_path" &

使用 ansible 拷贝脚本到其它接收端服务器

ansible host01 -m copy -a 'src=/root/re.sh dest=/root'

批量运行接收端脚本

ansible host01 -m shell -a "bash /root/re.sh"

查看nc是否运行

# ps aux | grep nc
nc -l -p 6789

运行发送端

  • 发送端运行脚本 se.sh
#!/bin/bash

# 定义待传输文件路径
file_path="/root/100.txt"
remote_port="6789"
remote_hosts_file="/root/hostlist.txt"

# 读取远程主机列表文件,保存到数组中
readarray -t remote_hosts < "$remote_hosts_file"

for host in "${remote_hosts[@]}"; do
  # 去除每行首尾的空格和换行符
  host="$(echo -e "${
    
    host}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"

  echo "host = $host , port = $remote_port"
  # 向目标主机发送文件
  cat "$file_path" | nc -w 10 "$host" "$remote_port" &
  echo "host = $host 开始传输"
done

wait
echo "文件传输完成"
  • hostlist.txt 为接收端IP
# cat hostlist.txt
192.168.20
192.168.21
192.168.22

运行发送端脚本开始传输文件

bash se.sh

猜你喜欢

转载自blog.csdn.net/u010953692/article/details/131757280
今日推荐