基于ubuntu nc指令实现远程传输文件到嵌入式设备中

背景:

最近在使用nc进行远程文件传输的时候发现在文件传输完成时,没有正确的反馈,而是界面一直停留在传输阶段,加上使用nc传输需要设置一些诸如-l、 -p等参数,于是想将这些参数包裹在sh脚本中,一键执行脚本,方便快捷,具体如下:

一、本地端:

powershell 复制代码
#!/bin/bash

# 初始化ip地址变量为空
ip_address=""
file_name=""

# 使用getopts处理-p和-f选项
while getopts p:f: option
do
  case "${option}"
  in
  p) ip_address=${OPTARG};;
  f) file_name=${OPTARG};;
  esac
done

{ cat "$file_name"; printf '\nEOF\n'; } | nc "$ip_address" 12345

二、远程端:

powershell 复制代码
file_name=" "
# 使用getopts处理-p和-f选项
while getopts f: option
do
  case "${option}"
  in
  f) file_name=${OPTARG};;
  esac
done
nc -l -p 12345 | while IFS= read -r line
do
  if [[ "$line" == *EOF* ]]; then
    pkill nc
  else
    echo "$line" >> $file_name
  fi
done
echo "file pass successfully!"

三、执行

1. 先远程端:sh receive.sh -f {文件名称}

2. 后本地端:sh server_send.sh -p {远程ip} -f {文件名称}

相关推荐
多弗朗皮卡丘6 小时前
网络基础:数据链路层与其他协议或技术
linux·网络·tcp/ip
速捷工控 PLC触摸屏还原程序10 小时前
欧姆龙NB上传密码忘了,换屏前怎么保留原程序?
运维·服务器·欧姆龙
爱勇宝11 小时前
SSL 证书别再手动续了:acme.sh + Let's Encrypt + 云厂商 DNS 全自动实战
运维·服务器
dingdingfish12 小时前
TLPI 第52章 读书笔记:POSIX Message Queues
linux·mq·ipc·posix·message·tlpi
wacsii13 小时前
【Linux进程(五)】进程控制全解析:从创建、终止、等待到程序替换
linux·服务器
七夜zippoe14 小时前
Nexent Linux 部署:用 deploy.sh
linux·服务器·docker·部署·ai智能体·nexent
去码头整点薯条9814 小时前
NFS服务器
linux·运维·服务器
蜗牛一步步往上爬15 小时前
第一章 初识Linux shell
linux·运维·服务器
拂拉氏15 小时前
【知识讲解】 Linux线程中锁的认识
linux·互斥锁