
내 Linux 서버에서 사용 가능한 모든 네트워크 장치 이름 목록을 얻고 싶습니다. 나는 그렇게 생각했다
ifconfig
작업을 수행하지만 ifconfig가 상당히 많은 출력을 생성합니다.
eth0 Link encap:Ethernet Hardware Adresse 08:00:27:fc:5c:98
inet Adresse:192.168.2.222 Bcast:192.168.2.255 Maske:255.255.255.0
inet6-Adresse: fe80::a00:27ff:fefc:5c98/64 Gültigkeitsbereich:Verbindung
UP BROADCAST RUNNING MULTICAST MTU:1500 Metrik:1
RX packets:329 errors:0 dropped:0 overruns:0 frame:0
TX packets:177 errors:0 dropped:0 overruns:0 carrier:0
Kollisionen:0 Sendewarteschlangenlänge:1000
RX bytes:41496 (40.5 KiB) TX bytes:32503 (31.7 KiB)
eth1 Link encap:Ethernet Hardware Adresse 08:00:27:e9:35:7d
[...]
eth2 Link encap:Ethernet Hardware Adresse 08:00:27:ff:db:fe
[...]
lo Link encap:Lokale Schleife
[...]
내가 달성하고 싶은 것은 다음과 같은 목록입니다.
eth0
eth1
eth2
lo
아니면 그냥 더 나은
eth0
eth1
eth2
나는 이것이 "cat", "sed" 및 "grep"의 조합으로 수행될 수 있다고 가정하지만 불필요한 정보를 제거하는 방법에 대한 단서가 없습니다.
답변1
이것을 시도해 보십시오:
ifconfig -a | sed 's/[ \t].*//;/^$/d'
이는 다음을 생략합니다 lo
:
ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d'
답변2
또 다른 대안은 다음과 같습니다.
ip -o link show | awk -F': ' '{print $2}'
아니면:
ls /sys/class/net
답변3
/sys/class/net을 사용하고 경로를 제거하십시오.
$ basename -a /sys/class/net/*
eth0
eth1
lo
ppp0
tun0
보다 현대적인 방법은 다음과 같이 iproute json 출력과 파서를 사용하는 것입니다.
$ ip -j link |jq -r '.[].ifname'
lo
wlp0s20f3
enp0s31f6
virbr0
virbr0-nic
루프백 인터페이스를 필터링할 수 있습니다.
$ ip -j link |jq -r '.[].ifname | select(. != "lo")'
wlp0s20f3
enp0s31f6
virbr0
virbr0-nic
답변4
ls /sys/class/net/
eth0 eth1 eth2 lo
또는 eth만 필요한 경우*
ls /sys/class/net/eth*
eth0
eth1
eth2