Package: simplenetlink
Simplenetlink is an abstraction layer to simplify programatic non-persistent network configuration for linux on top of pyroute2. It was written to have a simple interface for network test setups.FeaturesSimplenetlink has following features:create and manage linux namespacesmove interfaces between namespacescreate tagged vlan interfacescreate ipvlan interfacesconfigure ipv4 addressesidempotentInstallationInstall simplenetlink by running:pip3installsimplenetlinkExamplesCreate Tagged VLAN interface in namespace and set IPv4 configurationfromsimplenetlinkimportSimpleNetlinkfrompprintimportpprintip=SimpleNetlink()#create vlan interface in new or existing namespace test with ipv4 address and implicitly switch to namespace testip.ensure_interface_exists("vlan2",namespace='test',link_state='up',parent_interface='eth0',type='tagged',vlan_id='2',ipv4=['100.64.0.10/24'])#show interfaces of namespace testpprint(ip.get_network_interfaces_info())#move tagged interface from namespace test to namespace test2 and implicitly switch to namespace test2ip.ensure_interface_exists("vlan2",namespace='test2',link_state='up',parent_interface='eth0',type='tagged',vlan_id='2',ipv4=['100.64.0.10/24'])#show interfaces of namespace test2pprint(ip.get_network_interfaces_info())# remove namespace testip.delete_namespace('test')# remove namespace test2ip.delete_namespace('test2')Create IPVLAN interface in namespace and set IPv4 configurationfromsimplenetlinkimportSimpleNetlinkfrompprintimportpprintip=SimpleNetlink()#create ipvlan interface in new or existing namespace test with ipv4 address and implicitly switch to namespace testip.ensure_interface_exists("ipvlan_test",namespace='test',link_state='up',parent_interface='eth0',type='ipvlan',ipv4=['100.64.0.11/24'])#show interfaces of namespace testpprint(ip.get_network_interfaces_info())#move ipvlan interface from namespace test to namespace test2 and implicitly switch to namespace test2ip.ensure_interface_exists("ipvlan_test",namespace='test2',link_state='up',parent_interface='eth0',type='ipvlan',ipv4=['100.64.0.11/24'])#show interfaces of namespace test2pprint(ip.get_network_interfaces_info())# remove namespace testip.delete_namespace('test')# remove namespace test2ip.delete_namespace('test2')Add route to namespacefromsimplenetlinkimportSimpleNetlink# switch to namespace test (will be created if not exists)ip.set_current_namespace('test')#create ipvlan interface in new or existing namespace test with ipv4 address and implicitly switch to namespace testip.ensure_interface_exists("ipvlan_test",namespace='test',link_state='up',parent_interface='eth0',type='ipvlan',ipv4=['100.64.0.11/24'])#add default route to current namespace which is test at the momentip.add_route('0.0.0.0/0','100.64.0.1')#add route to current namespace which is test at the momentip.add_route('10.100.0.0/24','100.64.0.2')# delete namespace testip.delete_namespace('test')Configure multiple interfaces in multiple namespaces from a YAML descriptionfromsimplenetlinkimportSimpleNetlinkimportyamlfrompprintimportpprintip=SimpleNetlink()yml="""
vl1_1:
    namespace: test
    link_state: up
    parent_interface: eth0
    type: ipvlan
    ipv4:
        - 100.64.0.11/24
vl1_2:
    namespace: test2
    link_state: up
    parent_interface: eth0
    type: ipvlan
    ipv4:
        - 100.64.0.12/24
vl2:
    namespace: test3
    link_state: up
    parent_interface: eth0
    type: tagged
    vlan_id: 2
    ipv4:
        - 100.64.1.11/24
vl2_2:
    namespace: test4
    link_state: up
    parent_interface: vl2
    type: ipvlan
    ipv4:
        - 100.64.1.12/24

"""config=yaml.safe_load(yml)# for k,v in config.items():#     if v.get('namespace'):#         ip.delete_namespace(v.get('namespace'))fork,vinconfig.items():ip.ensure_interface_exists(k,**v)ip.set_current_namespace('test')ip.add_route('0.0.0.0/0','100.64.0.1')ip.set_current_namespace('test2')ip.add_route('0.0.0.0/0','100.64.0.1')ip.set_current_namespace('test3')ip.add_route('0.0.0.0/0','100.64.1.1')ip.set_current_namespace('test4')ip.add_route('0.0.0.0/0','100.64.1.1')frompprintimportpprintfornamespaceinip.get_namespaces():ip.set_current_namespace(namespace)pprint(ip.get_network_interfaces_info())ContributeIssue Tracker:https://github.com/jinjamator/simplenetlink/issuesSource Code:https://github.com/jinjamator/simplenetlinkRoadmapSelected Roadmap items:add support for more virtual interface types e.g.: macvlan, vxlan, bridgesadd support for ipv6add class documentationFor documentation please refer tohttps://simplenetlink.readthedocs.io/en/latest/LicenseThis project is licensed under the Apache License Version 2.0