当前位置: 首页>>代码示例>>Python>>正文


Python IPRoute.rule方法代码示例

本文整理汇总了Python中pyroute2.IPRoute.rule方法的典型用法代码示例。如果您正苦于以下问题:Python IPRoute.rule方法的具体用法?Python IPRoute.rule怎么用?Python IPRoute.rule使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyroute2.IPRoute的用法示例。


在下文中一共展示了IPRoute.rule方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestIPRoute

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import rule [as 别名]

#.........这里部分代码省略.........
        ifaddr2 = self.ifaddr()
        self.ip.addr('add', self.ifaces[0],
                     address=ifaddr1,
                     mask=24,
                     broadcast=ifaddr2)
        assert ifaddr2 in get_ip_brd()

    def test_addr_broadcast_default(self):
        require_user('root')
        ifaddr1 = self.ifaddr()  # -> 255
        ifaddr2 = self.ifaddr()  # -> 254
        self.ip.addr('add', self.ifaces[0],
                     address=ifaddr2,
                     mask=24,
                     broadcast=True)
        assert ifaddr1 in get_ip_brd()

    def test_flush_addr(self):
        require_user('root')
        ifaddr1 = self.ifaddr(0)
        ifaddr2 = self.ifaddr(0)
        ifaddr3 = self.ifaddr(1)
        ifaddr4 = self.ifaddr(1)
        self.ip.addr('add', self.ifaces[0], address=ifaddr1, mask=24)
        self.ip.addr('add', self.ifaces[0], address=ifaddr2, mask=24)
        self.ip.addr('add', self.ifaces[0], address=ifaddr3, mask=24)
        self.ip.addr('add', self.ifaces[0], address=ifaddr4, mask=24)
        assert len(self.ip.get_addr(index=self.ifaces[0],
                                    family=socket.AF_INET)) == 4
        self.ip.flush_addr(index=self.ifaces[0])
        assert len(self.ip.get_addr(index=self.ifaces[0],
                                    family=socket.AF_INET)) == 0

    def test_flush_rules(self):
        require_user('root')
        ifaddr1 = self.ifaddr(0)
        ifaddr2 = self.ifaddr(1)
        init = len(self.ip.get_rules(family=socket.AF_INET))
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        self.ip.rule('add', table=10, priority=110)
        self.ip.rule('add', table=15, priority=150, action='FR_ACT_PROHIBIT')
        self.ip.rule('add', table=20, priority=200, src=ifaddr1)
        self.ip.rule('add', table=25, priority=250, dst=ifaddr2)
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 4
        assert len(self.ip.get_rules(src=ifaddr1)) == 1
        assert len(self.ip.get_rules(dst=ifaddr2)) == 1
        self.ip.flush_rules(family=socket.AF_INET,
                            priority=lambda x: 100 < x < 500)
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        assert len(self.ip.get_rules(src=ifaddr1)) == 0
        assert len(self.ip.get_rules(dst=ifaddr2)) == 0
        assert len(self.ip.get_rules(family=socket.AF_INET)) == init

    def test_rules_deprecated(self):
        require_user('root')
        init = len(self.ip.get_rules(family=socket.AF_INET))
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        self.ip.rule('add', 10, 110)
        self.ip.rule('add', 15, 150, 'FR_ACT_PROHIBIT')
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 2
        self.ip.flush_rules(family=socket.AF_INET,
                            priority=lambda x: 100 < x < 500)
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        assert len(self.ip.get_rules(family=socket.AF_INET)) == init

    def test_match_callable(self):
开发者ID:svinota,项目名称:pyroute2,代码行数:70,代码来源:test_ipr.py

示例2: TestIPRoute

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import rule [as 别名]
class TestIPRoute(object):

    def setup(self):
        self.ip = IPRoute()
        try:
            self.dev, idx = self.create()
            self.ifaces = [idx]
        except IndexError:
            pass

    def create(self, kind='dummy'):
        name = uifname()
        create_link(name, kind=kind)
        idx = self.ip.link_lookup(ifname=name)[0]
        return (name, idx)

    def teardown(self):
        if hasattr(self, 'ifaces'):
            for dev in self.ifaces:
                try:
                    self.ip.link('delete', index=dev)
                except:
                    pass
        self.ip.close()

    def _test_nla_operators(self):
        require_user('root')
        self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
        self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24)
        r = [x for x in self.ip.get_addr() if x['index'] == self.ifaces[0]]
        complement = r[0] - r[1]
        intersection = r[0] & r[1]

        assert complement.get_attr('IFA_ADDRESS') == '172.16.0.1'
        assert complement.get_attr('IFA_LABEL') is None
        assert complement['prefixlen'] == 0
        assert complement['index'] == 0

        assert intersection.get_attr('IFA_ADDRESS') is None
        assert intersection.get_attr('IFA_LABEL') == self.dev
        assert intersection['prefixlen'] == 24
        assert intersection['index'] == self.ifaces[0]

    def test_addr_add(self):
        require_user('root')
        self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
        assert '172.16.0.1/24' in get_ip_addr()

    def test_flush_addr(self):
        require_user('root')
        self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
        self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24)
        self.ip.addr('add', self.ifaces[0], address='172.16.1.1', mask=24)
        self.ip.addr('add', self.ifaces[0], address='172.16.1.2', mask=24)
        assert len(self.ip.get_addr(index=self.ifaces[0],
                                    family=socket.AF_INET)) == 4
        self.ip.flush_addr(index=self.ifaces[0])
        assert len(self.ip.get_addr(index=self.ifaces[0],
                                    family=socket.AF_INET)) == 0

    def test_flush_rules(self):
        require_user('root')
        init = len(self.ip.get_rules(family=socket.AF_INET))
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        self.ip.rule('add', table=10, priority=110)
        self.ip.rule('add', table=15, priority=150, action='FR_ACT_PROHIBIT')
        self.ip.rule('add', table=20, priority=200, src='172.16.200.1')
        self.ip.rule('add', table=25, priority=250, dst='172.16.250.1')
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 4
        assert len(self.ip.get_rules(src='172.16.200.1')) == 1
        assert len(self.ip.get_rules(dst='172.16.250.1')) == 1
        self.ip.flush_rules(family=socket.AF_INET,
                            priority=lambda x: 100 < x < 500)
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        assert len(self.ip.get_rules(src='172.16.200.1')) == 0
        assert len(self.ip.get_rules(dst='172.16.250.1')) == 0
        assert len(self.ip.get_rules(family=socket.AF_INET)) == init

    def test_rules_deprecated(self):
        require_user('root')
        init = len(self.ip.get_rules(family=socket.AF_INET))
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        self.ip.rule('add', 10, 110)
        self.ip.rule('add', 15, 150, 'FR_ACT_PROHIBIT')
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 2
        self.ip.flush_rules(family=socket.AF_INET,
                            priority=lambda x: 100 < x < 500)
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        assert len(self.ip.get_rules(family=socket.AF_INET)) == init

    def test_addr_filter(self):
        require_user('root')
        self.ip.addr('add',
                     index=self.ifaces[0],
                     address='172.16.0.1',
                     prefixlen=24,
                     broadcast='172.16.0.255')
        self.ip.addr('add',
                     index=self.ifaces[0],
                     address='172.16.0.2',
#.........这里部分代码省略.........
开发者ID:thezeep,项目名称:pyroute2,代码行数:103,代码来源:test_ipr.py

示例3: TestIPRoute

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import rule [as 别名]

#.........这里部分代码省略.........
        address = link.get_attr('IFA_ADDRESS')
        local = link.get_attr('IFA_LOCAL')
        assert address == '172.16.0.2'
        assert local == '172.16.0.1'

    def test_addr_broadcast(self):
        require_user('root')
        self.ip.addr('add', self.ifaces[0],
                     address='172.16.0.1',
                     mask=24,
                     broadcast='172.16.0.250')
        assert '172.16.0.250' in get_ip_brd()

    def test_addr_broadcast_default(self):
        require_user('root')
        self.ip.addr('add', self.ifaces[0],
                     address='172.16.0.1',
                     mask=24,
                     broadcast=True)
        assert '172.16.0.255' in get_ip_brd()

    def test_flush_addr(self):
        require_user('root')
        self.ip.addr('add', self.ifaces[0], address='172.16.0.1', mask=24)
        self.ip.addr('add', self.ifaces[0], address='172.16.0.2', mask=24)
        self.ip.addr('add', self.ifaces[0], address='172.16.1.1', mask=24)
        self.ip.addr('add', self.ifaces[0], address='172.16.1.2', mask=24)
        assert len(self.ip.get_addr(index=self.ifaces[0],
                                    family=socket.AF_INET)) == 4
        self.ip.flush_addr(index=self.ifaces[0])
        assert len(self.ip.get_addr(index=self.ifaces[0],
                                    family=socket.AF_INET)) == 0

    def test_flush_rules(self):
        require_user('root')
        init = len(self.ip.get_rules(family=socket.AF_INET))
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        self.ip.rule('add', table=10, priority=110)
        self.ip.rule('add', table=15, priority=150, action='FR_ACT_PROHIBIT')
        self.ip.rule('add', table=20, priority=200, src='172.16.200.1')
        self.ip.rule('add', table=25, priority=250, dst='172.16.250.1')
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 4
        assert len(self.ip.get_rules(src='172.16.200.1')) == 1
        assert len(self.ip.get_rules(dst='172.16.250.1')) == 1
        self.ip.flush_rules(family=socket.AF_INET,
                            priority=lambda x: 100 < x < 500)
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        assert len(self.ip.get_rules(src='172.16.200.1')) == 0
        assert len(self.ip.get_rules(dst='172.16.250.1')) == 0
        assert len(self.ip.get_rules(family=socket.AF_INET)) == init

    def test_rules_deprecated(self):
        require_user('root')
        init = len(self.ip.get_rules(family=socket.AF_INET))
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        self.ip.rule('add', 10, 110)
        self.ip.rule('add', 15, 150, 'FR_ACT_PROHIBIT')
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 2
        self.ip.flush_rules(family=socket.AF_INET,
                            priority=lambda x: 100 < x < 500)
        assert len(self.ip.get_rules(priority=lambda x: 100 < x < 500)) == 0
        assert len(self.ip.get_rules(family=socket.AF_INET)) == init

    def test_addr_filter(self):
        require_user('root')
        self.ip.addr('add',
开发者ID:pwns4cash,项目名称:pyroute2,代码行数:70,代码来源:test_ipr.py

示例4: TestRule

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import rule [as 别名]
class TestRule(object):

    def setup(self):
        require_user('root')
        self.ip = IPRoute()
        self.ifname = uifname()
        self.ip.link('add', ifname=self.ifname, kind='dummy')
        self.interface = self.ip.link_lookup(ifname=self.ifname)[0]

    def teardown(self):
        self.ip.link('delete', index=self.interface)
        self.ip.close()

    def test_basic(self):
        self.ip.rule('add', 10, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32000 and
                    x.get_attr('FRA_TABLE') == 10]) == 1
        self.ip.rule('delete', 10, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32000 and
                    x.get_attr('FRA_TABLE') == 10]) == 0

    def test_fwmark(self):
        self.ip.rule('add', 15, 32006, fwmark=10)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK')]) == 1
        self.ip.rule('delete', 15, 32006, fwmark=10)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK')]) == 0

    def test_fwmark_mask_normalized(self):
        self.ip.rule('add', 15, 32006, fwmark=10, fwmask=20)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK') and
                    x.get_attr('FRA_FWMASK')]) == 1
        self.ip.rule('delete', 15, 32006, fwmark=10, fwmask=20)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK') and
                    x.get_attr('FRA_FWMASK')]) == 0

    def test_fwmark_mask_raw(self):
        self.ip.rule('add', 15, 32006, fwmark=10, FRA_FWMASK=20)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK') and
                    x.get_attr('FRA_FWMASK')]) == 1
        self.ip.rule('delete', 15, 32006, fwmark=10, FRA_FWMASK=20)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32006 and
                    x.get_attr('FRA_TABLE') == 15 and
                    x.get_attr('FRA_FWMARK') and
                    x.get_attr('FRA_FWMASK')]) == 0

    def test_bad_table(self):
        try:
            self.ip.rule('add', -1, 32000)
        except Exception:
            pass

    def test_big_table(self):
        self.ip.rule('add', 1024, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32000 and
                    x.get_attr('FRA_TABLE') == 1024]) == 1
        self.ip.rule('delete', 1024, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32000 and
                    x.get_attr('FRA_TABLE') == 1024]) == 0

    def test_src_dst(self):
        self.ip.rule('add', 17, 32005,
                     src='10.0.0.0', src_len=24,
                     dst='10.1.0.0', dst_len=24)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32005 and
                    x.get_attr('FRA_TABLE') == 17 and
                    x.get_attr('FRA_SRC') == '10.0.0.0' and
                    x.get_attr('FRA_DST') == '10.1.0.0' and
                    x['src_len'] == 24 and
                    x['dst_len'] == 24]) == 1
        self.ip.rule('delete', 17, 32005,
                     src='10.0.0.0', src_len=24,
                     dst='10.1.0.0', dst_len=24)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('FRA_PRIORITY') == 32005 and
                    x.get_attr('FRA_TABLE') == 17 and
                    x.get_attr('FRA_SRC') == '10.0.0.0' and
                    x.get_attr('FRA_DST') == '10.1.0.0' and
                    x['src_len'] == 24 and
                    x['dst_len'] == 24]) == 0
开发者ID:0xD3ADB33F,项目名称:pyroute2,代码行数:102,代码来源:test_rule.py

示例5: TestRule

# 需要导入模块: from pyroute2 import IPRoute [as 别名]
# 或者: from pyroute2.IPRoute import rule [as 别名]
class TestRule(object):

    def setup(self):
        require_user('root')
        self.ip = IPRoute()
        self.ip.link('add',
                     index=0,
                     ifname='dummyX',
                     linkinfo={'attrs': [['IFLA_INFO_KIND', 'dummy']]})
        self.interface = self.ip.link_lookup(ifname='dummyX')[0]

    def teardown(self):
        self.ip.link('delete', index=self.interface)
        self.ip.close()

    def test_basic(self):
        self.ip.rule('add', 10, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('RTA_PRIORITY') == 32000 and
                    x.get_attr('RTA_TABLE') == 10]) == 1
        self.ip.rule('delete', 10, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('RTA_PRIORITY') == 32000 and
                    x.get_attr('RTA_TABLE') == 10]) == 0

    def test_fwmark(self):
        self.ip.rule('add', 15, 32006, fwmark=10)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('RTA_PRIORITY') == 32006 and
                    x.get_attr('RTA_TABLE') == 15 and
                    x.get_attr('RTA_PROTOINFO')]) == 1
        self.ip.rule('delete', 15, 32006, fwmark=10)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('RTA_PRIORITY') == 32006 and
                    x.get_attr('RTA_TABLE') == 15 and
                    x.get_attr('RTA_PROTOINFO')]) == 0

    def test_bad_table(self):
        try:
            self.ip.rule('add', -1, 32000)
        except ValueError:
            pass

    def test_big_table(self):
        self.ip.rule('add', 1024, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('RTA_PRIORITY') == 32000 and
                    x.get_attr('RTA_TABLE') == 1024]) == 1
        self.ip.rule('delete', 1024, 32000)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('RTA_PRIORITY') == 32000 and
                    x.get_attr('RTA_TABLE') == 1024]) == 0

    def test_src_dst(self):
        self.ip.rule('add', 17, 32005,
                     src='10.0.0.0', src_len=24,
                     dst='10.1.0.0', dst_len=24)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('RTA_PRIORITY') == 32005 and
                    x.get_attr('RTA_TABLE') == 17 and
                    x.get_attr('RTA_SRC') == '10.0.0.0' and
                    x.get_attr('RTA_DST') == '10.1.0.0' and
                    x['src_len'] == 24 and
                    x['dst_len'] == 24]) == 1
        self.ip.rule('delete', 17, 32005,
                     src='10.0.0.0', src_len=24,
                     dst='10.1.0.0', dst_len=24)
        assert len([x for x in self.ip.get_rules() if
                    x.get_attr('RTA_PRIORITY') == 32005 and
                    x.get_attr('RTA_TABLE') == 17 and
                    x.get_attr('RTA_SRC') == '10.0.0.0' and
                    x.get_attr('RTA_DST') == '10.1.0.0' and
                    x['src_len'] == 24 and
                    x['dst_len'] == 24]) == 0
开发者ID:0x90,项目名称:pyroute2,代码行数:76,代码来源:test_rule.py


注:本文中的pyroute2.IPRoute.rule方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。