#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)check-port 3.2.1 13.10.2024 (c)2024 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # # This script can be used free of charge. Use it as is or customize as needed. It is not guaranteed to be # error free and you will not be reimbursed for any damage it may cause. # #@ Checks if port can be reached over the internet (ingres) #@ #@Usage: check-port [options] port #@ Options: #@ -h, --help : Displays helptext. #@ -v, --version : Displays the version of the script. #@ -o, --output : Output format: can be "line", "json", "tsv" or "table" (default). #@ Port: Can be a specific port or a port range e.g. '22-25,80,443' e.g. #@ 80: Test if http port can be reached from outside. # # Exit codes: # 01: Unknown or wrong parameter. # 02: **curl** not found. This script needs **curl** to perform. # 03: No internet connection. # 99: User interrupt. # # See also: # **check-ip**(1), **convert-number**(1), **install-scripts**(1) # # ToDo: # Listener check - selinux check - other Firewalls e.g. Mac # Getting a list of portnames e.g. http, ssh from Internet (JSON file?) # Getting a list of Apps that usese special Ports e.g. Oracle DB Port 1521 # # Update history: # # V 3.0.0 02.05.2020 New version # V 3.0.1 03.06.2020 Using transfer instead of curl # V 3.0.2 11.06.2020 Using library # V 3.0.3 21.05.2023 Using HTTPS instead of HTTP # V 3.1.0 05.06.2023 New copyright # V 3.2.0 11.09.2024 New minor version # V 3.2.1 13.10.2024 Don't use convert-json and jq # # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else progdir=`dirname "$0"` if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Set the Base URL url="https://ifconfig.co/port" # Preset param="" maxchecks=25 # Maximal number of port checks formatstr="" # Print result in table format # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$1" in -o | --output) shift if [ "$1" != "" ]; then if [ "$formatstr" = "" ]; then formatstr=`echo "$1" | tolower` if [ "$formatstr" != "tsv" -a "$formatstr" != "line" -a "$formatstr" != "table" -a "$formatstr" != "json" ]; then errstr="Unknown format '$formatstr' after parameter '$pname'. Please choose from 'tsv', 'line', 'table' or 'json'." fi else errstr="Option '$pname' used more then once." fi shift else errstr="Please specify a format ('tsv', 'line', 'table' or 'json') after parameter '$pname'." fi ;; -v | --version) shift showversion=true ;; -h | --help) shift showhelp=true ;; *) shift paramck=`echo "$pname" | grep '^-'` # Keys don't begin with '-' if [ "$paramck" != "" ]; then errstr="Unknown option '$pname'." else if [ "$errstr" = "" ]; then if [ "$param" = "" ]; then param=`echo "$pname" | tolower` else errstr="Port was already specified '$param'. Unknown additional parameter: '$pname'." fi fi fi esac done # Plausibility checks if [ "$errstr" = "" -a "$param" = "" ]; then errstr="No port number given." else if [ "$formatstr" = "" ]; then formatstr="table" fi fi # Display help or error message DisplayHelp # Check if he have curl and jq in path curl=`filecheck -x curl` if [ "$curl" = "" ]; then exitcode=2 errormsg $exitcode "($progstr) No 'curl' in '$PATH'. Please install first e.g. 'sudo yum -y install curl'." exit $exitcode else myip=`get-ip ip` if [ "$myip" = "" ]; then exitcode=3 errormsg $exitcode "($progstr) No connection to the Internet. Can't check ports." exit $exitcode else # Convert port range convert-number "$param" --max 65535 --padding 5 > $scratchfile ports="`head -n $maxchecks $scratchfile | tr '\n' ' '`" printf "%s\t%s\t%s\n" "ip" "port" "reachable" > ${scratchfile}.out for port in $ports; do result="" while [ "$result" = "" ]; do # Get result from server via curl # $curl $curlparam "${url}/$port" -o $scratchfile transfer --quiet "${url}/$port" --export $scratchfile stat=$? if [ $stat -eq 0 ]; then result=`head -n 1 $scratchfile | grep '^{'` if [ "$result" != "" ]; then # ip=`grep '"ip":' $scratchfile | cut -d':' -f2- | tr -d '"' | tr -d ' ' | tr -d ','` port=`grep '"port":' $scratchfile | cut -d':' -f2 | tr -d '"' | tr -d ' ' | tr -d ','` reachable=`grep '"reachable":' $scratchfile | cut -d':' -f2 | tr -d '"' | tr -d ' ' | tr -d ','` printf "%s\t%s\t%s\n" "$myip" "/$port/" "/$reachable/" >> ${scratchfile}.out fi fi done done # ToDo: listener check - selinux check - other Firewalls e.g. Mac # ToDo: Getting a list of portnames e.g. http, ssh from Internet (JSON file?) # ToDo: Getting a list of Apps that usese special Ports e.g. Oracle DB Port 1521 # ToDo: Check from other ip then public ip result=`filecheck -sl ${scratchfile}.out` if [ "$result" = "${scratchfile}.out" ]; then if [ "$formatstr" = "json" ]; then print-table --import ${scratchfile}.out --output $formatstr | sed 's|\("contentItems":.*\)|\1,\n "creator": "'$progstr'"|' else print-table --import ${scratchfile}.out --output $formatstr fi fi fi fi # Cleanup and exit Cleanup exit $exitcode