1.撰寫 ipconfig 程式
#!/bin/bash
iname="eth0 eth1 eth2 eth3"
# interfaces
for i in $iname
do
x=$(ifconfig -a | grep -A1 $i)
if [ ! -z "$x" ]; then
ifn="${x%% *}"
echo "$ifn"
t=${x#*HWaddr }
echo " MAC : ${t%% *}"
t=${x#*inet addr:}
[ "$t" != "$x" ] && echo " IP : ${t%% *}"
r=$(route -n | grep '^0.0.0.0' | grep "$ifn")
if [ ! -z "$r" ]; then
t=${r#0.0.0.0}
t=$(echo $t) # 刪除開頭多個空白字元
echo " GW : ${t%% *}"
fi
echo ""
fi
done
# nameserver
dn=$(cat /etc/resolv.conf | grep 'nameserver')
echo "DNS"
echo " ${dn//nameserver/}"
echo ""
exit 0
2. 設定執行權限
$ chmod +x ipconfig
3. 執行程式
$ ./ipconfig
eth0
MAC : 00:0c:29:35:5a:90
IP : 192.168.111.146
GW : 192.168.111.2
eth1
MAC : 00:0c:29:35:5a:9a
DNS
192.168.111.2
標題因改為ifconfig設定
回覆刪除