简体   繁体   中英

How to find/grep ethernet interface (en) from inet ip in Mac shell script

I want to grep or need result which ethernet interface "en" is getting inet ip. In below ifconfig in terminal result en6 is getting inet 192.188.10.111 so the result should be en6.

example: If en2 or en5 or any en XxX is getting inet IP id should show the ethernet interface enno.

It should not show lo0 loopback inet 127.0.0.1

MY_MAC:~ SKULL$ ifconfig

    lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
        options=3<RXCSUM,TXCSUM>
        inet6 ::1 prefixlen 128 
        inet 127.0.0.1 netmask 0xff000000 
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
        nd6 options=1<PERFORMNUD>
    gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
    stf0: flags=0<> mtu 1280
    en0: flags=8823<UP,BROADCAST,SMART,SIMPLEX,MULTICAST> mtu 1500
        ether 83:c1:9y:z0:g9:99 
        nd6 options=1<PERFORMNUD>
        media: autoselect (<unknown type>)
        status: inactive
    en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
        options=60<TSO4,TSO6>
        ether 99:85:40:69:05:z0 
        media: autoselect <full-duplex>
        status: inactive
    en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
        options=60<TSO4,TSO6>
        ether 99:84:43:67:43:b6 
        media: autoselect <full-duplex>
        status: inactive
    p2p0: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 2304
        ether 09:k5:6m:s6:q1:78 
        media: autoselect
        status: inactive
    awdl0: flags=8902<BROADCAST,PROMISC,SIMPLEX,MULTICAST> mtu 1452
        ether w1:6t:c3:w2:n4:78 
        nd6 options=1<PERFORMNUD>
        media: autoselect
        status: inactive
    bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        options=63<RXCSUM,TXCSUM,TSO4,TSO6>
        ether 23:j5:12:43:43:67 
        Configuration:
            id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
            maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
            root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
            ipfilter disabled flags 0x2
        member: en1 flags=3<LEARNING,DISCOVER>
                ifmaxaddr 0 port 5 priority 0 path cost 0
        member: en2 flags=3<LEARNING,DISCOVER>
                ifmaxaddr 0 port 6 priority 0 path cost 0
        nd6 options=1<PERFORMNUD>
        media: <unknown type>
        status: inactive
    en6: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500

        inet6 fe80::2e0:4cff:fe36:550%en6 prefixlen 64 scopeid 0xb 
        inet 192.188.10.111 netmask 0xffffff00 broadcast 192.168.255.255
        nd6 options=1<PERFORMNUD>
        media: autoselect (100baseTX <full-duplex>)
        status: active

Does this meet your needs?

#!/bin/bash

for interface in `ifconfig | grep -v "\t" | awk -F':' '{print $1}'`; do
    if [ `ipconfig getifaddr $interface` ] ; then
        echo $interface
    fi
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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