change getip to not use dig but host

use same code in sshd_dns and abort if no ip found
master
David Kebler 2024-08-21 18:48:51 -07:00
parent 7f15b61f23
commit 68bbdccadf
2 changed files with 31 additions and 5 deletions

View File

@ -37,8 +37,8 @@ lookup_host () {
# usage: lookup_host hostname < configfile > # usage: lookup_host hostname < configfile >
local config; local host; local lhost local config; local host; local lhost
config=$([[ $2 ]] && echo $2 || echo ${SSH_CONFIG:-$HOME/.ssh/config}) config=$([[ $2 ]] && echo $2 || echo ${SSH_CONFIG:-$HOME/.ssh/config})
host=$(get_hostname_host $1)return host=$(get_hostname_host $1)
lhost=$(ssh -F $config -G $host | grep -w hostname | cut -d' ' -f2) lhost=$($(which ssh) -F $config -G $host | grep -w hostname | cut -d' ' -f2)
[[ $lhost ]] && echo $lhost || echo $host [[ $lhost ]] && echo $lhost || echo $host
} }
@ -107,6 +107,11 @@ dig +short myip.opendns.com @resolver1.opendns.com
} }
getip () { getip () {
dig +short $1 | tail -1 [[ ! $1 ]] && return 1
if ip=$(host -4 -t A $1); then
echo $ip | awk '{print $NF}'
else
return 2
fi
} }

View File

@ -1,9 +1,21 @@
#!/bin/bash #!/bin/bash
sshd_dns () { sshd_dns () {
local ip
local dir local dir
local dnsconf local dnsconf
local conf local conf
_getip () {
[[ ! $1 ]] && return 1
if ip=$(host -4 -t A $1); then
echo $ip | awk '{print $NF}'
else
return 2
fi
}
dir=${1:-"/etc/ssh/sshd_config.d"} dir=${1:-"/etc/ssh/sshd_config.d"}
pushd "$dir" 1>/dev/null || return; pushd "$dir" 1>/dev/null || return;
for dnsconf in *.conf.dns; do for dnsconf in *.conf.dns; do
@ -14,8 +26,17 @@ for dnsconf in *.conf.dns; do
cat $conf cat $conf
echo -e "\n------------" echo -e "\n------------"
for host in $(sed -e 's/[ ,]/\n/g' $conf | sed -n 's/[Dd][Nn][Ss]://p'); do for host in $(sed -e 's/[ ,]/\n/g' $conf | sed -n 's/[Dd][Nn][Ss]://p'); do
echo found host $host, substituting $(dig +short $host) echo host to lookup $host,
sudo sed -i 's/[Dd][Nn][Ss]:'$host'/'$(dig +short $host)'/g' $conf if ip=$(_getip $host); then
echo substituting $(dig +short $host)
sudo sed -i 's/[Dd][Nn][Ss]:'$host'/'$ip'/g' $conf
else
echo unable to find ip address for $host
echo fatal: removing $conf, exiting,
sudo rm -f $conf
popd 1>/dev/null || return 2
return 2
fi
done done
echo "----- created sshd conf file $conf ----" echo "----- created sshd conf file $conf ----"
cat $conf cat $conf