Bash_Scripts/CentOS/Port-Forward.bash

99 lines
2.4 KiB
Bash
Raw Normal View History

2021-07-18 02:56:20 -05:00
#!/bin/bash --posix
set -e
IP=""
INBOUND_PORT=""
FORWARD_PORT=""
usage() {
printf "%s" "Usage:
--ip
The ip address to forward data to
Example:
--ip 192.168.10.11
--inbound-port
The port that data will be received on
Example:
--inbound-port 22
--forward-port
The port that data will be forwarded to
Example:
--forward-port 2020
All above flags must be passed with arguments"
}
error() {
2021-07-18 12:57:56 -05:00
printf '%s\n' "$1" >&2
exit 1
2021-07-18 02:56:20 -05:00
}
while :; do
2021-07-18 12:57:56 -05:00
case $1 in
-h | -\? | --help)
usage # Display a usage synopsis.
exit
;;
--) # End of all options.
shift
break
;;
--ip)
shift
[[ "${1}" == "" ]] && error "--ip requires an argument"
IP=${1}
;;
--inbound-port)
2021-07-18 02:56:20 -05:00
shift
2021-07-18 12:57:56 -05:00
[[ "${1}" == "" ]] && error "--inbound-port requires an argument"
INBOUND_PORT=${1}
;;
--forward-port)
shift
[[ "${1}" == "" ]] && error "--forward-port requires an argument"
FORWARD_PORT=${1}
;;
-?*)
printf 'Unknown option: %s\n' "$1" >&2
usage
;;
*) # Default case: No more options, so break out of the loop.
break ;;
esac
shift
2021-07-18 02:56:20 -05:00
done
2021-07-18 12:57:56 -05:00
[[ "${IP}" == "" ]] && error "--ip requires an argument"
[[ "${FORWARD_PORT}" == "" ]] && error "--forward-port requires an argument"
[[ "${INBOUND_PORT}" == "" ]] && error "--inbound-port requires an argument"
2021-07-18 02:56:20 -05:00
firewall-cmd --permanent --add-forward-port=port="${INBOUND_PORT}":proto=tcp:toaddr="${IP}":toport="${FORWARD_PORT}"
firewall-cmd --permanent --add-port="${INBOUND_PORT}"/tcp
2021-07-28 09:32:08 -05:00
firewall-cmd --add-masquerade --permanent
2021-07-18 12:57:56 -05:00
firewall-cmd --reload
2021-07-18 02:56:20 -05:00
2021-08-10 04:18:08 -05:00
echo "Routing all data on ${INBOUND_PORT} to ${IP}:${FORWARD_PORT}"
cat << 'EOF' > ~/.ssh/Kansas-5800X-VPS-Website.key
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACBINvEv0E+Vyy8ockkVG6Vf/n5ru3pf1AbIDrbcVBv+cwAAAKgnhs+yJ4bP
sgAAAAtzc2gtZWQyNTUxOQAAACBINvEv0E+Vyy8ockkVG6Vf/n5ru3pf1AbIDrbcVBv+cw
AAAEArFGoaGcd1L5QrtFpokqb1vPqYuZUR1Y58QWlMl6On+Eg28S/QT5XLLyhySRUbpV/+
fmu7el/UBsgOttxUG/5zAAAAInByaWNlaGlsbGVyQFByaWNlcy1NQlAuc2F0eC5yci5jb2
0BAgM=
-----END OPENSSH PRIVATE KEY-----
EOF
chmod 600 ~/.ssh/Kansas-5800X-VPS-Website.key
cat << 'EOF' >> ~/.ssh/config
Host Kansas-5800X-VPS-Website
User root
HostName 208.110.85.242
IdentityFile ~/.ssh/Kansas-5800X-VPS-Website.key
Port 2020
LogLevel INFO
EOF