Debian Codegraph @

scripts/bin/drbl-get-dnsserver

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/bin/bash
2 # Author: Steven Shiau, <steven _at_ clonezilla org>
3 # License: GPL
4 # Program to get DNS server
5
6 # Load DRBL setting and functions
7 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
8
9 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
10 #
11 # Settings
12 DNS_sys="/etc/resolv.conf"
13
14 Usage() {
15 echo "Get the DNS server IP address for system."
16 echo "Usage:"
17 echo "$(basename $0)"
18 echo "Ex: $(basename $0)"
19 }
20
21 #
22 while [ $# -gt 0 ]; do
23 case "$1" in
24 -*) echo "${0}: ${1}: invalid option" >&2
25 Usage >&2
26 exit 2 ;;
27 *) break ;;
28 esac
29 done
30
31 nameserver_sys="$(LC_ALL=C grep -Ew "^nameserver" $DNS_sys | awk -F" " '{print $2}')"
32 if [ -z "$nameserver_sys" -o -n "$(echo "$nameserver_sys" | grep -E "^127\..*")" ]; then
33 # On Ubuntu, dnsmasq with network-manager, the result might 127.0.0.1. We use nm-tool to get the real one
34 # Ubuntu >= 22.04, use "resolvectl status"
35 # Ubuntu >= 17.10 or Debian >= 9, use "systemd-resolve --status"
36 # Ubuntu <= 14.04, use "nmcli dev list", while > 14.04, "nmcli dev show".
37 # Results of systemd-resolve --status, example:
38 # DNS Servers: 8.8.8.8
39 # 8.8.4.4
40 # or
41 # DNS Servers: 8.8.8.8
42 # 8.8.4.4
43 # 168.95.1.1
44 # DNSSEC NTA: 10.in-addr.arpa
45 nameserver_sys="$(LC_ALL=C resolvectl status 2>/dev/null | grep -E "^[[:space:]]*DNS Servers" | awk -F":" '{print $2}')"
46 if [ -z "$nameserver_sys" ]; then
47 nameserver_sys="$(LC_ALL=C systemd-resolve --status 2>/dev/null | sed -r -n '/DNS Servers:/,/(^$|^[[:print:]]*:[[:space:]]+.*)/p' | sed -r -e "s/DNS Servers://g")"
48 # Decide the last line if it's the IP address.
49 last_line="$(LC_ALL=C echo "$nameserver_sys" | tail -n 1 | grep -E "^[[:print:]]*:[[:space:]]+.*$")"
50 fi
51 if [ -n "$last_line" ]; then
52 # Do not show the last line, remove it.
53 nameserver_sys="$(echo "$nameserver_sys" | head -n -1)"
54 fi
55 if [ -z "$nameserver_sys" ]; then
56 nameserver_sys="$(LC_ALL=C nmcli dev show 2>/dev/null | grep -i "IP4.DNS" | awk -F " " '{print $2}')"
57 fi
58 if [ -z "$nameserver_sys" ]; then
59 nameserver_sys="$(LC_ALL=C nmcli dev list 2>/dev/null | grep -i "IP4.DNS" | awk -F " " '{print $2}')"
60 fi
61 if [ -z "$nameserver_sys" ]; then
62 # Last posibility, dnsmasq
63 nameserver_sys="$(LC_ALL=C grep -Ew "^nameserver" /var/run/dnsmasq/resolv.conf 2>/dev/null | awk -F" " '{print $2}')"
64 fi
65 fi
66
67 sys_dns=""
68 # For multiple NIC configured with DNS,
69 # nameserver_sys can be like: '8.8.8.8 DNS Domain: ~. 8.8.8.8 DNS Domain: ~. '
70 # Hence we only put correct IPv4 address.
71 # Ref: https://groups.google.com/g/drbl/c/ebgoMdLIo9c
72 # Thanks to Alber Yang (alber.yang _at_ gmail com) for reporting this issue.
73 for i in $nameserver_sys; do
74 if `is_valid_IPv4_add $i`; then
75 sys_dns="$sys_dns, $i"
76 fi
77 done
78 # Strip the leading ", "
79 echo "$(echo $sys_dns | sed -r -e "s/^, //")"