ifconfig -a が必要なデータ grep を取得します。

ifconfig -a が必要なデータ grep を取得します。

ネットワークインタフェースデータを取得するために次のコマンドを実行する場合:

[root@pi lib]# ifconfig -a

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.154  netmask 255.255.255.0  broadcast 192.168.0.255
        ether b8:27:eb:3c:03:fe  txqueuelen 1000  (Ethernet)
        RX packets 44219  bytes 17569207 (16.7 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9964  bytes 7176485 (6.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 0  (Local Loopback)
        RX packets 229  bytes 21989 (21.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 229  bytes 21989 (21.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 00:11:6b:f0:bb:31  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        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

[root@pi lib]#

端末からネットワークインターフェイス名、IP、サブネットマスク、ゲートウェイ、MACアドレスを取得するには?

ベストアンサー1

を使用する方が良いかもしれませんが、ip a現在の出力に応じて以下を使用できますawk

awk '
  BEGIN { RS="\n\n"} 
  /eth0/ && /UP/ {ifc=$1; ip=$6; subn=$8; gway=$10; mac=$12} 
  END {print "Interface: "ifc "\nIP: "ip "\nSubnet: "subn "\nGateway: "gway "\nMac: "mac}
' <(ifconfig -a)

Interface: eth0:
IP: 192.168.0.154
Subnet: 255.255.255.0
Gateway: 192.168.0.255
Mac: b8:27:eb:3c:03:fe

インターフェイスの起動時にのみ詳細が印刷されます。

おすすめ記事