grml-sniff: make sure network devices configured as BRIDGE_DEVICES exist
[grml-network.git] / sbin / grml-sniff
1 #!/bin/sh
2 # Filename:      grml-sniff
3 # Purpose:       script for configuring a network sniffing setup
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 CONFIG_FILE=/etc/grml/routersetup
10 . /etc/grml/lsb-functions
11 . /etc/grml/script-functions
12
13 usage_info()
14 {
15    einfo "$0 - script for configuring a network sniffing setup"
16    einfo "Configure via $CONFIG_FILE - see man 8 grml-sniff" ; eend 0
17 }
18
19 if ! [ -r "$CONFIG_FILE" ] ; then
20   eerror "$CONFIG_FILE could not be read."
21   exit 1
22 fi
23
24 . "$CONFIG_FILE"
25
26 if [ -z "$BRIDGE_DEVICES" ] ; then
27    eerror "Bridge devices (\$BRIDGE_DEVICES) not set in $CONFIG_FILE"
28    exit 1
29 fi
30
31 [ -n "$BRCTL" ]          || BRCTL='brctl'
32 [ -n "$BRIDGE_NAME" ]    || BRIDGE_NAME='br0'
33 [ -n "$BRIDGE_DEVICES" ] || BRIDGE_DEVICES='eth0 eth1'
34
35 check_devs() {
36   for dev in $BRIDGE_DEVICES ; do
37     ip link show dev "$dev" >/dev/null 2>&1 || return 1
38  done
39 }
40
41 if ! check_devs ; then
42   eerror "BRIDGE_DEVICES $BRIDGE_DEVICES do not seem to exist." >&2
43   exit 1
44 fi
45
46 check4progs $BRCTL || exit 1
47
48 case "$1" in
49     start)
50         check4root || exit 1
51         einfo "Starting sniffing setup"
52         eindent
53             einfo "Creating bridge device"
54             brctl addbr "$BRIDGE_NAME"
55             eend $?
56
57             einfo "Bringing network device in promiscuous mode up:"
58             eindent
59                for i in $BRIDGE_DEVICES ; do
60                    einfo "$i"
61                    ifconfig "$i" -arp promisc 0.0.0.0 up ; eend $?
62                done
63             eoutdent
64
65             einfo "Adding network devices to $BRIDGE_NAME:"
66             eindent
67             for i in $BRIDGE_DEVICES ; do
68                 einfo "$i"
69                 brctl addif "$BRIDGE_NAME" $i  ; eend $?
70             done
71             eoutdent
72
73             einfo "Bringing bridge $BRIDGE_NAME in promiscuous up"
74             ip link set "$BRIDGE_NAME" promisc on up ; eend $?
75         eoutdent
76    ;;
77
78    stop)
79         check4root || exit 1
80         einfo "Stopping sniffing setup"
81         eindent
82             einfo "Removing network devices from $BRIDGE_NAME: "
83
84             eindent
85                for i in $BRIDGE_DEVICES ; do
86                    einfo "$i "
87                    brctl delif "$BRIDGE_NAME" $i  ; eend $?
88                done
89             eoutdent
90
91             einfo "Disabling promiscuous mode on: "
92             eindent
93                for i in $BRIDGE_DEVICES ; do
94                    einfo "$i "
95                    ip link set "$i" promisc off ; eend $?
96                done
97             eoutdent
98
99             einfo "Bringing bridge $BRIDGE_NAME down"
100             ip link set "$BRIDGE_NAME" down; eend $?
101
102             einfo "Removing bridge device $BRIDGE_NAME"
103             ifconfig "$BRIDGE_NAME" down || /bin/true
104             brctl delbr "$BRIDGE_NAME"
105             eend $?
106         eoutdent
107    ;;
108
109    restart)
110         check4root || exit 1
111         $0 stop
112         sleep 1
113         $0 start
114    ;;
115
116    info|-h|--help)
117         usage_info
118    ;;
119
120    status)
121         check4root || exit 1
122         einfo "$0 - status:"
123         $BRCTL show ; eend $?
124    ;;
125
126    *)
127         echo "Usage: $0 {start|stop|restart|status|info}"
128         exit 1
129    ;;
130 esac
131
132 ## END OF FILE #################################################################
133 # vim: ft=sh expandtab ai