Minor tweaks.

pull/18/head^2
schizza 2024-04-28 17:00:42 +02:00
parent dc263083c4
commit c1a5afb510
2 changed files with 34 additions and 30 deletions

View File

@ -73,7 +73,10 @@ function cont() {
read -n 1 YN read -n 1 YN
YN=${YN:-N} YN=${YN:-N}
case $YN in case $YN in
[Yy]) return 0 ;; [Yy])
echo -e "\n"
return 0
;;
[Nn]) error "\nExiting." ;; [Nn]) error "\nExiting." ;;
*) error "\nInvalid response.\n" false ;; *) error "\nInvalid response.\n" false ;;
esac esac

View File

@ -8,7 +8,6 @@
# #
# Script pro přesměrování portu pro stanici SWS12500 # Script pro přesměrování portu pro stanici SWS12500
STATION_IP=[_STATION_IP_] STATION_IP=[_STATION_IP_]
HA=[_HA_] HA=[_HA_]
SRC_PORT=[_SRC_PORT_] SRC_PORT=[_SRC_PORT_]
@ -22,21 +21,22 @@ GREEN_COLOR='\033[0;32m'
GREEN_YELLOW='\033[1;33m' GREEN_YELLOW='\033[1;33m'
NO_COLOR='\033[0m' NO_COLOR='\033[0m'
function info() { echo -e "${GREEN_COLOR}$1${NO_COLOR}"; }
function info () { echo -e "${GREEN_COLOR}INFO: $1${NO_COLOR}";} function warn() { echo -e "${GREEN_YELLOW}$1${NO_COLOR}"; }
function warn () { echo -e "${GREEN_YELLOW}WARN: $1${NO_COLOR}";} function error() {
function error () { echo -e "${RED_COLOR}ERROR: $1${NO_COLOR}"; if [ "$2" != "false" ]; then exit 1;fi; } echo -e "${RED_COLOR}$1${NO_COLOR}"
if [ "$2" != "false" ]; then exit 1; fi
function check () {
echo -n "Checking dependencies: '$1' ... "
if [ -z "$(command -v "$1")" ]; then
error "not installed" $2
return 1
fi
info "OK."
return 0
} }
function check() {
echo -n "Checking dependencies: '$1' ... "
if [ -z "$(command -v "$1")" ]; then
error "not installed" $2
return 1
fi
info "OK."
return 0
}
echo echo
echo "**************************************************************" echo "**************************************************************"
@ -54,22 +54,21 @@ INSTALL_IPTABLES=$?
check "apk" false check "apk" false
APK_MISSING=$? APK_MISSING=$?
if [ $APK_MISSING -eq 1 ] && [ $INSTALL_IPTABLES -eq 1 ]; then if [ $APK_MISSING -eq 1 ] && [ $INSTALL_IPTABLES -eq 1 ]; then
error "Could not install and run iptables.\n'apk' installer is missing and 'iptables' are not installed.\n" error "Could not install and run iptables.\n'apk' installer is missing and 'iptables' are not installed.\n"
fi fi
if [ $INSTALL_IPTABLES -eq 1 ] && [ $APK_MISSING -eq 0 ]; then if [ $INSTALL_IPTABLES -eq 1 ] && [ $APK_MISSING -eq 0 ]; then
declare -a RUNINSTALL=(apk add iptables) declare -a RUNINSTALL=(apk add iptables)
echo -n "Installing 'iptables' ... ${RUNINSTALL[@]} ... " echo -n "Installing 'iptables' ... ${RUNINSTALL[@]} ... "
${RUNINSTALL[@]} ${RUNINSTALL[@]}
EXIT_STATUS=$? EXIT_STATUS=$?
if [ $EXIT_STATUS -ne 0 ]; then if [ $EXIT_STATUS -ne 0 ]; then
warn "apk error code: $EXIT_STATUS" warn "apk error code: $EXIT_STATUS"
error "Installation of iptables failed!" error "Installation of iptables failed!"
else else
info "'iptables' installed successfully." info "'iptables' installed successfully."
fi fi
fi fi
declare -a RULE=(PREROUTING -t nat -s $STATION_IP -d $HA -p tcp -m tcp --dport $SRC_PORT -j REDIRECT --to-ports $DST_PORT) declare -a RULE=(PREROUTING -t nat -s $STATION_IP -d $HA -p tcp -m tcp --dport $SRC_PORT -j REDIRECT --to-ports $DST_PORT)
echo -n "Chceking for existing rule in iptables ... " echo -n "Chceking for existing rule in iptables ... "
@ -77,13 +76,15 @@ $(iptables -C ${RULE[@]} 2>/dev/null)
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
warn "Rule is already present in PREROUTING chain." warn "Rule is already present in PREROUTING chain."
else else
info "not found."
echo -n "Inserting iptables rule to PREROUTING chain ... " echo -n "Inserting iptables rule to PREROUTING chain ... "
$(iptables -I ${RULE[@]}) $(iptables -I ${RULE[@]})
fi fi
EXIT_STATUS=$? EXIT_STATUS=$?
if [ $EXIT_STATUS -ne 0 ]; then if [ $EXIT_STATUS -ne 0 ]; then
warn "iptables error code: ${EXIT_STATUS} " warn "iptables error code: ${EXIT_STATUS} "
error "Rule could not be added!" error "Rule could not be added!"
fi fi
info "iptables are set to forward port $SRC_PORT -> $DST_PORT for station's IP: $STATION_IP" info "OK."
info "iptables are now set to redirect incomming connections from $STATION_IP:Any -> $HA:$SRC_PORT to $HA:$DST_PORT"