-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathup.sh
More file actions
executable file
·110 lines (95 loc) · 3.03 KB
/
up.sh
File metadata and controls
executable file
·110 lines (95 loc) · 3.03 KB
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
#!/bin/bash
if [ "$(id -u)" != "0" ] ;
then echo -e "\e[31m Please run as root!\033[0m"
exit 126
fi
function check_error {
if [ $? -ne "0" ] ;
then echo -e "\e[31mError!\033[0m"
exit 126
fi
}
function check_preconditions {
# Checking if ovs is installed
which ovs-vsctl > /dev/null 2>&1
if [ $? -ne "0" ] ;
then echo -e "\e[31mOpenvSwitch not installed!\033[0m"
exit 127
fi
# Checking if the ovs bridge is created, if created exit
S=$(ifconfig | grep br-ext > /dev/null 2>&1 )
if [[ $S == *"br-ext"* ]] ;
then echo -e "\e[31mBridge br-ext already created!\033[0m"
exit 127
fi
# Checking if host is connected to a bridged VPN, otherwise exit
S=$(ifconfig | grep tap > /dev/null 2>&1)
if [[ $S == *"tap"* ]] ;
then echo -e "\e[31mMachine not connected to a bridged VPN. \033[0m"
exit 127
fi
}
function configure_ovs {
# Configuration values
NIC="eth0"
SDN_CTRL_IP="10.8.44.55:6633"
PROTO_SDN="tcp"
IP=$(ip addr show $NIC | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
GW=$(ip route | grep default | awk '{print $3}')
MAC=$(ifconfig $NIC | grep "HWaddr\b" | awk '{print $5}')
MASK=$(ip addr show $NIC | grep "inet\b" | awk '{print $2}' | cut -d/ -f2)
echo -ne "Creating an OpenvSwitch bridge to the physical interface...\t\t"
ovs-vsctl add-br br-ext -- set bridge br-ext other-config:hwaddr=$MAC > /dev/null 2>&1
check_error
ovs-vsctl set bridge br-ext protocols=OpenFlow10,OpenFlow12,OpenFlow13
check_error
echo "Done!"
echo -ne "Adding the physical interface to the ovs bridge...\t\t\t"
ovs-vsctl add-port br-ext $NIC > /dev/null 2>&1
check_error
echo "Done!"
echo -ne "Adding the VPN interface to the ovs bridge...\t\t\t\t"
ovs-vsctl add-port br-ext tap0 > /dev/null 2>&1
check_error
echo "Done!"
echo -ne "Removing IP address from the physical interface...\t\t\t"
ifconfig $NIC 0.0.0.0 > /dev/null 2>&1
check_error
echo "Done!"
echo -ne "Giving the ovs bridge the host IP address...\t\t\t\t"
ifconfig br-ext $IP/$MASK > /dev/null 2>&1
check_error
echo "Done!"
echo -ne "Changing the interface MAC address...\t\t\t\t\t"
LAST_MAC_CHAR=${MAC:(-1)}
AUX="${MAC:0:${#MAC}-1}"
if [ "$LAST_MAC_CHAR" -eq "$LAST_MAC_CHAR" ] 2>/dev/null; then
NL="a"
else
NL="1"
fi
NEW_MAC="$AUX$NL"
ifconfig $NIC down
check_error
ifconfig $NIC hw ether $NEW_MAC
check_error
ifconfig $NIC up
check_error
echo "Done!"
echo -ne "Routing traffic through the new bridge...\t\t\t\t"
while $(ip route del default > /dev/null 2>&1); do :; done
ip route add default via $GW dev br-ext
check_error
echo "Done!"
echo -ne "Connecting OVS brige to controller...\t\t\t\t\t"
ovs-vsctl set-controller br-ext $PROTO_SDN:$SDN_CTRL_IP > /dev/null 2>&1
check_error
echo "Done!"
echo -ne "Updating problematic OpenFlow rules if any...\t\t\t\t"
sleep 5
ovs-ofctl mod-flows br-ext "actions:output=1" > /dev/null 2>&1
ovs-ofctl mod-flows br-ext "in_port=1, actions:output=LOCAL" > /dev/null 2>&1
echo "Done!"
}
check_preconditions
configure_ovs