#!/bin/bash # usage: # [[ $(host_reachable host.net) ]] && echo sure || echo nope # or # host_reachable &> /dev/null && echo sure || echo nope # or host_reachable () { # usage: host_reachable < -F configfile > host < port > # reachable returns yes and return 0 # return 1 - not reachable # return 2 no method available to try local host; local config function try( ) ( 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 ) [[ $1 = "-F " ]] && { config=$2;shift 2; } host=$(lookup_host $1 $config) try $host $2 return $? } lookup_host () { # usage: lookup_host hostname < configfile > local config; local host; local lhost config=$([[ $2 ]] && echo $2 || echo ${SSH_CONFIG:-$HOME/.ssh/config}) host=$(get_hostname_host $1) lhost=$($(which ssh) -F $config -G $host | grep -w hostname | cut -d' ' -f2) [[ $lhost ]] && echo $lhost || echo $host } get_hostname_host () { # usage: get_hostname_host # returns extracted host if passed user@host # otherwise return value passed # [[ $1 =~ "@" ]] && { echo $(sed 's/.*@\(.*\)/\1/' <<< "$1");return 0; } local hostname hostname=$(cut -s -d@ -f2 <<< "$1") if [[ ! $hostname ]] && type ssh_config_get &>/dev/null ; then hostname=$(ssh_config_get -h $1) fi [[ $hostname ]] && echo $hostname || return 1 } get_user_host () { # [[ $1 =~ "@" ]] && { echo $(sed 's/\(.*\)@.*/\1/' <<< "$1"); return 0; } || return 1 # usage: get_user_host # returns extracted user if passed user@host # otherwise returns nothing # return 1 no user in string local user user=$(cut -s -d@ -f1 <<< "$1") if [[ ! $user ]] && type ssh_config_get &>/dev/null ; then user=$(ssh_config_get -u $1) fi [[ $user ]] && echo $user || return 1 } valid_ip() { # usage: valid_ip ip # check so see if basic format is correct and all subs or numbers, no returns 2 # then checks if they are 0 to 254, no returns 1 # otherwise if good returns "true" and 0 local ip=$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) [[ $res ]] && { echo $res;return 0; } return 1 fi return 2 } get_domain() { # usage: get_domain fulldomain # returns domain.tld # exampe: get_domain discuss.mydomain.com => returns mydomain.com local domain domain=$(echo $1 | awk -F\. '{print $(NF-1) FS $NF}') echo "$domain" } #https://stackoverflow.com/a/44191743 #https://blog.apnic.net/2021/06/17/how-a-small-free-ip-tool-survived/ publicip () { # if which curl >/dev/null 2>&1; then if curl ipinfo.io/ip 2> /dev/null; then return; fi fi # if which dig>/dev/null 2>&1; then if dig +short myip.opendns.com @resolver1.opendns.com 2> /dev/null; then return; fi fi exec 3<>/dev/tcp/icanhazip.com/80 echo -e 'GET / HTTP/1.0\r\nhost: icanhazip.com\r\n\r' >&3 while read i do [ "$i" ] && myip="$i" done <&3 echo "$myip" } getip () { [[ ! $1 ]] && return 1 if ip=$(getent ahostsv4 $1); then echo $ip | grep STREAM | awk '{ print $1 ; exit }' else return 2 fi }