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


Python CiscoConfParse.insert_after方法代码示例

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


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

示例1: testValues_insert_after_atomic_factory_01

# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import insert_after [as 别名]
    def testValues_insert_after_atomic_factory_01(self):
        """Ensure that comments which are added, assert is_comment"""
        with patch('__main__.IOSCfgLine') as mock:
            # the mock pretends to be an IOSCfgLine so we can test against it
            result_correct01 = mock
            result_correct01.linenum = 16
            result_correct01.text = ' ! TODO: some note to self'
            result_correct01.classname = 'IOSCfgLine'
            result_correct01.is_comment = True

        result_correct02 = [
            'interface GigabitEthernet4/1', 
            ' ! TODO: some note to self',
            ' switchport', 
            ' switchport access vlan 100', 
            ' switchport voice vlan 150', 
            ' power inline static max 7000',
            ]
        linespec = 'interface GigabitEthernet4/1'
        cfg = CiscoConfParse(self.c01, factory=True)
        cfg.insert_after(linespec, ' ! TODO: some note to self', 
            exactmatch=True, atomic=True)

        test_result01 = cfg.find_objects('TODO')[0]
        self.assertEqual(result_correct01.linenum, test_result01.linenum)
        self.assertEqual(result_correct01.text, test_result01.text)
        self.assertEqual(result_correct01.classname, test_result01.classname)
        self.assertEqual(result_correct01.is_comment, test_result01.is_comment)

        # FIXME: this fails... maybe because I don't parse comments as children correctly???
        test_result02 = cfg.find_children(linespec)
        self.assertEqual(result_correct02, test_result02)
开发者ID:ashkhan007,项目名称:ciscoconfparse,代码行数:34,代码来源:testCiscoConfParse.py

示例2: testValues_insert_after_nonatomic_02

# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import insert_after [as 别名]
    def testValues_insert_after_nonatomic_02(self):
        """Negative test.  We expect insert_after(atomic=False) to miss any children added like this at some point in the future I might fix insert_after so it knows how to correctly parse children"""
        result_correct = ['interface GigabitEthernet4/1', 
            #' shutdown',   <--- Intentionally commented out
            ' switchport', 
            ' switchport access vlan 100', 
            ' switchport voice vlan 150', 
            ' power inline static max 7000',
            ]
        linespec = 'interface GigabitEthernet4/1'
        cfg = CiscoConfParse(self.c01, factory=False)
        cfg.insert_after(linespec, ' shutdown', exactmatch=True, atomic=False)
        test_result = cfg.find_children(linespec)

        self.assertEqual(result_correct, test_result)
开发者ID:ashkhan007,项目名称:ciscoconfparse,代码行数:17,代码来源:testCiscoConfParse.py

示例3: testValues_insert_after_atomic_01

# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import insert_after [as 别名]
    def testValues_insert_after_atomic_01(self):
        """We expect insert_after(atomic=True) to correctly parse children"""
        ## See also -> testValues_insert_after_nonatomic_02()
        result_correct = [
            'interface GigabitEthernet4/1', 
            ' shutdown',
            ' switchport', 
            ' switchport access vlan 100', 
            ' switchport voice vlan 150', 
            ' power inline static max 7000',
            ]
        linespec = 'interface GigabitEthernet4/1'
        cfg = CiscoConfParse(self.c01, factory=False)
        cfg.insert_after(linespec, ' shutdown', exactmatch=True, atomic=True)
        test_result = cfg.find_children(linespec)

        self.assertEqual(result_correct, test_result)
开发者ID:ashkhan007,项目名称:ciscoconfparse,代码行数:19,代码来源:testCiscoConfParse.py

示例4: testValues_insert_after_nonatomic_01

# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import insert_after [as 别名]
 def testValues_insert_after_nonatomic_01(self):
     inputs = ['interface GigabitEthernet4/1',
         'interface GigabitEthernet4/2',
         'interface GigabitEthernet4/3',
         'interface GigabitEthernet4/4',
         'interface GigabitEthernet4/5',
         'interface GigabitEthernet4/6',
         'interface GigabitEthernet4/7',
         'interface GigabitEthernet4/8',
         ]
     cfg = CiscoConfParse(self.c01, factory=False)
     for idx, linespec in enumerate(inputs):
         test_result = cfg.insert_after(linespec, ' shutdown',
             exactmatch=True, atomic=False)
         result_correct = [inputs[idx]]
         self.assertEqual(result_correct, test_result)
开发者ID:ashkhan007,项目名称:ciscoconfparse,代码行数:18,代码来源:testCiscoConfParse.py

示例5: standardize_configs

# 需要导入模块: from ciscoconfparse import CiscoConfParse [as 别名]
# 或者: from ciscoconfparse.CiscoConfParse import insert_after [as 别名]
def standardize_configs(ip, chan):

    print "Downloading running configuration"
    output = send_command("enable", "!Tmgmt", "skip", "sh run", read=True, chan=chan)

    # Captures running config
    capture = re.findall(r"!\r\nver.*end", output, re.S).pop()

    # Parses running config
    print "Parsing configuraion"
    output = CiscoConfParse(capture.splitlines())

    # Corrects DNS Setting
    dns = output.find_lines(r"ip dns server-address.*")
    output.replace_lines(r"ip dns server-address.*", r"ip dns server-address 10.210.1.214 10.220.1.200")
    print ("Correcting dns entry")
    send_command(
        "no {0}".format(dns.pop()), "ip dns server-address 10.210.1.214 10.220.1.200", configure=True, chan=chan
    )

    # Enables IGMP Snooping globally
    output.insert_after(r"hostname", r"ip multicast active")
    print ('Enabling "ip multicast"')
    send_command("ip multicast active", configure=True, chan=chan)

    # print 'Searching for interfaces with dhcp helper'
    # virtual_interfaces = output.find_objects_w_child(r'interface ve.*', r'\s+ip\s+helper.*',)
    # for intf in virtual_interfaces:
    #     interface = intf.text
    #     print interface
    #     has_dhcp_helper = intf.has_child_with(r'ip helper.*')
    #     for line in intf.all_children:
    #         if 'ip address' in line.text:
    #             address = line.text
    #             print address
    #         if 'helper-address' in line.text:
    #             dhcp = line.text
    #             print dhcp
    #     if has_dhcp_helper:
    #         writer.writerow((ip, interface, address, dhcp))
    #
    # Iterates through interfaces and cleans up misconfigurations
    for interface in output.find_objects(r"^interface.+"):
        has_loop_detection = interface.has_child_with(r"loop-detection")
        has_bpdu_guard = interface.has_child_with(r"stp-bpdu-guard")
        has_root_protection = interface.has_child_with(r"spanning-tree\sroot-protect")
        has_no_flow_control = interface.has_child_with(r"no\sflow-control")
        is_layer3_intf = (interface.has_child_with(r"ip\s+add")) or ("interface ve.*" in interface.text)

        # Temporarily disabled

        # Remove loop-detection misconfiguration
        if has_loop_detection and has_bpdu_guard:
            interface.delete_children_matching("loop-detection")
            print ('Removing "loop-detection" from {0}'.format(interface.text))
            send_command(interface.text, "no loop-detection", configure=True)

        # Remove spanning-tree root-protect misconfiguration
        if has_root_protection:
            interface.delete_children_matching(r"spanning-tree\sroot-protect")
            print ('Removing "spanning-tree root-protect" from {0}'.format(interface.text))
            send_command(interface.text, "no spanning-tree root", configure=True)

        # Adds IGMP snooping and QoS to Layer 3 interfaces
        if is_layer3_intf and not ("loopback" in interface.text or "management" in interface.text):
            interface.append_to_family(r" trust dscp")
            print ('Adding "trust dscp" to {0}'.format(interface.text))
            send_command(interface.text, "trust dscp", configure=True, chan=chan)
            interface.append_to_family(r" ip igmp version 2")
            print ('Adding "ip igmp version 2" to {0}'.format(interface.text))
            send_command(interface.text, "ip igmp version 2", configure=True, chan=chan)

        # enables flow-control
        if has_no_flow_control:
            interface.delete_children_matching(r"no\sflow-control.+")
            print ('Enabling "flow-control" on {0}'.format(interface.text))
            send_command(interface.text, "flow-control", configure=True, chan=chan)
    return output
开发者ID:dcrozier,项目名称:PyTest,代码行数:80,代码来源:standardize_configurations.py


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