您现在的位置是:首页 > 文章详情

Python的pyroute2网络模块

日期:2018-05-27点击:581


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

 

 

 

 

 

 

原文链接:https://yq.aliyun.com/articles/597070
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章