46 lines
1.1 KiB
Bash
46 lines
1.1 KiB
Bash
#!/bin/bash
|
|
|
|
# usage:
|
|
# [[ $(host_reachable host.net) ]] && echo sure || echo nope
|
|
# or
|
|
# host_reachable &> /dev/null && echo sure || echo nope
|
|
# or
|
|
host_reachable () {
|
|
if [[ $(command -v nmap) ]] && [[ $2 ]]; then
|
|
[[ $(nmap $1 -PN -p $2 | grep open) ]] && echo yes && return 0 || return 1
|
|
fi
|
|
if [[ $(command -v nc) ]] && [[ $2 ]]; then
|
|
[[ $(nc -w 2 $1 $2) ]] && echo yes && return 0 || return 1
|
|
fi
|
|
if [[ $(command -v ping) ]]; then
|
|
ping -c1 -W1 $1 &> /dev/null && echo yes && return 0 || return 1
|
|
fi
|
|
return 2 # return 2 to indicate no method was available
|
|
}
|
|
|
|
valid_ip()
|
|
{
|
|
local ip=$1
|
|
local stat=1
|
|
local res
|
|
|
|
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
|
|
OIFS=$IFS
|
|
IFS='.'
|
|
ip=($ip)
|
|
IFS=$OIFS
|
|
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
|
|
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
|
|
res=$([ $? == 0 ] && echo true || echo false)
|
|
else
|
|
res=false
|
|
fi
|
|
echo $res
|
|
}
|
|
|
|
get_domain() {
|
|
local domain
|
|
domain=$(echo $1 | awk -F\. '{print $(NF-1) FS $NF}')
|
|
echo "$domain"
|
|
}
|