| 1 |
#!/bin/bashscip-shell filesystem . . `/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.confscip-shell filesystem . . `/etc/resolv.conf`." |
| 13 |
|
| 14 |
Usagescip-shell shell shell-project 0.0.0 Usage().() { |
| 15 |
echoscip-shell system . . echo. "Get the DNS server IP address for system." |
| 16 |
echoscip-shell system . . echo. "Usage:" |
| 17 |
echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. $0)" |
| 18 |
echoscip-shell system . . echo. "Ex: $(basenamescip-shell system . . basename. $0)" |
| 19 |
} |
| 20 |
|
| 21 |
# |
| 22 |
while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 23 |
case "$1" in |
| 24 |
-*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 25 |
Usagescip-shell shell shell-project 0.0.0 Usage(). >&2 |
| 26 |
exit 2 ;; |
| 27 |
*) break ;; |
| 28 |
esac |
| 29 |
done |
| 30 |
|
| 31 |
nameserver_sys="$(LC_ALL=C grepscip-shell system . . grep. -Ew "^nameserver" $DNS_sys | awkscip-shell system . . awk. -F" " '{print $2}')" |
| 32 |
if [scip-shell system . . `[`. -z "$nameserver_sys" -o -n "$(echoscip-shell system . . echo. "$nameserver_sys" | grepscip-shell system . . 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/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -E "^[[:space:]]*DNS Servers" | awkscip-shell system . . awk. -F":" '{print $2}')" |
| 46 |
if [scip-shell system . . `[`. -z "$nameserver_sys" ]; then |
| •47 |
nameserver_sys="$(LC_ALL=C resolvectl status 2>/dev/nullscip-shell filesystem . . `/dev/null`. | sedscip-shell system . . sed. -r -n '/DNS Servers:/,/(^$|^[[:print:]]*:[[:space:]]+.*)/pscip-shell filesystem . . `/DNS Servers:/,/(^$|^[[:print:]]*:[[:space:]]+.*)/p`.' | sedscip-shell system . . sed. -r -e "s/DNS Servers://g")" |
| 48 |
# Decide the last line if it's the IP address. |
| 49 |
last_line="$(LC_ALL=C echoscip-shell system . . echo. "$nameserver_sys" | tailscip-shell system . . tail. -n 1 | grepscip-shell system . . grep. -E "^[[:print:]]*:[[:space:]]+.*$")" |
| 50 |
fi |
| 51 |
if [scip-shell system . . `[`. -n "$last_line" ]; then |
| 52 |
# Do not show the last line, remove it. |
| 53 |
nameserver_sys="$(echoscip-shell system . . echo. "$nameserver_sys" | headscip-shell system . . head. -n -1)" |
| 54 |
fi |
| 55 |
if [scip-shell system . . `[`. -z "$nameserver_sys" ]; then |
| 56 |
nameserver_sys="$(LC_ALL=C nmcli dev show 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -i "IP4.DNS" | awkscip-shell system . . awk. -F " " '{print $2}')" |
| 57 |
fi |
| 58 |
if [scip-shell system . . `[`. -z "$nameserver_sys" ]; then |
| 59 |
nameserver_sys="$(LC_ALL=C nmcli dev list 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -i "IP4.DNS" | awkscip-shell system . . awk. -F " " '{print $2}')" |
| 60 |
fi |
| 61 |
if [scip-shell system . . `[`. -z "$nameserver_sys" ]; then |
| 62 |
# Last posibility, dnsmasq |
| 63 |
nameserver_sys="$(LC_ALL=C grepscip-shell system . . grep. -Ew "^nameserver" /var/run/dnsmasq/resolv.confscip-shell filesystem . . `/var/run/dnsmasq/resolv.conf`. 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . 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 |
echoscip-shell system . . echo. "$(echoscip-shell system . . echo. $sys_dns | sedscip-shell system . . sed. -r -e "s/^, //")" |