Provide more detailed network information
[grml-quickconfig.git] / quickconfig / 002-ipaddr.sh
1 # always try to gather network information
2 LOCAL_IF=$(ip -o route show | sed -nre '/^default /s/^default .*dev ([^ ]+).*/\1/p' | head -1)
3 if [ -n "${LOCAL_IF}" ] ; then
4   IPADDR=$(ip -o addr show "${LOCAL_IF}" | grep ' inet ' | head -n 1 | sed -e 's/.*inet \([^ ]*\) .*/\1/' -e 's/\/.*//')
5 fi
6
7 LOCAL_IF6=$(ip -6 -o route show | sed -nre '/^default /s/^default .*dev ([^ ]+).*/\1/p' | head -1)
8 if [ -n "${LOCAL_IF6}" ] ; then
9   IPADDR6=$(ip -6 -o addr show "${LOCAL_IF6}" | grep ' inet6 ' | head -n 1 | sed -e 's/.*inet6 \([^ ]*\) .*/\1/' -e 's/\/.*//')
10 fi
11
12 # ssh / password information
13 SSHD_PID=$(pgrep sshd || true)
14 SSH_OPTION=$(grep -Eo '\<ssh=[^ ]+' /proc/cmdline || true)
15 PWD_OPTION=$(grep -Eo '\<passwd=[^ ]+' /proc/cmdline || true)
16
17 if [ -n "${SSHD_PID:-}" ] ; then
18   LINE+="print_line 'You can connect with SSH to: ${IPADDR:-} ${IPADDR6:-} ${AVAHI_INFO:-}';"
19 fi
20
21 USER=$(getent passwd 1000 | cut -d: -f1)
22 [ -n "${USER:-}" ] || USER="grml"
23 if [ -n "${PWD_OPTION:-}" ] ; then
24   PWD_OPTION=${PWD_OPTION#*=}
25   LINE+="print_line 'The password for user root/${USER} is: ${PWD_OPTION:-}';"
26 elif [ -n "${SSH_OPTION:-}" ] ; then
27   SSH_OPTION=${SSH_OPTION#*=}
28   LINE+="print_line 'The password for user root/${USER} is: ${SSH_OPTION:-}';"
29 fi
30
31 LINE+="print_line 'Network information:';"
32
33 NETINFO_PRESENT=false
34
35 # zeroconf information
36 AVAHI_PID=$(pgrep avahi-daemon || true)
37 if [ -n "${AVAHI_PID:-}" ] && [ -x "$(which avahi-resolve-address)" ] && [ -n "${IPADDR:-}" ] ; then
38   AVAHI_INFO="$(avahi-resolve-address "${IPADDR}" 2>/dev/null | awk '{print $2}')"
39   if [ -n "${AVAHI_INFO:-}" ] ; then
40     LINE+="print_line '-> Hostname:   ${AVAHI_INFO}';"
41     NETINFO_PRESENT=true
42   fi
43 fi
44
45 # list all non-localhost IP addresses
46 declare -a IP_LIST
47 for ip in $(
48   {
49     ip -4 -o addr show | grep ' inet '  | sed -e 's/.*inet \([^ ]*\) .*/\1/'  -e 's/\/.*//'
50     ip -6 -o addr show | grep ' inet6 ' | sed -e 's/.*inet6 \([^ ]*\) .*/\1/' -e 's/\/.*//'
51   } | sort -u) ; do
52   case "${ip}" in
53     127.0.0.*)
54       continue
55       ;;
56     ::1)
57       continue
58       ;;
59     fe80::*)
60       continue
61       ;;
62     *)
63       LINE+="print_line '-> IP address: $ip';"
64       NETINFO_PRESENT=true
65       ;;
66   esac
67 done
68
69 if [ "${NETINFO_PRESENT}" = "false" ] ; then
70   LINE+="print_line '-> network seems to be unconfigured (yet)';"
71 fi
72
73 LINE+='print_delim;'
74
75 display_entry() { return 0; }