BASH第七课第一题

来源:互联网 发布:云计算与电子政务 编辑:程序博客网 时间:2024/06/11 04:16
1、写一个脚本getinterface.sh,脚本可以接受参数(i,I,a),完成以下任务:
   (1)使用以下形式:getinterface.sh [-i interface|-I IP|-a]
   (2)当用户使用-i选项时,显示其指定网卡的IP地址;
   (3)当用户使用-I选项时,显示其后面的IP地址所属的网络接口;(如 192.168.199.183:eth0)

   (4)当用户单独使用-a选项时,显示所有网络接口及其IP地址(lo除外)

#!/bin/bashexec 2>>/dev/nullcommand=$1list=`netstat -i | sed -n '3,65535p'|awk -F" " '{print $1}'`[ -z $command ]&&command="-h"function helptext(){echo "getinterface.sh[-i interface|-I IP|-a|-h]"echo "-----------------------------------------"        echo "-i interface (show ip of the interface)"        echo "-I IP (show interface of the IP)"        echo "-a list all interfaces and their IPs"echo "-h show the help text"echo "-----------------------------------------"}if [ $command == "-h" ];thenhelptextelif [ $command == "-i" ];theninterface=$2ifconfig $interface >/dev/nullflag=$?if [ -z $interface ]; thenecho "please input the interface"exit 2elif [ $flag -ne 1 ];thenip=`ifconfig $interface | grep "inet addr" | awk -F" " '{print $2}' | awk -F":" '{print $2}'`echo "$interface $ip"elseecho "$interface is not exist"fielif [ $command == "-I" ];thenIP=$2if [ -z $IP ]; then                echo "please input the IP"                exit 2elsefor inter in $list;doip_temp=`ifconfig $inter | grep "inet addr" | awk -F" " '{print $2}' | awk -F":" '{print $2}'`if [ $IP == $ip_temp ];thenecho "$IP : $inter"exit 0fidonefiecho "$IP is not exist in the interfaces of  the device"elif [ $command == "-a" ];thenfor inter in $list;        do                ip_temp=`ifconfig $inter | grep "inet addr" | awk -F" " '{print $2}' | awk -F":" '{print $2}'`if [ $inter != "lo" ];thenecho "$inter:$ip_temp"fi        doneelseecho "the format error!!!"helptextfi


0 0
原创粉丝点击