forked from outime/ipv6-dhclient-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv6-dhclient-script.sh
120 lines (104 loc) · 4.56 KB
/
ipv6-dhclient-script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
# ipv6-dhclient-script - https://github.com/outime/ipv6-dhclient-script/
INTERFACE=$1
BLOCK_ADDR=$2
BLOCK_SUBNET=$3
BLOCK_DUID=$4
DEFAULT_INTERFACE=`ip route get 8.8.8.8 | awk '{print $5; exit}'`
if [[ "$(id -u)" != 0 ]]; then
echo "Sorry, you need to run this as root"
exit 1
fi
if [[ -e /etc/debian_version ]]; then
DISTRO="Debian"
elif [[ -f /etc/redhat-release ]]; then
DISTRO="Redhat"
else
echo "This distribution is not supported"
exit 1
fi
while :
do
clear
if ! [[ -f /proc/net/if_inet6 ]]; then
echo "Seems that IPv6 is not supported by your kernel or the module is not loaded (is it blacklisted?)"
exit 1
fi
echo "WARNING: Network will restart at the end of this script so any existing connections will be dropped!"
while [[ $INTERFACE = "" ]]; do
read -e -p "Interface where IPv6 will be enabled: " -i $DEFAULT_INTERFACE INTERFACE
done
CURRENT_IPV6=$(ip addr show dev $INTERFACE | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d')
if [[ $? -eq 0 ]]; then
echo "You have the following IPv6 addreses configured for $INTERFACE:"
echo "$CURRENT_IPV6"
read -e -p "Continue? [Y/n]: " -i "Y" SKIP
if ! [[ $SKIP =~ ^([yY][eE][sS]|[yY])$ ]]; then
exit 1
fi
fi
while [[ $BLOCK_ADDR = "" ]]; do # to be replaced with regex
read -p "Your IPv6 block address (e.g. 2001:bb8:3e23:200::): " BLOCK_ADDR
done
while ! [[ $BLOCK_SUBNET =~ ^[0-9]+$ ]]; do
read -p "Subnet for your block (e.g. if it's /56, input 56): " BLOCK_SUBNET
done
while [[ $BLOCK_DUID = "" ]]; do # to be replaced with regex
read -p "Associated DUID (e.g. 00:03:00:00:34:b0:0c:47:4a:0e): " BLOCK_DUID
done
echo "Working..."
if [[ $DISTRO = "Debian" ]]; then
INTERFACES_FILE="/etc/network/interfaces"
echo "" >> $INTERFACES_FILE
echo "iface $INTERFACE inet6 static" >> $INTERFACES_FILE
echo "address $BLOCK_ADDR" >> $INTERFACES_FILE
echo "netmask $BLOCK_SUBNET" >> $INTERFACES_FILE
echo "accept_ra 1" >> $INTERFACES_FILE
echo "pre-up dhclient -cf /etc/dhcp/dhclient6.conf -pf /run/dhclient6.$INTERFACE.pid -6 -P $INTERFACE" >> $INTERFACES_FILE
echo "pre-down dhclient -x -pf /run/dhclient6.$INTERFACE.pid" >> $INTERFACES_FILE
elif [[ $DISTRO = "Redhat" ]]; then
INTERFACES_FILE="/etc/systemd/system/ipv6-dhclient.service"
echo "[Unit]" >> $INTERFACES_FILE
echo "Description=$INTERFACE IPv6" >> $INTERFACES_FILE
echo "After=network.target" >> $INTERFACES_FILE
echo "" >> $INTERFACES_FILE
echo "[Service]" >> $INTERFACES_FILE
echo "Type=oneshot" >> $INTERFACES_FILE
echo "RemainAfterExit=yes" >> $INTERFACES_FILE
echo "ExecStart=/usr/sbin/dhclient -cf /etc/dhcp/dhclient6.conf -pf /run/dhclient6.$INTERFACE.pid -6 -P $INTERFACE" >> $INTERFACES_FILE
echo "ExecStart=/usr/sbin/ifconfig $INTERFACE inet6 add $BLOCK_ADDR/$BLOCK_SUBNET" >> $INTERFACES_FILE
echo "" >> $INTERFACES_FILE
echo "ExecStop=/usr/bin/killall dhclient" >> $INTERFACES_FILE
echo "ExecStop=/usr/sbin/ifconfig $INTERFACE inet6 del $BLOCK_ADDR/$BLOCK_SUBNET" >> $INTERFACES_FILE
echo "" >> $INTERFACES_FILE
echo "[Install]" >> $INTERFACES_FILE
echo "WantedBy=multi-user.target" >> $INTERFACES_FILE
fi
DHCLIENT6_FILE="/etc/dhcp/dhclient6.conf"
echo "interface \"$INTERFACE\" {" >> $DHCLIENT6_FILE
echo "send dhcp6.client-id $BLOCK_DUID;" >> $DHCLIENT6_FILE
echo "request;" >> $DHCLIENT6_FILE
echo "}" >> $DHCLIENT6_FILE
if [[ $DISTRO = "Debian" ]]; then
sysctl -w net.ipv6.conf.$INTERFACE.autoconf=0
echo "net.ipv6.conf.$INTERFACE.autoconf=0" >> /etc/sysctl.conf
echo "net.ipv6.conf.$INTERFACE.accept_ra=2" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.forwarding=1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding=1" >> /etc/sysctl.conf
echo "net.ipv6.conf.default.proxy_ndp=1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.proxy_ndp=1" >> /etc/sysctl.conf
ifdown $INTERFACE && ifup $INTERFACE
elif [[ $DISTRO = "Redhat" ]]; then
systemctl enable ipv6-dhclient
systemctl restart ipv6-dhclient
fi
echo "Testing IPv6 connectivity..."
IPV6_TEST=$(ping6 -c 8 ipv6.google.com | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [[ $IPV6_TEST > 0 ]]; then
echo "Success!"
exit 0
else
echo "Something went wrong :("
exit 1
fi
done