Codebase list debian-goodies / 7bf88a9
Imported Debian version 0.39 Javier Fernandez-Sanguino 12 years ago
5 changed file(s) with 10 addition(s) and 616 deletion(s). Raw diff Collapse all Expand all
0 debian-goodies (0.39) unstable; urgency=low
1
2 * Remove the network-test script, which now has been moved to the
3 ifupdown-extra package. Also remove the dependencies introduced by
4 that script.
5
6 -- Javier Fernandez-Sanguino Pen~a <jfs@debian.org> Tue, 18 Dec 2007 21:37:18 +0100
7
08 debian-goodies (0.38) unstable; urgency=low
19
210 * Fix bug in network-test which prevent it from working properly
77 Package: debian-goodies
88 Architecture: all
99 Depends: dctrl-tools | grep-dctrl, curl, python (>= 2.4), lsof, whiptail | dialog
10 Suggests: popularity-contest, netcat, xdg-utils
10 Suggests: popularity-contest, xdg-utils
1111 Conflicts: debget
1212 Replaces: debget
1313 Description: Small toolbox-style utilities for Debian systems
3535 install -d $(CURDIR)/debian/debian-goodies/usr/bin
3636 install -d $(CURDIR)/debian/debian-goodies/usr/sbin
3737 install -m 755 dgrep dglob debget dpigs debman popbugs which-pkg-broke \
38 network-test \
3938 $(CURDIR)/debian/debian-goodies/usr/bin
4039 install -m 755 checkrestart \
4140 $(CURDIR)/debian/debian-goodies/usr/sbin
5554 dh_testroot
5655 dh_installdocs README
5756 dh_installman debget.1 debman.1 dglob.1 dgrep.1 dpigs.1 popbugs.1 \
58 which-pkg-broke.1 network-test.1 checkrestart.1 \
57 which-pkg-broke.1 checkrestart.1 \
5958 debmany/man/debmany.1 debmany/man/debmany.de.1
6059 # dh_undocumented
6160 dh_installchangelogs
+0
-528
network-test less more
0 #!/bin/bash
1 # Network testing script v 1.8
2 # (c) 2005,2006 Javier Fernandez-Sanguino
3 #
4 # This script will test your system's network configuration using basic
5 # tests and providing both information (INFO messages), warnings (WARN)
6 # and possible errors (ERR messages) by checking:
7 # - Interface status
8 # - Availability of configured routers, including the default route
9 # - Proper host resolution, including DNS checks
10 # - Proper network connectivity, including ICMP and web connections to
11 # a remote web server (the web server used for the tests can be configured,
12 # see below)
13 #
14 # Some of the network tests are described in more detail at
15 # http://ubuntuforums.org/archive/index.php/t-25557.html
16 #
17 # The script does not need special privileges to run as it does not
18 # do any system change. It also will not fix the errors by itself.
19 #
20 # Additional software requirements:
21 # * ip from the iproute package. (could probably be rewrittent to
22 # use ifconfig only or to parse /proc)
23 # * ping from the iputils-ping package or the netkit-ping package.
24 # * nc from the netcat package.
25 #
26 # This program is free software; you can redistribute it and/or modify
27 # it under the terms of the GNU General Public License as published by
28 # the Free Software Foundation; either version 2 of the License, or
29 # (at your option) any later version.
30 #
31 # This program is distributed in the hope that it will be useful,
32 # but WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 # GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License
37 # along with this program; if not, write to the Free Software
38 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #
40 # You can also find a copy of the GNU General Public License at
41 # http://www.gnu.org/licenses/licenses.html#TOCLGPL
42 #
43 # TODO
44 # - Works only on Linux, can this be generalised for other UNIX systems
45 # (probably not unless rewritten in C)
46 # - Does not check for errors properly, use -e and test intensively
47 # so that expected errors are trapped
48 # (specially for tools that are not available, like netcat)
49 # - If the tools are localised to languages != english the script might
50 # break
51 # - Ask 'host' maintainer to implement error codes as done with
52 # dlint
53 # - Should be able to check if DNS server is in the same network, if
54 # it doesn't answer to pings, check ARP in that case.
55 # - DHCP checks?
56 # - Other internal services tests? (LDAP if using pam...)
57 # - Generate summary of errors in the end (pretty report?)
58 # - Check if packets are being dropped by local firewall? (use dmesg
59 # and look for our tests)
60 # - Support wireless interfaces? (use iwconfig)
61 # - Check for more than one web server (have CHECK_HOSTS be a number
62 # of hosts and determine a metric to spout an error) ?
63 # - Use traceroute or tcptraceroute to see if there is network connectivity?
64 # (traceroute is usually blocked by firewalls but tcptraceroute might
65 # be an alternative to using nc)
66 # - Use mii-tool (requires root privileges)
67 # - Use ping -s XXXX to detect invalid MTUs
68 # - Use arpping to detect another host with our same IP address
69 # - Check other TODOs inline in the code
70
71 # Defaults
72 VERB=3
73 LOG=0
74 while getopts ":hsv:" Option
75 do
76 case $Option in
77 h ) cat <<- EOF
78 Usage: $0 [-s][-v <num>]
79
80 -s Also log messages to local3 syslog facility
81 -v 0 Silent run
82 -v 1 Show only error messages
83 -v 2 Show error and warning messages
84 -v 3 Fully verbose (default)
85
86 EOF
87 exit 0;;
88 v ) VERB=$OPTARG;;
89 s ) LOG=1;;
90 esac
91 done
92
93 # BEGIN configuration
94 # Configure to your needs, these values will be used when
95 # checking DNS and Internet connectivity
96 # DNS name to resolve.
97 # These are default values which can be overriden by the environment.
98 [ -z "$CHECK_HOST" ] && CHECK_HOST=www.debian.org
99 [ -z "$CHECK_IP_ADRESS" ] && CHECK_IP_ADRESS=194.109.137.218
100 # Web server to check for
101 [ -z "$CHECK_WEB_HOST" ] && CHECK_WEB_HOST=www.debian.org
102 [ -z "$CHECK_WEB_PORT" ] && CHECK_WEB_PORT=80
103 # END configuration
104 export CHECK_HOST CHECK_IP_ADRESS CHECK_WEB_HOST CHECK_WEB_PORT
105
106 PATH=/bin:/sbin:/usr/bin:/usr/sbin
107 LC_ALL=C
108 export PATH LC_ALL
109
110 # error reporting and logging functions
111 info () {
112 [ "$VERB" -gt 2 ] && echo "INFO: $1"
113 [ "$VERB" -gt 2 ] && [ "$LOG" -eq 1 ] && logger -p local3.info "$0 INFO: $1"
114 }
115
116 warn () {
117 [ "$VERB" -gt 1 ] && echo "WARN: $1"
118 [ "$VERB" -gt 1 ] && [ "$LOG" -eq 1 ] && logger -p local3.warn "$0 WARN: $1"
119 }
120
121 err () {
122 [ "$VERB" -gt 0 ] && echo "ERR: $1" >&2
123 [ "$VERB" -gt 0 ] && [ "$LOG" -eq 1 ] && logger -p local3.err "$0 ERR: $1"
124 }
125
126
127 # Check if all commands we need are available
128 # NOTE: if using nslookup add "nslookup dnsutils"
129 ( echo -e "netstat net-tools\nifconfig net-tools\n\
130 ping netkit-ping|inetutils-ping|iputils-ping\n\
131 arp net-tools\nip iproute\nhost host|bind9-host\nmktemp debianutils\n\
132 nc netcat" |
133 while read cmd package; do
134 if ! `which $cmd 2>/dev/null >&2`; then
135 err "$cmd is not available! (please install $package)"
136 exit 1
137 fi
138 done ) || exit 1
139 # Recommended programs
140 ( echo -e "ethtool ethtool" |
141 while read cmd package; do
142 if ! `which $cmd 2>/dev/null >&2`; then
143 warn "$cmd is not available (consider installing $package)"
144 exit 1
145 fi
146 done )
147
148 # Default route for programs
149 ETHTOOL=/usr/sbin/ethtool
150 MIITOOL=/sbin/mii-tool
151
152 # Other needs
153 # We need /proc/net
154 if [ ! -d /proc/net ] ; then
155 err "/proc is not available! Please mount it ('mount -t /proc')"
156 exit 1
157 fi
158
159
160 # Extract the interface of our default route
161
162 defaultif=`netstat -nr |grep ^0.0.0.0 | awk '{print $8}' | head -1`
163 defaultroutes=`netstat -nr |grep ^0.0.0.0 | wc -l`
164 if [ -z "$defaultif" ] ; then
165 defaultif=none
166 warn "This system does not have a default route"
167 elif [ "$defaultroutes" -gt 1 ] ; then
168 warn "This system has more than one default route"
169 else
170 info "This system has exactly one default route"
171 fi
172
173
174
175 # Check loopback
176 check_local () {
177 # Is there a loopback interface?
178 if [ -n "`ip link show lo`" ] ; then
179 # OK, can we ping localhost
180 if ! check_host localhost 1; then
181 # Check 127.0.0.1 instead (not everybody uses this IP address however,
182 # although its the one commonly used)
183 if ! check_host 127.0.0.1 1; then
184 err "Cannot ping localhost (127.0.0.1), loopback is broken in this system"
185 else
186 err "Localhost is not answering but 127.0.0.1, check /etc/hosts and verify localhost points to 127.0.0.1"
187 fi
188 else
189 info "Loopback interface is working properly"
190 fi
191
192 else
193 err "There is no loopback interface in this system"
194 status=1
195 fi
196 status=0
197 return $status
198 }
199
200 check_if_link_miitool () {
201 ifname=$1
202 [ ! -x "$MIITOOL" ] && return 0
203 status=0
204 if $MIITOOL $ifname 2>&1| grep -q "no link"; then
205 status=1
206 fi
207 return $status
208 }
209
210 check_if_link_ethtool () {
211 # Check if the interface has link
212 # Note: Unlike other sections of the script we need to be root
213 # to test this
214 ifname=$1
215 [ ! -x "$ETHTOOL" ] && return 0
216 status=0
217 LINK=`$ETHTOOL $ifname 2>&1| grep "Link detected"`
218 # If ethtool fails to print out the link line we break off
219 # notice that ethtool cannot get the link status out of all
220 # possible network interfaces
221 [ -z "$LINK" ] && return
222 if ! echo $LINK | grep -q "Link detected: yes" ; then
223 status=1
224 fi
225 return $status
226 }
227
228 check_if_link_iplink () {
229 ifname=$1
230 status=0
231 [ ! -x /sbin/ip ] && return 0
232 if /sbin/ip link show $ifname 2>&1 | grep -q "NO-CARRIER"; then
233 status=1
234 fi
235 return $status
236 }
237
238
239
240 check_if_link() {
241 status=-1
242 iface=$1
243 # Use ethtool if installed (preferable to mii-tool)
244 # If none are installed we will test using 'ip link show'
245 if [ "`id -u`" -eq 0 ] ; then
246 if [ -x "$ETHTOOL" ] ; then
247 check_if_link_ethtool $iface
248 status=$?
249 elif [ -x "$MIITOOL" ]; then
250 check_if_link_miitool $iface
251 status=$?
252 fi
253 fi
254 # If no test has done use ip link
255 if [ $status -eq -1 ]; then
256 check_if_link_iplink $iface
257 status=$?
258 fi
259 return $status
260 }
261
262 # Check network interfaces
263 check_if () {
264 ifname=$1
265 status=0
266 [ -z "$ifname" ] && return 1
267 # Check if the interface has a link
268 case "$ifname" in
269 eth*) check_if_link $ifname ; status=$?;;
270 *) ;;
271 esac
272 # Print results
273 if [ $status -ne 0 ] ; then
274 if [ "$ifname" = "$defaultif" ] ; then
275 err "The $ifname interface that is associated with your default route has no link!"
276 else
277 warn "Interface $ifname does not have link"
278 fi
279 fi
280 # Find IP addresses for $ifname
281 inetaddr=`ip addr show $ifname | grep inet | awk '{print $2}'`
282 if [ -z "$inetaddr" ] ; then
283 warn "The $ifname interface does not have an IP address assigned"
284 status=1
285 else
286 # TODO: WARN if more than 2 IP addresses?
287 echo $inetaddr | while read ipaddr; do
288 info "The $ifname interface has IP address $ipaddr assigned"
289 done
290 fi
291
292 # Lookup TX and RX statistics
293 # TODO: This is done using ifconfig but could use /proc/net/dev for
294 # more readibility or, better, 'netstat -i'
295 txpkts=`ifconfig $ifname | awk '/RX packets/ { print $2 }' |sed 's/.*://'`
296 rxpkts=`ifconfig $ifname | awk '/RX packets/ { print $2 }' |sed 's/.*://'`
297 txerrors=`ifconfig $ifname | awk '/TX packets/ { print $3 }' |sed 's/.*://'`
298 rxerrors=`ifconfig $ifname | awk '/RX packets/ { print $3 }' |sed 's/.*://'`
299 # TODO: Check also frames and collisions, to detect faulty cables
300 # or network devices (cheap hubs)
301 if [ "$txpkts" -eq 0 ] && [ "$rxpkts" -eq 0 ] ; then
302 err "The $ifname interface has not tx or rx any packets. Link down?"
303 status=1
304 elif [ "$txpkts" -eq 0 ]; then
305 warn "The $ifname interface has not transmitted any packets."
306 elif [ "$rxpkts" -eq 0 ] ; then
307 warn "The $ifname interface has not received any packets."
308 else
309 info "The $ifname interface has tx and rx packets."
310 fi
311 # TODO: It should be best if there was a comparison with tx/rx packets.
312 # a few errors are not uncommon if the card has been running for a long
313 # time. It would be better if a relative comparison was done (i.e.
314 # less than 1% ok, more than 20% warning, over 80% major issue, etc.)
315 if [ "$txerrors" -ne 0 ]; then
316 warn "The $ifname interface has tx errors."
317 fi
318 if [ "$rxerrors" -ne 0 ]; then
319 warn "The $ifname interface has rx errors."
320 fi
321 return $status
322 }
323
324 check_netif () {
325 status=0
326 ip link show | egrep '^[[:digit:]]' |
327 while read ifnumber ifname status extra; do
328 ifname=`echo $ifname |sed -e 's/:$//'`
329 # TODO: this is redundant with the check if_link test
330 # (although faster since using it would make us call 'ip'
331 # twice.
332 if [ -n "`echo $status | grep NO-CARRIER`" ] ; then
333 if [ "$ifname" = "$defaultif" ] ; then
334 err "The $ifname interface that is associated with your default route is down!"
335 status=1
336 elif [ "$ifname" = "lo" ] ; then
337 err "Your lo interface is down, this might cause issues with local applications (but not necessarily with network connectivity)"
338 else
339 warn "The $ifname interface is down"
340 fi
341 else
342 # Check network routes associated with this interface
343 info "The $ifname interface is up"
344 check_if $ifname
345 check_netroute $ifname
346 fi
347 done
348 return $status
349 }
350
351 check_netroute () {
352 ifname=$1
353 [ -z "$ifname" ] && return 1
354 netstat -nr | grep "${ifname}$" |
355 while read network gw netmask flags mss window irtt iface; do
356 # For each gw that is not the default one, ping it
357 if [ "$gw" != "0.0.0.0" ] ; then
358 if ! check_router $gw ; then
359 err "The default route is not available since the default router is unreachable"
360 fi
361 fi
362 done
363 }
364
365 check_router () {
366 # Checks if a router is up
367 router=$1
368 [ -z "$router" ] && return 1
369 status=0
370 # First ping the router, if it does not answer then check arp tables and
371 # see if we have an arp. We use 5 packets since it is in our local network.
372 ping -n -q -c 5 "$router" >/dev/null 2>&1
373 if [ "$?" -ne 0 ]; then
374 warn "Router $router does not answer to ICMP pings"
375 # Router does not answer, check arp
376 routerarp=`arp -n | grep "^$router" | grep -v incomplete`
377 if [ -z "$routerarp" ] ; then
378 err "We cannot retrieve a MAC address for router $router"
379 status=1
380 fi
381 fi
382 if [ "$status" -eq 0 ] ; then
383 info "The router $router is reachable"
384 fi
385 return $status
386 }
387
388 check_host () {
389 # Check if a host is reachable
390 # TODO:
391 # - if the host is in our local network (no route needs to be used) then
392 # check ARP availability
393 # - if the host is not on our local network then check if we have a route
394 # for it
395 host=$1
396 [ -z "$host" ] && return 1
397 # Use 10 packets as we expect this to be outside of our network
398 COUNT=10
399 [ -n "$2" ] && COUNT=$2
400 status=0
401 ping -n -q -c $COUNT "$host" >/dev/null 2>&1
402 if [ "$?" -ne 0 ]; then
403 warn "Host $host does not answer to ICMP pings"
404 status=1
405 else
406 info "Host $host answers to ICMP pings"
407 fi
408 return $status
409 }
410
411 check_dns () {
412 # Check the nameservers defined in /etc/resolv.conf
413 status=1
414 nsfound=0
415 nsok=0
416 tempfile=`mktemp -t tmptestnet.XXXXXX` || { err "Cannot create temporary file! Aborting! " ; exit 1; }
417 trap " [ -f \"$tempfile\" ] && /bin/rm -f -- \"$tempfile\"" 0 1 2 3 13 15
418 cat /etc/resolv.conf | grep -v ^# | grep nameserver |
419 awk '/nameserver/ { for (i=2;i<=NF;i++) { print $i ; } }' >$tempfile
420 for nameserver in `cat $tempfile`; do
421 nsfound=$(( $nsfound + 1 ))
422 info "This system is configured to use nameserver $nameserver"
423 check_host $nameserver 5
424 if check_ns $nameserver ; then
425 nsok=$(( $nsok +1 ))
426 else
427 status=$?
428 fi
429 done
430 #Could also do:
431 #nsfound=`wc -l $tempfile | awk '{print $1}'`
432 /bin/rm -f -- "$tempfile"
433 trap 0 1 2 3 13 15
434 if [ "$nsfound" -eq 0 ] ; then
435 err "The system does not have any nameserver configured"
436 else
437 if [ "$status" -ne 0 ] ; then
438 if [ "$nsfound" -eq 1 ] ; then
439 err "There is one nameserver configured for this system but it does not work properly"
440 else
441 err "There are $nsfound nameservers configured for this system and none of them works properly"
442 fi
443 else
444 if [ "$nsfound" -eq 1 ] ; then
445 info "The nameserver configured for this system works properly"
446 else
447 info "There are $nsfound nameservers is configured for this system and $nsok are working properly"
448 fi
449 fi
450 fi
451 return $status
452 }
453
454 check_ns () {
455 # Check the nameserver using host
456 # TODO: use nslookup?
457 # nslookup $CHECK_HOST -$nameserver
458 nameserver=$1
459 [ -z "$nameserver" ] && return 1
460 status=1
461 CHECK_RESULT="$CHECK_HOST .* $CHECK_IP_ADDRESS"
462 # Using dnscheck:
463 dnscheck=`host -t A $CHECK_HOST $nameserver 2>&1 | tail -1`
464 if [ -n "`echo $dnscheck |grep NXDOMAIN`" ] ; then
465 err "Dns server $nameserver does not resolv properly"
466 elif [ -n "`echo $dnscheck | grep \"timed out\"`" ] ; then
467 err "Dns server $nameserver is not available"
468 elif [ -z "`echo $dnscheck | egrep \"$CHECK_RESULT\"`" ] ; then
469 warn "Dns server $nameserver did not return the expected result for $CHECK_HOST"
470 else
471 info "Dns server $nameserver resolved correctly $CHECK_HOST"
472 status=0
473 fi
474
475 # Using dlint
476 # dlint $CHECK_HOST @$nameserver >/dev/null 2>&1
477 # if [ $? -eq 2 ] ; then
478 # err "Dns server $nameserver does not resolv properly"
479 # elif [ $? -ne 0 ]; then
480 # err "Unexpected error when testing $nameserver"
481 # else
482 # info "Dns server $nameserver resolved correctly $CHECK_HOST"
483 # status=0
484 # fi
485
486 return $status
487 }
488
489 check_conn () {
490 # Checks network connectivity
491 if ! check_host $CHECK_WEB_HOST >/dev/null ; then
492 warn "System does not seem to reach Internet host $CHECK_WEB_HOST through ICMP"
493 else
494 info "System can reach Internet host $CHECK_WEB_HOST"
495 fi
496 status=0
497 # Check web access, using nc
498 # TODO:
499 # - this could also implement proxy checks (if the http_proxy environment is
500 # defined?)
501 # - could also check against a valid content copy (otherwise it might be
502 # fooled by transparent proxies)
503 echo -e "HEAD / HTTP/1.0\n\n" |nc -w 20 $CHECK_WEB_HOST $CHECK_WEB_PORT >/dev/null 2>&1
504 if [ $? -ne 0 ] ; then
505 err "Cannot access web server at Internet host $CHECK_WEB_HOST"
506 status=1
507 else
508 info "System can access web server at Internet host $CHECK_WEB_HOST"
509 fi
510 return $status
511 }
512
513 # TODO: checks could be conditioned, i.e. if there is no proper
514 # interface setup don't bother with DNS and don't do some Inet checks
515 # if DNS is not setup properly
516 check_local || exit 1
517 check_netif || exit 1
518 check_dns || exit 1
519 check_conn || exit 1
520
521 exit 0
522
523
524 # Set our locale environment, just in case ethtool gets translated
525 LC_ALL=C
526 export LC_ALL
527
+0
-85
network-test.1 less more
0 .\" network-test.1 - check the network and test if everything is OK
1 .\" Copyright (C) 2006 Javier Fernandez-Sanguino
2 .\" Everybody is allowed to distribute this manual page,
3 .\" to modify it, and to distribute modifed versions of it.
4 .TH network-test 1 "July 24 2006" "debian\-goodies" "debian\-goodies"
5 .SH NAME
6 network-test \- check the network and test if everything is fine
7 .SH SYNOPSIS
8 .B network-test
9 .SH DESCRIPTION
10 The
11 .B network-test
12 program will test your system's network configuration using basic
13 tests and providing both information (\fBINFO\fP), warnings (\fBWARN\fP)
14 and possible errors (\fBERR\fP) by checking:
15 .RS
16 * Interface status, number of transmitted packets and error rates.
17
18 * Availability of configured routers, including the default router.
19
20 * Proper host resolution, testing DNS resolution against a known host.
21
22 * Proper network connectivity, testing reachability of remote hosts using
23 ICMP and simulating a web connections to a remote web server (the web server
24 used for the tests can be configured through the environment, see below)
25 .RE
26
27 .P
28 The script does not need special privileges to run as it does not
29 do any system change.
30
31 .SH ENVIRONMENT
32
33 The program will, by default, check
34 .B www.debian.org
35 and its associated web server. If you want to use a different check host you
36 can setup the environment as follows:
37 .br
38 .TP
39 .B CHECK_HOST
40 The name of a host to use when testing DNS resolution.
41 .TP
42 .B CHECK_IP_ADRESS
43 The IP address of the host defined in
44 .B CHECK_HOST
45 .TP
46 .B CHECK_WEB_HOST
47 The web server to use for testing purposes when testing network connectivity.
48 .TP
49 .B CHECK_WEB_PORT
50 The web server port of server
51 .B CHECK_WEB_HOST
52 that will be used for testing.
53
54 .SH EXIT STATUS
55
56 The program will exit with error (1) if any of the network checks fail.
57
58 .SH BUGS
59 This program does not have \fIsuper cow powers\fP so it is unable to fix the
60 errors by itself. It is also unable to detect if the network is failing due to
61 a local firewall policy been in place so make sure you check your system logs
62 with
63 .B dmesg(1)
64
65 .\".SH SEE ALSO
66
67 .SH AUTHOR
68
69 .B network-test
70 was written by Javier Fernandez-Sanguino for the Debian
71 GNU/Linux distribution.
72
73 .SH COPYRIGHT AND LICENCE
74
75 Copyright (C) 2005,2006 Javier Fernandez-Sanguino <jfs@debian.org>.
76
77 This program is free software; you can redistribute it and/or modify
78 it under the terms of the GNU General Public License as published by
79 the Free Software Foundation; either version 2, or (at your option)
80 any later version.
81
82 On Debian systems, a copy of the GNU General Public License may be
83 found in /usr/share/common-licenses/GPL.
84