直接用Bash发送HTTP请求 —— 筑梦之路

bash 复制代码
追踪命令执行

strace -e trace=network,open,close bash -c 'exec 3<>/dev/tcp/example.org/80'

关键部分

socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
connect(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("93.184.215.14")}, 16) = 0

检查文件描述符

ls -lat /proc/self/fd/

lrwx------ 1 user user 64 Aug 25 15:01 0 -> /dev/pts/0
lrwx------ 1 user user 64 Aug 25 15:01 1 -> /dev/pts/1
lrwx------ 1 user user 64 Aug 25 15:01 2 -> /dev/pts/2
lrwx------ 1 user user 64 Aug 25 15:01 3 -> 'socket:[2910856]'
lr-x------ 1 user user 64 Aug 25 15:01 4 -> /proc/1579316/fd

3是连接到example.org的套接字的文件描述符

重点来了:

exec 3<> /dev/tcp/...这个语法。它的含义是: "创建一个用于输入和输出操作的套接字,并将其分配给进程标识符为3的文件描述符。


发送http请求

echo -ne "GET / HTTP/1.1\r\nHost: example.org\r\n\r\n" >&3

读取响应结果

cat <&3

HTTP/1.1 200 OK
Age: 243157
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sun, 25 Aug 2024 14:15:54 GMT
Etag: "3147526947+ident"
Expires: Sun, 01 Sep 2024 14:15:54 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECAcc (dcd/7D77)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    ...
</head>
<body>
    <div>
        <h1>Example Domain</h1>
        <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p>
        <p><a href="https://www.iana.org/domains/example">More information...</a></p>
    </div>
</body>
</html>


关闭套接字

exec 3<&-

搜集来自掌握Bash的秘密:直接用Bash发送HTTP请求的终极指南

相关推荐
开源技术9 小时前
DNS详解——域名是如何解析的
http
三水不滴16 小时前
有 HTTP 了为什么还要有 RPC?
经验分享·笔记·网络协议·计算机网络·http·rpc
dingdingfish18 小时前
Bash学习 - 第3章:Basic Shell Features,第5节:Shell Expansions
开发语言·学习·bash
xuchaoxin137519 小时前
bash@脚本中的命令行参数解析基础@shift命令@getopts
bash
BLSxiaopanlaile19 小时前
《凤凰架构-构建可靠的大型分布式系统》读书笔记 -关于网络通信安全性的一些总结
http·加密·认证授权·网络通信安全
礼拜天没时间.21 小时前
自定义镜像制作——从Dockerfile到镜像
linux·docker·容器·centos·bash
源文雨1 天前
shell调用ffmpeg递归转换所有wav至flac的脚本
ffmpeg·bash·音视频·音频·unix·shell·音频编码
寻星探路1 天前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
王达舒19941 天前
HTTP vs HTTPS: 终极解析,保护你的数据究竟有多重要?
网络协议·http·https
朱皮皮呀1 天前
HTTPS的工作过程
网络协议·http·https