grml-network: nmap reports "0 hosts up" instead of "down" nowadays
[grml-network.git] / sbin / grml-network
1 #!/bin/bash
2 # Filename:      grml-network
3 # Purpose:       simple frontend to varous connection tools
4 # Authors:       (c) Klaus Knopper Mar 2004, (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 PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin"
10 export PATH
11
12 if [ $(id -ru) -ne 0 ] ; then
13   echo 1>&2 "Error: please run this script with uid 0 (root)." ; exit 1
14 fi
15
16 # XDIALOG_HIGH_DIALOG_COMPAT=1
17 # export XDIALOG_HIGH_DIALOG_COMPAT
18
19 CHECK=""
20 [ "$1" = "check" ] && CHECK="yes"
21
22 TMP=$(mktemp)
23
24 bailout(){
25   rm -f "$TMP"
26   exit $1
27 }
28
29 DIALOG="dialog"
30 # [ -n "$DISPLAY" ] && [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog"
31
32 trap bailout 1 2 3 15
33
34 # LANGUAGE etc.
35 [ -r /etc/default/locale ] && . /etc/default/locale
36
37 gauge(){
38   rm -f "$TMP.done"
39   status=0
40   while [ ! -e "$TMP.done" ]; do
41     echo "$status"
42     status="`expr \( 100 - $status \) / 4 + $status`"
43     sleep 1
44   done | $DIALOG --title "$0" --gauge "$1" 8 75 0
45 }
46
47 # Stop status bar
48 killgauge(){
49   touch "$TMP.done" ; wait ; rm -f "$TMP.done"
50 }
51
52 main(){
53   if [ -z "$NOCHECK" -a -x /usr/bin/nmap ] ; then
54   gauge "Checking network status..." &
55     STATUS=""
56     GW="$(echo $(route -n | awk '/^0\.0\.0\.0/{print $2}'))"
57     GWDEV="$(echo $(route -n | awk '/^0\.0\.0\.0/{print $NF}'))"
58     NMAP="$(nmap -sP --host_timeout 4000 --max_rtt_timeout 4000ms $GW 2>/dev/null)"
59
60     if [ $? -eq 0 ]; then
61       if ! echo "$NMAP" | grep -q '0 hosts up' ; then
62         STATUS="Online"
63       fi
64     fi
65
66     killgauge
67
68     if [ -n "$STATUS" ]; then
69        [ -n "$CHECK" ] && exit 0 # exit if we are already connected
70        STATUS="Online ($GWDEV)"
71     else
72        STATUS="Offline"
73     fi
74   else
75     STATUS="Unknown"
76   fi
77
78 # Language-dependent Messages
79 case "$LANGUAGE" in
80   de*|at*|ch*)
81     TITLE1="Netzwerk-Setup"
82     STATUS="Status: $STATUS"
83     MESSAGE1="Bitte auswaehlen:"
84     NETCARD="Netzwerkkarten (LAN/WLAN) Konfiguration"
85     DSL="Einwahl per DSL-Modem"
86     ISDN="Einwahl per ISDN"
87     EXIT="Beenden"
88     ;;
89   *)
90     TITLE1="Network Setup"
91     STATUS="Current state: $STATUS"
92     MESSAGE1="Please select:"
93     NETCARD="Configure network card (LAN/WLAN)"
94     DSL="Dial via DSL-adapter"
95     ISDN="Dial via ISDN"
96     EXIT="Quit"
97    ;;
98 esac
99
100 # Shortcut description selected
101 TYPES=(netcardconfig "$NETCARD" \
102 pppoeconf "$DSL" \
103 isdnconfig "$ISDN")
104
105 type pppoeconf   >/dev/null 2>&1 || { unset TYPES[2];  unset TYPES[3];  }
106 type isdnconfig  >/dev/null 2>&1 || { unset TYPES[4];  unset TYPES[5];  }
107
108 rm -f "$TMP"
109
110 $DIALOG --clear --cancel-label "$EXIT" --title "$TITLE1" --menu "$STATUS
111
112 $MESSAGE1" 18 75 10 "${TYPES[@]}" 2>"$TMP" || bailout 1
113
114 read TYPE <"$TMP"
115 TYPE="${TYPE#\"}"; TYPE="${TYPE%\"}"
116 rm -f "$TMP"
117
118 eval $TYPE
119 return "$?"
120 }
121
122 if [ -z "$NOCHECK" ] ; then
123   while true; do
124     main
125   done
126 else
127   main
128 fi
129
130 ## END OF FILE #################################################################