bib2html.py: Add incollection bibtype
[shutils.git] / ifonline
1 #!/bin/sh
2
3 set -e
4
5 usage() {
6 cat << EOF
7 Executes a given command if there is an internet connection. Availability of an
8 connection is tested by (i) the availability of a default route and (ii) the
9 pingability of a test host.
10
11 Usage:
12 $0 [OPTIONS]
13 $0 -h
14 $0 -c "echo 'online'"
15
16 OPTIONS:
17 -h Show this text
18 -v Verbose output
19 -p HOST Host for ping test. Default: 8.8.8.8
20 -c CMD Command to execute if online
21 EOF
22 }
23
24 CMD=""
25 VERBOSE=0
26 PINGHOST="8.8.8.8"
27
28 while getopts "hvc:p:" OPTION; do
29
30 case "${OPTION}" in
31 h)
32 usage
33 exit
34 ;;
35 v)
36 VERBOSE=1
37 ;;
38 p)
39 PINGHOST=${OPTARG}
40 ;;
41 c)
42 CMD=${OPTARG}
43 ;;
44 esac
45 done
46
47 if [ -z "${CMD}" ]; then
48 echo "No command given."
49 usage
50 exit 2
51 fi
52
53 if [ -z "$(ip r list exact default)" ]; then
54 if [ "1" -eq "${VERBOSE}" ]; then
55 echo "Not online because no default route."
56 fi
57 exit
58 fi
59
60
61 if ! ping -c1 "${PINGHOST}" 2>&1 >/dev/null; then
62 if [ "1" -eq "${VERBOSE}" ]; then
63 echo "Not online because cannot ping ${PINGHOST}."
64 fi
65 exit
66 fi
67
68 ${CMD}