在 Linux 上,可以通过一些常用工具验证 HTTP(S),socks 代理是否有效。下面是几种简单的方法:
1. 使用 telnet
或 nc
验证代理端口
如果想简单地验证代理服务器是否能连接,可以使用 telnet
或 nc
来检查代理服务器的端口是否开启并可连接。可以排查是否是防火墙原因。
使用 telnet
1
|
telnet <proxy-server> <port>
|
使用 nc
(Netcat)
1
|
nc -zv <proxy-server> <port>
|
如果连接成功,说明代理服务器的端口可以访问,您会看到类似 Connected
的提示。如果连接失败,可能会显示 Connection refused
或 Operation timed out
等信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# 连接失败,无回应,可能服务不存在
[root@localhost ~]# telnet 172.16.7.88 12345
Trying 172.16.7.88...
^C
# 连接失败,Connection refused,防火墙的原因
[root@localhost ~]# telnet 172.16.7.250 7890
Trying 172.16.7.250...
telnet: connect to address 172.16.7.250: Connection refused
# 连接成功
[root@localhost ~]# telnet 172.16.7.250 7891
Trying 172.16.7.250...
Connected to 172.16.7.250.
Escape character is '^]'.
Connection closed by foreign host.
|
1
2
3
4
5
6
7
8
9
10
11
12
|
# 无返回,失败
(base) [root@localhost ~]# nc -zv 172.16.7.88 7890
Ncat: Version 7.50 ( https://nmap.org/ncat )
# 连接被拒绝,防火墙的原因
[root@localhost ~]# nc -zv 172.16.7.250 7889
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connection refused.
# 连接成功
[root@localhost ~]# nc -zv 172.16.7.250 7890
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 172.16.7.250:7890.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.
|
2. 使用 curl
验证 HTTP 代理
curl
是一个常用的命令行工具,可以很方便地指定代理并发送请求。假设您的代理服务器地址为 http://<proxy-server>:<port>
,可以使用以下命令来验证代理是否正常工作:
1
|
curl -x http://<proxy-server>:<port> http://example.com
|
解释:
-x
选项用于指定代理服务器。
http://example.com
是目标 URL,可以换成任何其他网站。
1
2
3
4
5
6
7
8
9
10
11
12
|
# 不走(系统)代理
[root@localhost ~]# curl https://ifconfig.me
123.234.22.19
# http代理
[root@localhost ~]# curl -x http://172.16.7.94:7890 https://ifconfig.me
140.238.37.247
# https代理
[root@localhost ~]# curl -x https://172.16.7.94:7890 https://ifconfig.me
140.238.37.247
# socks代理
[root@localhost ~]# curl -x socks5://172.16.7.94:7890 https://ifconfig.me
140.238.37.247
|
如果代理生效,返回的 IP 地址应该是代理服务器的 IP,而不是公网IP。
3. 使用 wget
验证 HTTP 代理
wget
也是一个常用的命令行工具,可以通过它来指定代理并测试网络请求。假设代理服务器地址为 http://<proxy-server>:<port>
,可以使用以下命令:
1
|
wget -e use_proxy=on -e http_proxy=http://<proxy-server>:<port> http://example.com
|
解释:
-e use_proxy=on
告诉 wget
使用代理。
-e http_proxy
设置 HTTP 代理服务器。
http://example.com
是测试的目标 URL。
wget
会尝试通过代理下载该网页的内容,如果下载成功,则代理工作正常;如果失败,说明代理配置有问题。
1
2
3
4
5
6
7
8
9
|
# 不走代理
[root@localhost ~]# wget -q -O - https://ifconfig.me
123.234.22.19
# 配置http代理
[root@localhost ~]# wget -e use_proxy=on -e http_proxy=http://172.16.7.94:7890 -q -O - http://ifconfig.me
140.238.37.247
# 配置https代理
[root@localhost ~]# wget -e use_proxy=on -e https_proxy=http://172.16.7.94:7890 -q -O - https://ifconfig.me
140.238.37.247
|
4. 设置全局代理并测试
命令行工具使用代理的方法
可以在 Linux 环境中全局设置 HTTP 代理,然后通过其他网络工具(如 curl
或 ping
)来测试代理是否生效。
设置全局 HTTP 代理
1
2
3
|
export http_proxy=http://<proxy-server>:<port>
export https_proxy=http://<proxy-server>:<port>
export all_proxy=socks5://<proxy-server>:<port>
|
设置好后,您可以使用 curl
或其他工具测试网络请求是否通过代理。例如:
1
|
curl http://example.com
|
如果代理工作正常,curl
会通过代理服务器连接到目标网站。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# 未配置代理,https请求
[root@localhost ~]# curl https://ifconfig.me
123.234.22.19
# http请求
[root@localhost ~]# curl http://ifconfig.me
123.234.22.19
# 设置http代理
[root@localhost ~]# export http_proxy=http://172.16.7.94:7890
# 再次请求,由于设置了http代理,走http代理
[root@localhost ~]# curl http://ifconfig.me
140.238.37.247
# 再请请求https,但由于未设置https代理,所以没变
[root@localhost ~]# curl https://ifconfig.me
123.234.22.19
# 设置https代理
[root@localhost ~]# export https_proxy=http://172.16.7.94:7890
# 再起请求https
[root@localhost ~]# curl https://ifconfig.me
140.238.37.247
# 取消http代理
[root@localhost ~]# unset https_proxy
# 再次请求http
[root@localhost ~]# curl https://ifconfig.me
123.234.22.19
# 取消https
[root@localhost ~]# unset http_proxy
# 请求https
[root@localhost ~]# curl http://ifconfig.me
123.234.22.19
# 设置socks代理
[root@localhost ~]# export all_proxy=socks5://172.16.7.94:7890
# 请求http,发现也是走的socks代理
[root@localhost ~]# curl http://ifconfig.me
140.238.37.247
# 请求https,发现也是走的socks代理
[root@localhost ~]# curl https://ifconfig.me
140.238.37.247
# 取消socks代理
[root@localhost ~]# unset all_proxy
[root@localhost ~]# curl https://ifconfig.me
123.234.22.19
|