Created
April 18, 2023 17:08
-
-
Save valtoni/f196ffa27ea126e5a8416062c1a60df2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $(dpkg-query -W -f='${Status}' socat 2>/dev/null | grep -c "ok installed") -eq 0 ]; then | |
apt-get install socat; | |
else | |
echo "*** Package socat already installed" | |
fi | |
MAPPINGS=("1231,10.0.12.1,123" "1232,10.0.12.2,123") | |
for MAPPING in ${MAPPINGS[@]}; do | |
IFS="," read -r PORT_SRC IP PORT_DST <<< $MAPPING | |
echo "*** Create mapping >>> SRC: ${PORT_SRC}, IP: ${IP}, DST: ${PORT_DST}" | |
SERVICE_NAME="${IP}-${PORT_DST}.service" | |
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}" | |
cat <<EOT > ${SERVICE_FILE} | |
[Unit] | |
Description=Forward port ${PORT_SRC} to ${IP}:${PORT_DST} | |
[Service] | |
User=root | |
WorkingDirectory=/opt | |
ExecStart=socat TCP-LISTEN:${PORT_SRC},fork TCP:${IP}:${PORT_DST} | |
Restart=always | |
[Install] | |
WantedBy=multi-user.target | |
EOT | |
sudo systemctl daemon-reload | |
sudo systemctl enable ${SERVICE_NAME} | |
sudo systemctl start ${SERVICE_NAME} | |
echo "*** Service ${SERVICE_NAME} created" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment