写一个getinterface.sh脚本可以接受选项[i,I],完成下面任务:
#!/bin/bash
##Find ip or interface
##Written by Adai 2017-09-06
read -p "Input 'i' to interface;'I' to ip: " c
if [[ $c == i ]]
then
read -p "Please input your interface name: " n
ip=`ifconfig | egrep -A1 "$n" |egrep -v "$n" |awk '{print $2}'`
echo "ip is: $ip"
elif [[ $c == I ]]
then
read -p "Please input your ip:" m
ens=`ifconfig | egrep -B1 "$m" |egrep -v "$m"|awk -F ":" '{print $1}'`
echo "interface is: $ens"
else
echo "Unknown variate.Please check your option!"
fi
铭哥:
#!/bin/bash
ip add |awk -F ":" '$1 ~ /^[1-9]/ {print $2}'|sed 's/ //g' > /tmp/eths.txt
[ -f /tmp/eth_ip.log ] && rm -f /tmp/eth_ip.log
for eth in `cat /tmp/eths.txt`
do
ip=`ip add |grep -A2 ": $eth" |grep inet |awk '{print $2}' |cut -d '/' -f 1`
echo "$eth:$ip" >> /tmp/eth_ip.log
done
useage()
{
echo "Please useage: $0 -i 网卡名字 or $0 -I ip地址"
}
wrong_eth()
{
if ! grep -q "$1" /tmp/eth_ip.log
then
echo "请指定正确的网卡名字"
exit
fi
}
wrong_ip()
{
if ! grep -qw "$1" /tmp/eth_ip.log
then
echo "请指定正确的ip地址"
exit
fi
}
if [ $# -ne 2 ]
then
useage
exit
fi
case $1 in
-i)
wrong_eth $2
grep $2 /tmp/eth_ip.log |awk -F ':' '{print $2}'
;;
-I)
wrong_ip $2
grep $2 /tmp/eth_ip.log |awk -F ':' '{print $1}'
;;
*)
useage
exit
esac
(adsbygoogle = window.adsbygoogle || []).push({});