Skip to main content
mistakes minor
Source Link
Mike Q
  • 159
  • 5

Couple of solutions in BASH4+

# -- server available to check via port xxx ?  --
function isServerAvailableNC() {
    false
    max_secs_run="${3}"
    if timeout $max_secs_run nc -z ${1} ${2} 2>/dev/null >/dev/null; then
        #echo "${1} ✓"
        true
    fielse
        #echo "${1} ✗"
        return
   fi
}


# -- server available to check via port xxx ?  --
# -- supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE) --
#/usr/bin/curl -sSf --max-time 3 https://ifwewanted.to.confirm.https.com/ --insecure

function isServerAvailableCURL() {
    false

    max_secs_run="${3}"

    proto="http://"
    if [ ! -z ${2} ] || [ ${2} -gt 80 ] ;then
        proto="https://"
    else
        proto="http://"
    fi

    if /usr/bin/curl -sSf --max-time "${max_secs_run}" "${1}" --insecure 2>/dev/null >/dev/null; then
        #echo "${1} ✓"
        true
    fielse
        #echo "${1} ✗"
    return    false
    fi
}

Sample use:

RECOMMEND THAT NC used if we need a specific port

host="1.2.3.4"
if isServerAvailableCURL "$host" "80" "3";then
    check_remote_domain_cert "$host"
fi


host="1.2.3.4"
if isServerAvailableNC "$host" "80" "3";then
    check_remote_domain_cert "$host"
fi

Couple of solutions in BASH4+

# -- server available to check via port xxx ?  --
function isServerAvailableNC() {
    false
    max_secs_run="${3}"
    if timeout $max_secs_run nc -z ${1} ${2} 2>/dev/null >/dev/null; then
        #echo "${1} ✓"
        true
    fi
    #echo "${1} ✗"
    return
}


# -- server available to check via port xxx ?  --
# -- supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE) --
#/usr/bin/curl -sSf --max-time 3 https://ifwewanted.to.confirm.https.com/ --insecure

function isServerAvailableCURL() {
    false

    max_secs_run="${3}"
    if [ ! -z ${2} ] || [ ${2} -gt 80 ] ;then
        proto="https://"
    else
        proto="http://"
    fi

    if /usr/bin/curl -sSf --max-time "${max_secs_run}" "${1}" --insecure 2>/dev/null >/dev/null; then
        #echo "${1} ✓"
        true
    fi
    #echo "${1} ✗"
    return
}

Sample use:

RECOMMEND THAT NC used if we need a specific port

host="1.2.3.4"
if isServerAvailableCURL "$host" "80" "3";then
    check_remote_domain_cert "$host"
fi


host="1.2.3.4"
if isServerAvailableNC "$host" "80" "3";then
    check_remote_domain_cert "$host"
fi

Couple of solutions in BASH4+

# -- server available to check via port xxx ?  --
function isServerAvailableNC() {
    max_secs_run="${3}"
    if timeout $max_secs_run nc -z ${1} ${2} 2>/dev/null >/dev/null; then
        #echo "${1} ✓"
        true
   else
        #echo "${1} ✗"
        return
   fi
}


# -- server available to check via port xxx ?  --
# -- supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE) --
#/usr/bin/curl -sSf --max-time 3 https://ifwewanted.to.confirm.https.com/ --insecure

function isServerAvailableCURL() {
   
    max_secs_run="${3}"

    proto="http://"
    if [ ! -z ${2} ] || [ ${2} -gt 80 ] ;then
        proto="https://"
    fi

    if /usr/bin/curl -sSf --max-time "${max_secs_run}" "${1}" --insecure 2>/dev/null >/dev/null; then
        #echo "${1} ✓"
        true
    else
        #echo "${1} ✗"
        false
    fi
}

Sample use:

RECOMMEND THAT NC used if we need a specific port

host="1.2.3.4"
if isServerAvailableCURL "$host" "80" "3";then
    check_remote_domain_cert "$host"
fi


host="1.2.3.4"
if isServerAvailableNC "$host" "80" "3";then
    check_remote_domain_cert "$host"
fi
Source Link
Mike Q
  • 159
  • 5

Couple of solutions in BASH4+

# -- server available to check via port xxx ?  --
function isServerAvailableNC() {
    false
    max_secs_run="${3}"
    if timeout $max_secs_run nc -z ${1} ${2} 2>/dev/null >/dev/null; then
        #echo "${1} ✓"
        true
    fi
    #echo "${1} ✗"
    return
}


# -- server available to check via port xxx ?  --
# -- supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE) --
#/usr/bin/curl -sSf --max-time 3 https://ifwewanted.to.confirm.https.com/ --insecure

function isServerAvailableCURL() {
    false

    max_secs_run="${3}"
    if [ ! -z ${2} ] || [ ${2} -gt 80 ] ;then
        proto="https://"
    else
        proto="http://"
    fi

    if /usr/bin/curl -sSf --max-time "${max_secs_run}" "${1}" --insecure 2>/dev/null >/dev/null; then
        #echo "${1} ✓"
        true
    fi
    #echo "${1} ✗"
    return
}

Sample use:

RECOMMEND THAT NC used if we need a specific port

host="1.2.3.4"
if isServerAvailableCURL "$host" "80" "3";then
    check_remote_domain_cert "$host"
fi


host="1.2.3.4"
if isServerAvailableNC "$host" "80" "3";then
    check_remote_domain_cert "$host"
fi