homelab/wizard/scripts/update_hairpin_nat.sh

27 lines
793 B
Bash
Raw Normal View History

2022-08-07 14:20:15 -07:00
#!/bin/vbash
source /opt/vyatta/etc/functions/script-template
# create copies of config for modification and backup
cp /config/config.boot /config/config.new
cp /config/config.boot /config/backups/config.bk-$(date +%Y-%m-%d)
2023-10-16 22:37:13 -07:00
# switch mode to config
configure
2022-08-07 14:20:15 -07:00
# get current public IP
NEW_IP=$(run show interfaces pppoe pppoe1 | grep "inet\s" | cut -d ' ' -f 6)
# get old public IP
2023-10-16 22:37:13 -07:00
OLD_IP=$(run show nat destination rules | grep 1100 | head -1 | tr -s ' ' | cut -d' ' -f 3)
2022-08-07 14:20:15 -07:00
# edit the config.new file
sed -i "s/$OLD_IP/$NEW_IP/" /config/config.new
# apply the new config
load /config/config.new
{ # try commit, save, exit
commit && save && exit
} || { # catch, exit discard and create a very basic error file
exit discard
echo "Script failed. Write some real error handling."
}