嵌入式设备中上传文件方法总结
字数 910 2025-08-22 12:22:48
嵌入式设备中上传文件方法总结
前言
在研究嵌入式设备时,经常需要向设备上传二进制文件。本文将详细介绍几种常见的文件上传方法,包括FTP、TFTP、Python临时服务器和OpenSSL方法。
1. FTP服务上传
1.1 使用busybox自带的ftpd服务
BusyBox通常自带ftpd服务,无需身份验证即可进行文件传输。
启动方式一:通过tcpsvd启动
tcpsvd 0 21 ftpd -w /ftp_dir
参数说明:
-w:允许上传-v:打印错误信息-S:将错误信息写入SYSLOG-t N:设置空闲超时时间(默认2分钟)-T N:设置空闲后自动断开连接时间(默认1小时)
启动方式二:通过inetd.conf配置
编辑/etc/inetd.conf文件:
21 stream tcp nowait root ftpd ftpd -w /ftp_dir
然后启动inetd服务:
/usr/sbin/inetd &
1.2 tcpsvd参数详解
tcpsvd [选项] IP PORT PROG [PROG ARGS]
常用选项:
-l NAME:设置本地主机名-u USER[:GRP]:绑定后切换到指定用户/组-c N:设置最大连接数-C N[:MSG]:设置同一IP的最大连接数(MSG为超过时的响应信息)-v:打印详细输出
2. TFTP服务上传
BusyBox内置tftpd服务器,适合小文件传输。
2.1 服务端配置
mkdir /tftp_dir
cp /bin/busybox /tftp_dir
udpsvd -vE 0 69 tftpd -c /tftp_dir &
说明:
0表示监听所有IP地址-c允许客户端上传文件
2.2 客户端操作
上传文件:
busybox tftp -l test.txt -r test.txt -p 192.168.1.100
下载文件:
busybox tftp -l test.txt -r test.txt -g 192.168.1.100
3. Python临时服务器
适用于设备中安装了Python的环境。
3.1 服务器脚本
import socket
import base64
port = 55555
filename = 'test.bin'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', port))
sock.listen(5)
while True:
connection,address = sock.accept()
try:
content = 'hello'
f = file(filename)
content = base64.b64encode(f.read())
connection.sendall(content.strip())
connection.close()
except socket.timeout:
print 'time out'
connection.close()
3.2 客户端接收并解码
telnet 127.0.0.1 55555 | tee > temp.txt
tail -n +4 temp.txt > temp2.txt
base64 -d < temp2.txt | tee > test.bin
4. OpenSSL方法上传
适用于只有telnet权限且没有Python等工具的环境。
4.1 文件编码
import base64
fin = open("./hello", "rb")
fout = open("./base64.txt", "w")
data = fin.read()
base64_str = base64.encodestring(data).decode('ascii')
fout.write(base64_str.replace("\n", "").replace("\r", ""))
fin.close()
fout.close()
4.2 编码字符串整理
由于OpenSSL命令行解码限制,需要每64字符换行且一次最多处理约2000行:
f = open('./base64.txt', 'r')
result = list()
i = 64
size = len(f.read())
f.seek(0,0)
lines = 1
while i < size:
str1 = f.read(64)
str2 = str1 + '\r'
result.append(str2)
i = i + 64
lines = lines + 1
if i > size:
str1 = f.read(size-i)
str2 = str1 + '\r'
result.append(str2)
lines = lines + 1
l = 0
num = 1
while l < lines:
open('result' + str(num) + '.txt', 'w').write('%s' % '\n'.join(result[l:l+2000]))
num = num + 1
l = l + 2000
if l > lines:
open('result' + str(num) + '.txt', 'w').write('%s' % '\n'.join(result[l:]))
f.close()
4.3 通过telnet上传解码
手动方式:
echo "编码字符串" | openssl enc -base64 -d >> 目标文件
Python自动化脚本:
import getpass
import telnetlib
HOST = "192.168.1.100"
user = "admin"
password = "yourpasswd"
tn = telnetlib.Telnet(HOST)
tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
k = 13
filename = 'busybox'+ str(k) +'.txt'
f = open(filename, 'r')
tn.write(b"echo \"\n")
for i in range(0, 2000):
str1 = f.readline().encode('ascii')
tn.write(str1)
i = i + 1
tn.write(b"\"| openssl enc -base64 -d >> /ftp_dir/busybox_test\n")
tn.write(b"exit\n")
print(tn.read_all().decode('ascii'))
总结
- FTP方法:适合有网络服务且支持FTP的设备,配置简单
- TFTP方法:适合小文件传输,依赖UDP协议
- Python方法:灵活但需要Python环境,适合中等大小文件
- OpenSSL方法:适用于受限环境,但操作复杂,适合小文件
根据目标设备的环境和文件大小,选择最适合的上传方法。