Python的pyroute2网络模块
Pyroute2是纯python的netlink库,只需要python标准库不需要其他第三方的库。 最常用的是监控事件,例如监控磁盘空间事件: from pyroute2 import DQuotSocket with DQuotSocket() as ds: for message in ds.get(): print(message) 或者监控IP路由 from pyroute2 import IPRoute with IPRoute() as ipr: # With IPRoute objects you have to call bind() manually ipr.bind() for message in ipr.get(): print(message) 1. IPRoute配置网络 from pyroute2 import IPRoute ipr = IPRoute() # create an interface ipr.link('add', ifname='brx', kind='bridge') # lookup the index dev = ipr.link_lookup(ifname='brx')[0] # bring it down ipr.link('set', index=dev, state='down') # change the interface MAC address and rename it just for fun ipr.link('set', index=dev, address='00:11:22:33:44:55', ifname='br-ctrl') # add primary IP address ipr.addr('add', index=dev, address='10.0.0.1', mask=24, broadcast='10.0.0.255') # add secondary IP address ipr.addr('add', index=dev, address='10.0.0.2', mask=24, broadcast='10.0.0.255') # bring it up ipr.link('set', index=dev, state='up') 2. 参考 源码库:https://github.com/svinota/pyroute2 官方文档:http://docs.pyroute2.org/iproute.html IPRoute-tc:https://www.mankier.com/8/tc