Python Linux路由表查找
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python Linux路由表查找,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1799字,纯文字阅读大概需要3分钟。
内容图文
![Python Linux路由表查找](/upload/InfoBanner/zyjiaocheng/968/70e1dbd17bc8412199c5125d6427bbfa.jpg)
我发布了Python find first network hop关于尝试找到第一跳的内容,我想的越多,它就越容易成为python中路由表的过程.我不是程序员,我不知道我在做什么. :p
这就是我提出的,我注意到的第一个问题是环回接口没有显示在/ proc / net / route文件中 – 所以评估127.0.0.0/8会给你默认路由…对于我申请,没关系.
还有什么重要我忽略了吗?解析ip route get< ip>还是个好主意?
import re
import struct
import socket
'''
Read all the routes into a list. Most specific first.
# eth0 000219AC 04001EAC 0003 0 0 0 00FFFFFF ...
'''
def _RtTable():
_rt = []
rt_m = re.compile('^[a-z0-9]*\W([0-9A-F]{8})\W([0-9A-F]{8})[\W0-9]*([0-9A-F]{8})')
rt = open('/proc/net/route', 'r')
for line in rt.read().split('\n'):
if rt_m.match(line):
_rt.append(rt_m.findall(line)[0])
rt.close()
return _rt
'''
Create a temp ip (tip) that is the entered ip with the host
section striped off. Matching to routers in order,
the first match should be the most specific.
If we get 0.0.0.0 as the next hop, the network is likely(?)
directly attached- the entered IP is the next (only) hop
'''
def FindGw(ip):
int_ip = struct.unpack("I", socket.inet_aton(ip))[0]
for entry in _RtTable():
tip = int_ip & int(entry[2], 16)
if tip == int(entry[0], 16):
gw_s = socket.inet_ntoa(struct.pack("I", int(entry[1], 16)))
if gw_s == '0.0.0.0':
return ip
else:
return gw_s
if __name__ == '__main__':
import sys
print FindGw(sys.argv[1])
解决方法:
在proc文件系统的手册页中给出了它.
/proc/net
various net pseudo-files, all of which give the status of some part of
the networking layer. These files contain ASCII structures and are,
there‐fore, readable with cat(1).
However, the standard netstat(8) suite provides much
cleaner access to these files.
只需依靠为这些目的设计的工具.使用netstat,traceroute或任何其他标准工具.使用子进程模块干净地包装这些命令,并获取您要查找的信息.
内容总结
以上是互联网集市为您收集整理的Python Linux路由表查找全部内容,希望文章能够帮你解决Python Linux路由表查找所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。