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