Wie bekomme ich nur IP und welche Schnittstelle ist IP? Damit ich eine Aufzeichnungsdatei wie realtime.ini behalten kann
1 - test.sh
#!/bin/bash
ipstring ="inet (.*)"
for i in $(ip addr);
do
echo $i #on found it write down to my realtime.ini as a list for future query
done
2 - realtime.ini
em1,192.168.1.2
lo,127.0.0.1
wlan0,<not found>
Nachverfolgen: Nur für einzelne IP: $ ifconfig eth0 | grep "inet addr:" | cut -d: -f2 | awk "{print $ 1}" 192.168.1.2
Antworten:
2 für die Antwort № 1Dies ist nicht besonders elegant, auch nicht bash, aber Sie können Folgendes tun, wenn Sie sowohl awk als auch sed haben:
ifconfig | awk "BEGIN { FS = "n"; RS = "" } { print $1 $2 }" | sed -e "s/ .*inet addr:/,/" -e "s/ .*//"
Ich würde nicht darauf wetten, dass dies enorm tragbar ist. Vielleicht hat jemand eine bessere Antwort.
2 für die Antwort № 2
wenn Sie installieren moreutils
Paket können Sie praktisch verwenden ifdata
Befehl:
for INTF_PATH in /sys/class/net/* # list all interfaces
do
INTF=$(basename $INTF_PATH) # interface name
echo "$INTF,$(ifdata -pa $INTF)" # interface name and address
done
Beispielausgabe für nur 5 Schnittstellen eth0
und lo
sind auf:
eth0,123.234.10.12
lo,127.0.0.1
vboxnet0,NON-IP
wlan0,NON-IP
wlan1,NON-IP