You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python ipaddress库验证IP地址结果异常问题排查

IP地址验证逻辑错误排查:ipaddress库与Linux实际配置不符问题

我尝试使用Python的ipaddress库验证IP、网关和子网掩码输入,但在实际使用已验证的网络时出现无效结果,怀疑验证逻辑有误。

验证代码

network = ipaddress.ip_network("103.162.136.145/255.255.255.252", strict=False)
for ip in ipaddress.ip_network(network):
    print(ip)

代码输出的IP范围

103.162.136.144
103.162.136.145
103.162.136.146
103.162.136.147

Linux配置操作及错误信息

localhost:~# ip addr flush dev eth0
localhost:~# ip addr add 103.162.136.145/255.255.255.252 dev eth0
localhost:~# ip link set eth0 up
localhost:~# ip route replace default via 103.162.136.144
ip: RTNETLINK answers: Invalid argument

实际正确的可用IP范围

103.162.136.145
103.162.136.146

注:103.162.136.145/255.255.255.252为IP/子网掩码,103.162.136.144为网关。请问我的验证逻辑哪里出错了?


内容的提问来源于stack exchange,提问作者user2384330

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 03:35:04