upload script
This commit is contained in:
commit
a78bc9450b
1 changed files with 128 additions and 0 deletions
128
autogre.sh
Executable file
128
autogre.sh
Executable file
|
@ -0,0 +1,128 @@
|
|||
#!/bin/bash
|
||||
|
||||
# This script is used to automatically generate the GRE tunnel between two hosts based on FQDN
|
||||
|
||||
# CONSTANTS
|
||||
# If not defined in the environment, the following variables will be used
|
||||
|
||||
if [[ -z $GRE_TUNNEL_NAME ]]; then
|
||||
GRE_TUNNEL_NAME="gre1"
|
||||
GRE_TUNNEL_NAME="gre1"
|
||||
GRE_LOCAL_IP="45.13.XX.XX"
|
||||
GRE_LAN_IP_4="192.168.100.1/24"
|
||||
GRE_LAN_IP_6="2a0e:fd45:2a0c::1/64"
|
||||
fi
|
||||
|
||||
FQDN=$1 # The remote host FQDN
|
||||
|
||||
# FUNCTIONS
|
||||
function usage() {
|
||||
echo "Usage: $0 <remote_host_fqdn>"
|
||||
exit 1
|
||||
}
|
||||
|
||||
function check_root() {
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo "This script must be run as root"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function check_fqdn() {
|
||||
if [[ -z $FQDN ]]; then
|
||||
echo "Please provide the remote host FQDN"
|
||||
usage
|
||||
fi
|
||||
}
|
||||
|
||||
function get_current_ip() {
|
||||
# Get the dst IP on the GRE tunnel
|
||||
CURRENT_IP=$(ip addr show $GRE_TUNNEL_NAME | grep -Po 'peer \K[\d.]+')
|
||||
if [[ -z $CURRENT_IP ]]; then
|
||||
echo "GRE tunnel is down or not configured"
|
||||
# Create the GRE tunnel
|
||||
CURRENT_IP=""
|
||||
fi
|
||||
}
|
||||
|
||||
function get_remote_ip() {
|
||||
# Get the remote IP on the GRE tunnel
|
||||
REMOTE_IP=$(dig +short $FQDN | tail -n1)
|
||||
if [[ -z $REMOTE_IP ]]; then
|
||||
echo "Failed to resolve the remote host FQDN ($FQDN)"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
function check_gre_tunnel() {
|
||||
# Check if the GRE tunnel is up
|
||||
if [[ -z $CURRENT_IP ]]; then
|
||||
echo "GRE tunnel is down or not configured"
|
||||
# Return 1 to indicate that the GRE tunnel is down
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
function configure_gre_tunnel() {
|
||||
# Configure the GRE tunnel
|
||||
# Delete the GRE tunnel if it exists
|
||||
echo "LOG - $(date) - REMOTE IP: $REMOTE_IP"
|
||||
echo "ip link del $GRE_TUNNEL_NAME"
|
||||
ip link del $GRE_TUNNEL_NAME
|
||||
# Create the GRE tunnel
|
||||
echo "ip link add $GRE_TUNNEL_NAME type gre remote $REMOTE_IP local $GRE_LOCAL_IP ttl 255"
|
||||
ip link add $GRE_TUNNEL_NAME type gre remote $REMOTE_IP local $GRE_LOCAL_IP ttl 255
|
||||
echo "ip link set $GRE_TUNNEL_NAME up"
|
||||
ip link set $GRE_TUNNEL_NAME up
|
||||
echo "ip addr add $GRE_LAN_IP_4 dev $GRE_TUNNEL_NAME"
|
||||
ip addr add $GRE_LAN_IP_4 dev $GRE_TUNNEL_NAME
|
||||
echo "ip addr add $GRE_LAN_IP_6 dev $GRE_TUNNEL_NAME"
|
||||
ip addr add $GRE_LAN_IP_6 dev $GRE_TUNNEL_NAME
|
||||
}
|
||||
|
||||
function check_remote_ip() {
|
||||
# Check if the remote IP is the same as the one configured on the GRE tunnel
|
||||
if [[ $REMOTE_IP == $CURRENT_IP ]]; then
|
||||
echo "GRE tunnel is already configured with the right remote IP"
|
||||
# Return 0 to indicate that the GRE tunnel is already configured
|
||||
exit 0
|
||||
else
|
||||
echo "GRE tunnel is configured with the wrong remote IP"
|
||||
configure_gre_tunnel
|
||||
echo "Updated GRE tunnel with the right remote IP"
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
|
||||
# MAIN
|
||||
function main() {
|
||||
check_root
|
||||
check_fqdn
|
||||
|
||||
echo "LOG - $(date) - Starting autogre.sh"
|
||||
echo "LOG - $(date) - FQDN: $FQDN"
|
||||
echo "LOG - $(date) - GRE_TUNNEL_NAME: $GRE_TUNNEL_NAME"
|
||||
echo "LOG - $(date) - GRE_LOCAL_IP: $GRE_LOCAL_IP"
|
||||
echo "LOG - $(date) - GRE_LAN_IP_4: $GRE_LAN_IP_4"
|
||||
echo "LOG - $(date) - GRE_LAN_IP_6: $GRE_LAN_IP_6"
|
||||
|
||||
echo "LOG - $(date) - Checking if the GRE tunnel is configured"
|
||||
get_current_ip
|
||||
|
||||
echo "LOG - $(date) - Checking if the remote IP is the same as the one configured on the GRE tunnel"
|
||||
get_remote_ip
|
||||
|
||||
echo "LOG - $(date) - Checking if the GRE tunnel is up"
|
||||
if check_gre_tunnel ; then
|
||||
echo "LOG - $(date) - GRE tunnel is up"
|
||||
check_remote_ip
|
||||
echo "LOG - $(date) - GRE tunnel is configured with the right remote IP"
|
||||
else
|
||||
echo "LOG - $(date) - GRE tunnel is down"
|
||||
configure_gre_tunnel
|
||||
echo "LOG - $(date) - Updated GRE tunnel with the right remote IP"
|
||||
fi
|
||||
}
|
||||
|
||||
main
|
Loading…
Reference in a new issue