-
-
Save davidebettio/62e2470b872814bef58e84b2154ce6d0 to your computer and use it in GitHub Desktop.
IPIP Tunnel Maker
This file contains hidden or 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 | |
# this script creates an IPIP Tunnel | |
# tested for CENTOS 6 & 7 | |
#---------------------------- | |
# CONFIGURATION | |
#---------------------------- | |
# name of tunnel | |
name='tun_test' | |
# local virtual ipv4 address for the tunnel | |
# example: hostA 192.168.5.1, hostB 192.168.5.2 | |
inner='192.168.5.1' | |
# local ipv4 address | |
# example: hostA 192.168.30.10, hostB: 145.30.200.5 | |
outer='192.168.30.10' | |
# partner tunnel ipv4 address | |
# example: hostA 145.30.200.5, hostB: 192.168.30.10 | |
peer='145.30.200.5' | |
#------------------------ | |
# check if Redhat or Centos | |
OSVER=$(awk -F= '/^NAME/{print $2}' /etc/os-release | tr -d '"') | |
[ "${OSVER}" != "CentOS Linux" ] && echo "Host is not Centos or Redhat, exiting" && exit 1 | |
# IP packet forwarding | |
sysctl -w net.ipv4.ip_forward=1 | |
# enable tunnel mods | |
modprobe ip_tunnel | |
modprobe tun | |
ARG=${1:?"No argument was passed: ./ipip.sh up / down"} | |
SYSCFGDIR='/etc/sysconfig/network-scripts' | |
if [[ $ARG == 'up' ]] | |
then | |
# bring up the tunnel | |
ip tunnel add $name mode ipip remote $peer local $outer | |
ip link set $name up | |
ip addr add $inner/30 dev $name | |
# create permanent tunnel | |
echo -e "TYPE=IPIP | |
BOOTPROTO=none | |
DEVICE=$name | |
ONBOOT=yes | |
MY_INNER_IPADDR=$inner | |
MY_OUTER_IPADDR=$outer | |
PEER_OUTER_IPADDR=$peer" > $SYSCFGDIR/ifcfg-$name | |
fi | |
if [[ $ARG == 'down' ]] | |
then | |
ip link set dev $name down | |
ip link delete $name | |
[ -f $SYSCFGDIR/ifcfg-$name ] && rm -f $SYSCFGDIR/ifcfg-$name | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment