Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Last active February 28, 2023 07:28
Show Gist options
  • Save perfecto25/19fe6b3a07b0381b647d2a7bc66a6e22 to your computer and use it in GitHub Desktop.
Save perfecto25/19fe6b3a07b0381b647d2a7bc66a6e22 to your computer and use it in GitHub Desktop.
IPIP Tunnel Maker
#!/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