How to assign IP address to interface in python?

后端 未结 3 686
青春惊慌失措
青春惊慌失措 2021-02-06 01:29

I have python script that set the IP4 address for my wireless and wired interfaces. So far, I use subprocess command like :

subprocess.call([\"ip ad         


        
3条回答
  •  一向
    一向 (楼主)
    2021-02-06 02:03

    With pyroute2.IPRoute:

    from pyroute2 import IPRoute
    ip = IPRoute()
    index = ip.link_lookup(ifname='em1')[0]
    ip.addr('add', index, address='192.168.0.1', mask=24)
    ip.close()
    

    With pyroute2.IPDB:

    from pyroute2 import IPDB
    ip = IPDB()
    with ip.interfaces.em1 as em1:
        em1.add_ip('192.168.0.1/24')
    ip.release()
    

提交回复
热议问题