-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbuild_linux_bridge.sh
executable file
·142 lines (129 loc) · 3.43 KB
/
build_linux_bridge.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/sh
#
# This file is part of asadbg.
# Copyright (c) 2017, Aaron Adams <aaron.adams(at)nccgroup(dot)trust>
# Copyright (c) 2017, Cedric Halbronn <cedric.halbronn(at)nccgroup(dot)trust>
#
# This script is designed to setup an Ubuntu host with a bridged ethernet
# device that can be used to talk to a Cisco firmware running on GNS3/qemu
#
# Prerequisites
# - sudo apt install uml-utilities
#
# Note:
# On Ubuntu 18.04, it looks like br0 is the first interface in the routing table
# so it will always use this instead of eno2?
#
# TODO:
# - Make sure all commands are installed exit
usage()
{
echo "Usage:"
echo "-a Addr range - By default uses br0"
echo "-b Bridge interface - By default uses 192.168.100.1/24"
echo "-i Host ethernet interface - By default uses eno2"
echo "-k Kill the devices we've created"
echo "-g Default gateway - By default uses 192.168.100.1"
echo "-t Tap interface - By default uses tap2"
echo "./build_linux_bridge.sh -i <interface> -t <tap name> -b <bridge name>"
echo ""
echo "NOTE: If cmds fail: apt-get install uml-utilities bridge-utils"
exit
}
if [ "$(whoami)" != "root" ]; then
echo "Run me as root"
exit
fi
IFACE="eno2"
# IMPORTANT: Avoid tap0 if you are using a VPN
TAP="tap2"
BR="br0"
DUMMY_ADDR="192.168.100.254/24"
BRIDGE_ADDR="192.168.100.1/24"
GW="192.168.100.1"
KILL="NO"
while [ $# -gt 0 ]
do
key="$1"
case $key in
-a|--bridge-addr)
BRIDGE_ADDR="$2"
shift # past argument
;;
-b|--bridge)
BR="$2"
shift # past argument
;;
-d|--dummy-addr)
DUMMY_ADDR="$2"
shift # past argument
;;
-g|--gateway)
GW="$2"
shift # past argument
;;
-i|--interface)
IFACE="$2"
shift # past argument
;;
-k|--kill)
KILL="YES"
;;
-t|--tap)
TAP="$2"
shift # past argument
;;
*)
# unknown option
echo "Unknown option"
usage
;;
esac
shift # past argument or value
done
if [ "${KILL}" != "NO" ]; then
# echo "Deleting route"
route del default gw ${GW}
echo "Tearing down ${BR}"
ifconfig ${BR} down
brctl delbr ${BR}
echo "Deleting TAP ${TAP}"
tunctl -d ${TAP}
# ip addr del ${DUMMY_ADDR} brd + dev ${IFACE} label ${IFACE}:0
ip addr del ${DUMMY_ADDR} dev ${IFACE}
ip link delete ${IFACE} type dummy
# We don't unload dummy or tun incase it they were already in use
exit
fi
echo "Loading tun module"
modprobe tun
echo "Loading dummy module"
modprobe dummy
if [ $? != 0 ]; then
echo "Couldn't load the dummy module need for virtual interface"
fi
echo "Creating dummy interface"
ip link add dummy0 type dummy
ip link set name ${IFACE} dev dummy0
echo "Creating tap device ${TAP}"
tunctl -t ${TAP}
ifconfig ${TAP} 0.0.0.0 promisc up
ifconfig ${IFACE} 0.0.0.0 promisc up
echo "Creating bridge device ${BR}"
brctl addbr ${BR}
brctl show ${BR}
ifconfig ${IFACE} up
echo "Assocating ${IFACE} and ${TAP} to ${BR}"
brctl addif ${BR} ${IFACE}
brctl addif ${BR} ${TAP}
echo "Bringing up bridge"
ifconfig ${BR} up
sleep 1
echo "Setting ${BR} addr to ${BRIDGE_ADDR}"
ifconfig ${BR} ${BRIDGE_ADDR}
echo "Setting ${IFACE} addr to ${DUMMY_ADDR}"
ip addr add ${DUMMY_ADDR} dev ${IFACE}
echo "Setting default route"
ip route add 192.168.100.0/24 dev ${TAP}
echo "All done."
echo "You can tear the network down with --kill"