scripts/bridge_starter
author Jens Kasten <jens@kasten-edv.de>
Mon, 19 Mar 2012 04:19:05 +0100
changeset 87 228ce8fbd4ff
parent 0 d05fbfd9a5cf
child 127 8eb957db5ee1
permissions -rw-r--r--
update readme
0
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     1
#!/bin/bash
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     2
# rename a existing network interface
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     3
# create a bridge 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     4
# add the renamed network interface to the bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     5
# optional, set the ip address and default gw back to bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     6
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     7
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     8
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     9
# the network interface which should use
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    10
nic="eth0"
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    11
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    12
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    13
###########################################################
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    14
# from this point nothing has to edit                     #
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    15
# only if your wish to extend or modify the script itself #
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    16
###########################################################
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    17
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    18
# name of the new bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    19
dev_bridge=${nic}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    20
# name of the rename original network interface
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    21
dev_nic="r${nic}"
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    22
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    23
# this is only a simple check for ip address and gateway
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    24
# when network interface has ip, save it
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    25
ip_dev=$(ip addr show ${nic} | awk '/inet/ {print $2}')
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    26
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    27
if [ "${ip_dev}" = "" ]; then 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    28
    use_ip="no"
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    29
else 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    30
    use_ip="yes"
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    31
fi
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    32
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    33
# when network interface has gateway, save it
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    34
# searching for default gateway
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    35
ip_gw=$(ip route | awk '/default/ {print $3}')
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    36
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    37
if [ "${ip_gw}" = "" ]; then
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    38
    use_gw="no"
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    39
else 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    40
    use_gw="yes"
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    41
fi
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    42
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    43
ip addr flush ${nic}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    44
# set down nic 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    45
ip link set ${nic} down
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    46
# rename nic 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    47
ip link set ${nic} name ${dev_nic} 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    48
# bring up renamed nic
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    49
ip link set ${dev_nic} up
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    50
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    51
# create a bridge 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    52
brctl addbr ${dev_bridge}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    53
brctl setfd ${dev_bridge} 0
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    54
brctl sethello ${dev_bridge} 1
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    55
brctl stp ${dev_bridge} off
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    56
# add renamed nic to bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    57
brctl addif ${dev_bridge} ${dev_nic}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    58
# bring up bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    59
ip link set ${dev_bridge} up
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    60
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    61
if [ "${use_ip}" = "yes" ]; then
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    62
    # set ip add to bridge 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    63
    ip addr add ${ip_dev} dev ${dev_bridge}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    64
    if [ "${use_gw}" = "yes" ]; then
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    65
        # add default route
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    66
        ip route add via ${ip_gw}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    67
    fi
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    68
fi
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    69