Python的pyroute2网络模块

  • 时间:
  • 浏览:2

# bring it up

         ifname='br-ctrl')

         address='10.0.0.1', mask=24,

         broadcast='10.0.0.255')

from pyroute2 import DQuotSocket

# change the interface MAC address and rename it just for fun

官方文档:http://docs.pyroute2.org/iproute.html

然后监控IP路由

dev = ipr.link_lookup(ifname='brx')[0]

from pyroute2 import IPRoute

ipr.addr('add', index=dev,

from pyroute2 import IPRoute

         address='10.0.0.2', mask=24,

ipr.addr('add', index=dev,

ipr.link('set', index=dev, state='up')

IPRoute-tc:https://www.mankier.com/8/tc

ipr.link('set', index=dev, state='down')

         broadcast='10.0.0.255')

        print(message)

with IPRoute() as ipr:

最常用的是监控事件,类似于监控磁盘空间事件:

    # With IPRoute objects you have to call bind() manually

# create an interface

        print(message)

# add secondary IP address

# add primary IP address

# bring it down

ipr = IPRoute()

    for message in ds.get():

# lookup the index

    ipr.bind()

ipr.link('add', ifname='brx', kind='bridge')

Pyroute2是纯python的netlink库,只时要python标准库不时要一些第三方的库。

源码库:https://github.com/svinota/pyroute2

         address='00:11:22:33:44:55',

ipr.link('set', index=dev,

    for message in ipr.get():

with DQuotSocket() as ds: