Rework debian/ to follow current best practices
[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   . "$CONFIG_FILE"
21 else
22   ewarn "Could not read $CONFIG_FILE"
23 fi
24
25 # defaults if unconfigured
26 [ -n "$BRCTL" ]          || BRCTL='brctl'
27 [ -n "$BRIDGE_NAME" ]    || BRIDGE_NAME='br0'
28 [ -n "$BRIDGE_DEVICES" ] || BRIDGE_DEVICES='eth0 eth1'
29
30 check_devs() {
31   for dev in $BRIDGE_DEVICES ; do
32     ip link show dev "$dev" >/dev/null 2>&1 || return 1
33  done
34 }
35
36 if ! check_devs ; then
37   eerror "BRIDGE_DEVICES $BRIDGE_DEVICES do not seem to exist." >&2
38   exit 1
39 fi
40
41 check4progs $BRCTL || exit 1
42
43 case "$1" in
44     start)
45         check4root || exit 1
46         einfo "Starting sniffing setup"
47         eindent
48
49             einfo "Creating bridge device"
50             brctl addbr "$BRIDGE_NAME"
51             eend $?
52
53             einfo "Bringing network device in promiscuous mode up:"
54             eindent
55                for i in $BRIDGE_DEVICES ; do
56                    einfo "$i"
57                    ifconfig "$i" -arp promisc 0.0.0.0 up ; eend $?
58                done
59             eoutdent
60
61             einfo "Adding network devices to $BRIDGE_NAME:"
62             eindent
63             for i in $BRIDGE_DEVICES ; do
64                 einfo "$i"
65                 brctl addif "$BRIDGE_NAME" $i  ; eend $?
66             done
67             eoutdent
68
69             einfo "Bringing bridge $BRIDGE_NAME in promiscuous up"
70             ip link set "$BRIDGE_NAME" promisc on up ; eend $?
71         eoutdent
72    ;;
73
74    stop)
75         check4root || exit 1
76         einfo "Stopping sniffing setup"
77         eindent
78             einfo "Removing network devices from $BRIDGE_NAME: "
79
80             eindent
81                for i in $BRIDGE_DEVICES ; do
82                    einfo "$i "
83                    brctl delif "$BRIDGE_NAME" $i  ; eend $?
84                done
85             eoutdent
86
87             einfo "Disabling promiscuous mode on: "
88             eindent
89                for i in $BRIDGE_DEVICES ; do
90                    einfo "$i "
91                    ip link set "$i" promisc off ; eend $?
92                done
93             eoutdent
94
95             einfo "Bringing bridge $BRIDGE_NAME down"
96             ip link set "$BRIDGE_NAME" down; eend $?
97
98             einfo "Removing bridge device $BRIDGE_NAME"
99             ifconfig "$BRIDGE_NAME" down || /bin/true
100             brctl delbr "$BRIDGE_NAME"
101             eend $?
102
103         eoutdent
104    ;;
105
106    restart)
107         check4root || exit 1
108         $0 stop
109         sleep 1
110         $0 start
111    ;;
112
113    info|-h|--help)
114         usage_info
115    ;;
116
117    status)
118         check4root || exit 1
119         einfo "$0 - status:"
120         $BRCTL show ; eend $?
121    ;;
122
123    *)
124         echo "Usage: $0 {start|stop|restart|status|info}"
125         exit 1
126    ;;
127 esac
128
129 ## END OF FILE #################################################################
130 # vim: ft=sh expandtab ai