In EAVCapture project,I need to display wired and wireless network information in Qt GUI,but the Qt's Network Library is not good to get these data,so I wrote a simple script to archive this.The code was attached following.
I use QProgress to execute external scripts,and get its output.
I hope this script can help others.
Enjoy,guys!
#!/bin/bash
#this script is used to get network interface information,
#contains wired and wireless.
#at default,
#the wired name is eth0,
#the wireless name is wlan0.
#if the names are not adapted,please change them.
#May 19,2015 by zhangshaoyan.(13522296239)
#define the wired and wireless network interface name.
WIREDNAME=enp8s0
WIRELESSNAME=wlp5s0
#define the used utilties absolutely path.
#it is very important here!!!
IFCONFIG=/sbin/ifconfig
AWK=/usr/bin/awk
#the call format is
#./ZNetworkHelp.sh wired ip/mac/mask
#./ZNetworkHelp.sh wireless ip/mac/mask
if [ $# -lt 2 ];then
echo "Missing Parameter"
exit -1
fi
#get the network interface name.
NetworkName=${WIREDNAME}
if [ $1 = "wired" ];then
NetworkName=${WIREDNAME}
elif [ $1 = "wireless" ];then
NetworkName=${WIRELESSNAME}
else
echo "Invalid NICName"
exit -1
fi
#get ip/mac/mask.
if [ $2 = "ip" ];then
${IFCONFIG} ${NetworkName} | ${AWK} '/inet addr:/{print $2}' | ${AWK} -F: '{print $2}'
elif [ $2 = "mask" ];then
${IFCONFIG} ${NetworkName} | ${AWK} -F : '/Mask/{print $4}'
elif [ $2 = "mac" ];then
${IFCONFIG} ${NetworkName} | ${AWK} '/HWaddr/{print $5}'
else
echo "Invalid Command"
exit -1
fi