命令行shell,用于标识没有IP地址的接口

时间:2015-04-16 14:26:34

标签: bash shell awk

我正在尝试过滤我的机器中没有分配IP地址的接口。我的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

我正在寻找一个命令行shell,它给出了没有分配IP地址的接口(即enp2s0f3,enp2s0f2)。

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

一个简单的状态保持机制允许您在逐行读取输入的同时跟踪您的位置。

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

输出:

enp2s0f2
enp2s0f3