需要fping
下载:https://mirrors.aliyun.com/epel/7/x86_64/Packages/f/fping-3.10-4.el7.x86_64.rpm
脚本内容:
#!/bin/bash
#****************************************************************#
# ScriptName: UNIP
# Modify Date: 2023-10-31
# Modify Author: Micarlxm
#***************************************************************#
uipsh=$(basename ${BASH_SOURCE[0]})
uipshPath=$(dirname ${BASH_SOURCE})
function usage() {
echo "Usage: sh $uipsh [参数1] [参数2]
参数1:
-g 检测网段IPv4可用性 (必须使用参数2中网段地址格式)
x.x.x.x 检测的IPv4独立地址
参数2:
x.x.x.x/x 检测的IPv4网段 (检测网段时必须使用参数1)
例:
检测OOB网段内的IPv4占用情况
sh unip.sh -g 192.160.0.0/24
检测某个OOB地址是否在使用中
sh unip.sh 192.168.0.251
"
}
uip() {
FUIP=${TMPDIR-/tmp}/uip.$$ # 产生临时性文件名FUIP
FAIP=${TMPDIR-/tmp}/aip.$$ # 产生临时性文件名
trap 'rm -f $FAIP $FAIP ;exit' INT # 完成时删除临时性文件
trap 'rm -f $FUIP $FAIP' EXIT # 完成时删除临时性文件
if [ -x /usr/sbin/fping ]; then
fping -u -i 1 -t 1 -r 1 -B 1 $c $d >$FUIP
fping -a -i 1 -t 1 -r 1 -B 1 $c $d >$FAIP
AIP=$(cat $FAIP | wc -l)
UIP=$(cat $FUIP | wc -l)
echo ""
echo -e "\033[31m网段不可用IP总量为:\033[0m\033[31m$AIP \033[0m"
echo -e "\033[32m网段可用IP总量为:\033[0m\033[32m$UIP \033[0m"
echo -e "\033[32m网段可用IP列表如下(仅显示前30个):\033[0m"
cat $FUIP | head -n 30
exit 0
else
wget https://mirrors.aliyun.com/epel/7/x86_64/Packages/f/fping-3.10-4.el7.x86_64.rpm /tmp/fping-3.10-4.el7.x86_64.rpm && rpm -ivh --nodeps /tmp/fping-3.10-4.el7.x86_64.rpm >/dev/null 2>&1
if [ -x /usr/sbin/fping ]; then
fping -u -i 1 -t 1 -r 1 -B 1 $c $d >$FUIP
fping -a -i 1 -t 1 -r 1 -B 1 $c $d >$FAIP
AIP=$(cat $FAIP | wc -l)
UIP=$(cat $FUIP | wc -l)
echo ""
echo -e "\033[31m网段不可用IP总量为:\033[0m\033[31m$AIP \033[0m"
echo -e "\033[32m网段可用IP总量为:\033[0m\033[32m$UIP \033[0m"
echo -e "\033[32m网段可用IP列表如下(仅显示前30个):\033[0m"
cat $FUIP | head -n 30
exit 0
else
echo "无法检测!"
fi
fi
}
#执行判断部分
if [ -z $1 ]; then
usage
elif [ $1 == "-g" ]; then
if [ -z $2 ]; then
usage
else
IPPOOL=$2
IPv4_ERE=$(echo $IPPOOL | awk -F/ '{print $1}')
stat=1
ipcalc -cs $IPv4_ERE
stat=$?
if [ $stat == 1 ]; then
usage
else
echo $IPPOOL | egrep -q "^${IPv4_ERE}/[2][4-7]$" || result="${result} ${IPPOOL}"
if [ -z $result ]; then
d=$IPPOOL
c=$1
uip
else
usage
fi
fi
fi
elif [ $1 != "-g" ]; then
IPv4_E=$1
stat=1
if [[ $IPv4_E =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
IPv4_E=($IPv4_E)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 &&
${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
if [ $stat == 1 ]; then
usage
else
ipcalc -cs $IPv4_E
if [ $? == 1 ]; then
usage
else
c=$IPv4_E
uip
fi
fi
else
echo -e "**输入参数错误!**\n"
usage
fi
fping 使用
fping [ options ] [ systems... ] fping6 [ options ] [ systems... ]
参数:
NAME fping - send ICMP ECHO_REQUEST packets to network hosts SYNOPSIS fping [ options ] [ systems... ] fping6 [ options ] [ systems... ] DESCRIPTION fping is a program like ping which uses the Internet Control Message Protocol (ICMP) echo request to determine if a target host is responding. fping differs from ping in that you can specify any number of targets on the command line, or specify a file containing the lists of targets to ping. Instead of sending to one target until it times out or replies, fping will send out a ping packet and move on to the next target in a round-robin fashion. In the default mode, if a target replies, it is noted and removed from the list of targets to check; if a target does not respond within a certain time limit and/or retry limit it is designated as unreachable. fping also supports sending a specified number of pings to a target, or looping indefinitely (as in ping ). Unlike ping, fping is meant to be used in scripts, so its output is designed to be easy to parse. The binary named fping6 is the same as fping, except that it uses IPv6 addresses instead of IPv4. OPTIONS -a Show systems that are alive. -A Display targets by address rather than DNS name. Combined with -d, the output will be both the ip and (if available) the hostname. -b n Number of bytes of ping data to send. The minimum size (normally 12) allows room for the data that fping needs to do its work (sequence number, timestamp). The reported received data size includes the IP header (normally 20 bytes) and ICMP header (8 bytes), so the minimum total size is 40 bytes. Default is 56, as in ping. Maximum is the theoretical maximum IP datagram size (64K), though most systems limit this to a smaller, system- dependent number. -B n Backoff factor. In the default mode, fping sends several requests to a target before giving up, waiting longer for a reply on each successive request. This parameter is the value by which the wait time (-t) is multiplied on each successive request; it must be entered as a floating-point number (x.y). The default is 1.5. -c n Number of request packets to send to each target. In this mode, a line is displayed for each received response (this can suppressed with -q or -Q). Also, statistics about responses for each target are displayed when all requests have been sent (or when interrupted). -C n Similar to -c, but the per-target statistics are displayed in a format designed for automated response-time statistics gathering. For example: % fping -C 5 -q somehost somehost : 91.7 37.0 29.2 - 36.8 shows the response time in milliseconds for each of the five requests, with the "-" indicating that no response was received to the fourth request. -d Use DNS to lookup address of return ping packet. This allows you to give fping a list of IP addresses as input and print hostnames in the output. -D Add Unix timestamps in front of output lines generated with in looping or counting modes (-l, -c, or -C). -e Show elapsed (round-trip) time of packets. -f Read list of targets from a file. This option can only be used by the root user. Regular users should pipe in the file via stdin: % fping < targets_file -g addr/mask Generate a target list from a supplied IP netmask, or a starting and ending IP. Specify the netmask or start/end in the targets portion of the command line. If a network with netmask is given, the network and broadcast addresses will be excluded. ex. To ping the network 192.168.1.0/24, the specified command line could look like either: fping -g 192.168.1.0/24 or fping -g 192.168.1.1 192.168.1.254 -h Print usage message. -i n The minimum amount of time (in milliseconds) between sending a ping packet to any target (default is 25, minimum is 1). -l Loop sending packets to each target indefinitely. Can be interrupted with Ctrl-C; statistics about responses for each target are then displayed. -m Send pings to each of a target host's multiple IP addresses (use of option '-A' is recommended). -M Set the "Don't Fragment" bit in the IP header (used to determine/test the MTU). -n Same as -d. -N Format output for netdata (-l -Q are required). See: <http://my-netdata.io/> -o Calculate "outage time" based on the number of lost pings and the interval used (useful for network convergence tests). -O n Set the typ of service flag (TOS). n can be either decimal or hexadecimal (0xh) format. -p <n> In looping or counting modes (-l, -c, or -C), this parameter sets the time in milliseconds that fping waits between successive packets to an individual target. Default is 1000 and minimum is 10. -q Quiet. Don't show per-probe results, but only the final summary. Also don't show ICMP error messages. -Q n Like -q, but show summary results every n seconds. -r n Retry limit (default 3). This is the number of times an attempt at pinging a target will be made, not including the first try. -R Instead of using all-zeros as the packet data, generate random bytes. Use to defeat, e.g., link data compression. -s Print cumulative statistics upon exit. -S addr Set source address. -I if Set the interface (requires SO_BINDTODEVICE support) -t n Initial target timeout in milliseconds (default 500). In the default mode, this is the amount of time that fping waits for a response to its first request. Successive timeouts are multiplied by the backoff factor specified with -B. Note that this option has no effect looping or counting modes (-l, -c, or -C). -T n Ignored (for compatibility with fping 2.4). -u Show targets that are unreachable. -v Print fping version information. -H n Set the IP TTL field (time to live hops). EXAMPLES Generate ~1000 pings per second to a host until canceled, printing statistics on the fly at one second intervals, and printing statistics at the end: # fping -s -l -i 1 -p 1 -T 1 -Q 1 127.0.0.1 Note that ping intervals less than 1ms can only be used as root. AUTHORS o Roland J. Schemers III, Stanford University, concept and versions 1.x o RL "Bob" Morgan, Stanford University, versions 2.x o David Papp, versions 2.3x and up o David Schweikert, versions 3.0 and up fping website: <http://www.fping.org> DIAGNOSTICS Exit status is 0 if all the hosts are reachable, 1 if some hosts were unreachable, 2 if any IP addresses were not found, 3 for invalid command line arguments, and 4 for a system call failure. RESTRICTIONS In order to avoid users mistakingly flooding the network, the following values are not allowed for non-root users: o -i n, where n < 1 msec o -p n, where n < 10 msec ATTRIBUTES See attributes(7) for descriptions of the following attributes: +---------------+------------------+ |ATTRIBUTE TYPE | ATTRIBUTE VALUE | +---------------+------------------+ |Availability | diagnostic/fping | +---------------+------------------+ |Stability | Volatile | +---------------+------------------+ SEE ALSO ping(8) NOTES This software was built from source available at https://github.com/oracle/solaris-userland. The original community source was downloaded from http://www.fping.org/dist/fping-3.16.tar.gz Further information about this software can be found on the open source community website at http://www.fping.org/.