#!/bin/bash #ip addr|grep ‘^[0-9]‘|awk -F ‘:‘ ‘{print $2}‘ >/tmp/netcard.list #ip addr|grep -w ‘inet‘|awk -F ‘/‘ ‘{print $1}‘|awk ‘{print $2}‘> /tmp/ip.list ip addr|awk -F ‘:‘ ‘$1 ~ "^[0-9]" {print $2}‘ >/tmp/netcard.list get_ip() { ip addr show dev $1|grep ‘inet ‘|awk ‘{print $2}‘|awk -F ‘/‘ ‘{print $1}‘ } ## 获得网卡和对应ip的列表文件 for eth in `cat /tmp/netcard.list` do myip=`get_ip $eth` if [ -z "$myip" ] then echo $eth else echo $eth $myip fi done > /tmp/ip_netcard.list if [ $# -ne 2 ] then echo "Usage: sh $0 [-i interface|-I ip]" exit fi if [ $1 == "-i" ] then if awk ‘{print $1}‘ /tmp/ip_netcard.list |grep -qw $2 then eth=$2 ip1=`awk -v aeth=$eth ‘$1==aeth‘ /tmp/ip_netcard.list |sed "s/$eth //"` echo "网卡$2的ip是 $ip1" else echo "你指定的网卡不存在,系统中的网卡有:`cat /tmp/netcard.list|xargs`" exit fi elif [ $1 == "-I" ] then if grep -qw " $2 " /tmp/ip_netcard.list then eth=`grep -w " $2 " /tmp/ip_netcard.list|awk ‘{print $1}‘` echo "IP $2对应的网卡是$eth" else echo "你指定的ip不对,系统中的IP有:`ip addr|grep -w ‘inet‘|awk ‘{print $2}‘|awk -F ‘/‘ ‘{print $1}‘|xargs`" fi else echo "Usage: sh $0 [-i interface|-I ip]" exit fi |