3722c6a9b65e627bb1e0a31ecc2f7b19fd019f24
[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 check4progs $BRCTL || exit 1
36
37 case "$1" in
38     start)
39         check4root || exit 1
40         einfo "Starting sniffing setup"
41         eindent
42             einfo "Creating bridge device"
43             brctl addbr "$BRIDGE_NAME"
44             eend $?
45
46             einfo "Bringing network device in promiscuous mode up:"
47             eindent
48                for i in $BRIDGE_DEVICES ; do
49                    einfo "$i"
50                    ifconfig "$i" -arp promisc 0.0.0.0 up ; eend $?
51                done
52             eoutdent
53
54             einfo "Adding network devices to $BRIDGE_NAME:"
55             eindent
56             for i in $BRIDGE_DEVICES ; do
57                 einfo "$i"
58                 brctl addif "$BRIDGE_NAME" $i  ; eend $?
59             done
60             eoutdent
61
62             einfo "Bringing bridge $BRIDGE_NAME in promiscuous up"
63             ip link set "$BRIDGE_NAME" promisc on up ; eend $?
64         eoutdent
65    ;;
66
67    stop)
68         check4root || exit 1
69         einfo "Stopping sniffing setup"
70         eindent
71             einfo "Removing network devices from $BRIDGE_NAME: "
72
73             eindent
74                for i in $BRIDGE_DEVICES ; do
75                    einfo "$i "
76                    brctl delif "$BRIDGE_NAME" $i  ; eend $?
77                done
78             eoutdent
79
80             einfo "Disabling promiscuous mode on: "
81             eindent
82                for i in $BRIDGE_DEVICES ; do
83                    einfo "$i "
84                    ip link set "$i" promisc off ; eend $?
85                done
86             eoutdent
87
88             einfo "Bringing bridge $BRIDGE_NAME down"
89             ip link set "$BRIDGE_NAME" down; eend $?
90
91             einfo "Removing bridge device $BRIDGE_NAME"
92             ifconfig "$BRIDGE_NAME" down || /bin/true
93             brctl delbr "$BRIDGE_NAME"
94             eend $?
95         eoutdent
96    ;;
97
98    restart)
99         check4root || exit 1
100         $0 stop
101         sleep 1
102         $0 start
103    ;;
104
105    info|-h|--help)
106         usage_info
107    ;;
108
109    status)
110         check4root || exit 1
111         einfo "$0 - status:"
112         $BRCTL show ; eend $?
113    ;;
114
115    *)
116         echo "Usage: $0 {start|stop|restart|status|info}"
117         exit 1
118    ;;
119 esac
120
121 ## END OF FILE #################################################################
122 # vim: ft=sh expandtab ai