指定的网卡名,自动发现局域网内在用的同网段ip

    技术2025-05-02  14

    #!/bin/bash a=$(date +%H:%M:%S) ## 开始执行时间 NetC=eth0 ## 默认网卡eth0 if [ $# -ne 1 ];then echo "Usage: please try again like : $0 {Network Card Name} " exit else NetC=$1 fi FileName=${NetC}_list echo "待写入文件名:$FileName" #rm -f $FileName ## 参数有效性检查,并获取IP ifconfig $NetC > /dev/null if [ $? -ne 0 ];then echo -e "Network Card Name is not found by ifconfig ";exit else NetC_IP=`ifconfig $NetC |grep broadcast |head -n 1 | awk '{print $2}'` fi echo "需要寻找的是$NetC:$NetC_IP的同网段ip" ## ip生成 Lan=`echo $NetC_IP | awk -F "." '{print $1"."$2"."$3"."}'` ## 核心处理逻辑 for i in ${Lan}{1..254} do { ping -c 2 $i |grep -q "ttl" if [ $? -eq 0 ];then echo $i >> $FileName fi } & done wait b=$(date +%H:%M:%S) ## 结束执行时间 echo "success! $NetC的同网段ip已写入 $FileName " echo -e "startTime:\t$a" echo -e "endTime:\t$b" echo "如果重复执行,生成的文件中会包含多次执行的结果,可以用 uniq |sort 命令去重排序。"

     

    Processed: 0.015, SQL: 9