scripts/bridge_stoper
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
# bring down nic and bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     3
# rename a existing network interface
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
     4
# destroy 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 nic 
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 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 ${dev_bridge}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    44
# set down bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    45
ip link set ${dev_bridge} down
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    46
# set down nic
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    47
ip link set ${dev_nic} down
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    48
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    49
# remove nic from bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    50
brctl delif ${dev_bridge} ${dev_nic}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    51
# destroy bridge
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    52
brctl delbr ${dev_bridge}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    53
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    54
# rename nic 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    55
ip link set ${dev_nic} name ${nic} 
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    56
# bring up renamed nic
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    57
ip link set ${nic} up
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    58
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    59
if [ "${use_ip}" = "yes" ]; then
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    60
    # add ip to nic
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    61
    ip addr add ${ip_dev} dev ${nic}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    62
    if [ "${use_gw}" = "yes" ]; then
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    63
        # delte and add a default route
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    64
        ip route add via ${ip_gw}
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    65
    fi
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    66
fi
d05fbfd9a5cf build repository new
Jens Kasten <jens@kasten-edv.de>
parents:
diff changeset
    67