简体   繁体   English

命令行外壳,用于识别没有IP地址的接口

[英]command line shell for identifying interfaces without IP address

I am trying to filter interfaces within my machine which does not have an ip address assigned. 我正在尝试过滤我的机器中未分配IP地址的接口。 My ifconfig output is as follows 我的ifconfig输出如下

eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.200.8.99  netmask 255.255.255.0  broadcast 10.200.8.255
        inet6 fe80::ec4:7aff:fe32:92fa  prefixlen 64  scopeid 0x20<link>
        ether 0c:c4:7a:32:92:fa  txqueuelen 1000  (Ethernet)
        RX packets 447580  bytes 44008067 (41.9 MiB)
        RX errors 0  dropped 39  overruns 0  frame 0
        TX packets 556935  bytes 91902405 (87.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xfb820000-fb840000

enp2s0f2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 0c:c4:7a:15:0b:7a  txqueuelen 1000  (Ethernet)
        RX packets 16  bytes 5472 (5.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xfb200000-fb300000

enp2s0f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 0c:c4:7a:15:0b:7b  txqueuelen 1000  (Ethernet)
        RX packets 15  bytes 5130 (5.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xfb100000-fb200000


virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:54:3b:79  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 324 (324.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

I am looking for a command line shell which gives me the interfaces without IP address assigned (ie enp2s0f3, enp2s0f2). 我正在寻找一个命令行外壳程序,它可以为我提供未分配IP地址的接口(即enp2s0f3,enp2s0f2)。

$ awk -v RS= -F: '!/inet/{print $1}' file
enp2s0f2
enp2s0f3

A trivial state keeping mechanism allows you to keep track on where you are while reading the input line by line. 一个简单的状态保持机制使您可以在逐行读取输入时跟踪自己的位置。

with open("/tmp/ifs") as i:
    in_if = False
    for line in i.readlines():
        if not line.startswith(" "):
            interface = line.split(":")[0]
            in_if = True
        elif in_if and line.find("inet") > 0:
            in_if = False
        elif in_if and line.find("inet") < 0:
            print(interface)
            in_if = False

Output: 输出:

enp2s0f2
enp2s0f3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM