Created
September 1, 2023 09:25
-
-
Save asuna/cfdfd0836d90b8dd62d5dab3398b7714 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 | |
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" | |
while getopts ":i:n:" opt; do | |
case $opt in | |
i) | |
GRE_INTERFACE="$OPTARG" | |
;; | |
n) | |
DOMAIN_NAME="$OPTARG" | |
;; | |
\?) | |
echo "Usage: $0 -i <GRE_INTERFACE> -n <DOMAIN_NAME>" | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." | |
exit 1 | |
;; | |
esac | |
done | |
# Check if required options are provided | |
if [ -z "$GRE_INTERFACE" ] || [ -z "$DOMAIN_NAME" ]; then | |
echo "Usage: $0 -i <GRE_INTERFACE> -n <DOMAIN_NAME>" | |
exit 1 | |
fi | |
# Get the IP address from the current domain name resolution | |
resolved_ip=$(host -t A $DOMAIN_NAME | awk '/has address/ {print $NF}') | |
if [ -z "$resolved_ip" ]; then | |
echo "Unable to resolve the domain name $DOMAIN_NAME" | |
exit 1 | |
fi | |
# Get the current remote IP address of the GRE interface | |
current_gre_ip=$(ip tunnel show $GRE_INTERFACE | awk '/remote/ {split($4, a, "/"); print a[1]}') | |
echo "Resolved IP: $resolved_ip" | |
echo "Current GRE IP: $current_gre_ip" | |
if [ "$resolved_ip" == "$current_gre_ip" ]; then | |
echo "The GRE interface $GRE_INTERFACE is already configured with the IP address from the domain name resolution: $resolved_ip" | |
exit 0 | |
fi | |
# Update the remote IP address of the GRE interface | |
ip tunnel change $GRE_INTERFACE remote $resolved_ip | |
sed -i "s/id-remote\ =\ .*/id-remote\ =\ $resolved_ip/;s/remote_addrs\ =\ .*/remote_addrs\ =\ $resolved_ip/;/remote {/,/}/s/id = [0-9.]\+/id = $resolved_ip/" /etc/stronswan/conf.d/shzj.conf | |
systemctl restart strongswan-swanctl | |
if [ $? -eq 0 ]; then | |
echo "Successfully updated the remote IP address of GRE interface $GRE_INTERFACE to: $resolved_ip" | |
else | |
echo "Failed to update the remote IP address of GRE interface $GRE_INTERFACE" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment