0 2 * * * /root/mysqlbeifen.sh */10 * * * * /root/dns_update.sh #!/bin/bash LOG_DIR="/var/log/dns_script" HOST_FILE="/etc/hosts_NC" DOMAIN="sso.ccnhub.com" DNS_SERVER="114.114.114.114" # Replace with your DNS server IP log() { local log_file="$LOG_DIR/$(date +\%Y-\%m-\%d).log" echo "$(date +\%Y-\%m-\%d\ \%H:\%M:\%S) - $1" >> "$log_file" } get_dns_ips() { local domain="$1" local dns_server="$2" local resolved_ips resolved_ips=$(nslookup "$domain" "$dns_server" | awk '/^Address: / {print $2}') if [ -z "$resolved_ips" ]; then log "域名 $domain 的DNS解析失败。请检查DNS配置或域名是否存在。检测IP后脚本停止执行" host_file_ips2=($(get_host_file_ips "$DOMAIN")) #重新读取HOSTS文件 ping_and_remove_unreachable "$HOST_FILE" "$DOMAIN" "${host_file_ips2[@]}" exit 1 fi # Filter out IPv6 addresses resolved_ips=$(echo "$resolved_ips" | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}") echo "$resolved_ips" } get_host_file_ips() { local domain="$1" local host_ips host_ips=$(awk -v d="$domain" '$2==d {print $1}' "$HOST_FILE") if [ -z "$host_ips" ]; then log "在$HOST_FILE中未找到$domain的条目。请检查Hosts文件是否正确。" exit 1 fi echo "$host_ips" } delete_host_file_entry() { local host_file="$1" local domain="$2" shift 2 local ips_to_delete=("$@") # Check if the array is empty if [ "${#ips_to_delete[@]}" -eq 0 ]; then echo "数组为空,不执行删除操作。" return fi # Remove entries with the specified IP and domain for ip in "${ips_to_delete[@]}"; do sed -i -e "/^[[:space:]]*$ip[[:space:]]*$domain[[:space:]]*$/d" "$host_file" log "$ip $domain 为过期IP,已经从HOSTS文件删除。" done } update_host_file() { local host_file="$1" local domain="$2" shift 2 local ips_to_update=("$@") # Check if the array is empty if [ "${#ips_to_update[@]}" -eq 0 ]; then echo "数组为空,不执行插入操作。" return fi # Remove existing entries with the specified domain #sed -i -e "/^[[:space:]]*${ips_to_update[0]}[[:space:]]*$domain[[:space:]]*$/d" "$host_file" # Append new IPs to the file for ip in "${ips_to_update[@]}"; do # if ping -c 1 "$ip" &> /dev/null; then if nc -z -w 1 "$ip" 443 &> /dev/null; then echo "$ip $domain" >> "$host_file" log "$ip 新IP的NC端口检测成功。执行插入" else log "$ip 的Ping测试失败。不执行插入操作" fi done } ping_and_remove_unreachable() { local host_file="$1" local domain="$2" local host_ips=("${@:3}") for ip in "${host_ips[@]}"; do #if ping -c 1 "$ip" &> /dev/null; then if nc -z -w 1 "$ip" 443 &> /dev/null; then log "$ip 的Ping测试成功。" else sed -i -e "/^[[:space:]]*$ip[[:space:]]*$domain[[:space:]]*$/d" "$host_file" log "$ip 的Ping测试失败。已从$host_file中移除该条目。" fi done } initialize_log_directory() { if [ ! -d "$LOG_DIR" ]; then mkdir -p "$LOG_DIR" fi } main() { initialize_log_directory log "脚本执行开始。" # 通过DNS解析获取域名的IP地址 dns_ips=($(get_dns_ips "$DOMAIN" "$DNS_SERVER")) # 检查 resolve_domain 的返回值 if [ $? -ne 0 ]; then # resolve_domain 返回非零,说明出错,直接退出脚本 log "DNS解析失败。退出整体脚本。" exit 1 fi # 通过Host文件获取已有的IP地址 host_file_ips=($(get_host_file_ips "$DOMAIN")) # 寻找Host文件中有而DNS解析中没有的IP missing_ips=($(comm -23 <(printf "%s\n" "${host_file_ips[@]}" | sort) <(printf "%s\n" "${dns_ips[@]}" | sort))) # 删除Host文件中多余的IP delete_host_file_entry "$HOST_FILE" "$DOMAIN" "${missing_ips[@]}" # 寻找DNS解析中有而Host文件中没有的IP missing_ips2=($(comm -23 <(printf "%s\n" "${dns_ips[@]}" | sort) <(printf "%s\n" "${host_file_ips[@]}" | sort))) # 插入更新Host文件中的IP,插入前检测 update_host_file "$HOST_FILE" "$DOMAIN" "${missing_ips2[@]}" # 执行循环NC端口检测,发现不可达的IP,从Host文件中删除相应的记录 #ping_and_remove_unreachable "$HOST_FILE" "$DOMAIN" "${host_file_ips[@]}" #已经当作多余IP清理掉,不用重复 host_file_ips2=($(get_host_file_ips "$DOMAIN")) #重新读取HOSTS文件 ping_and_remove_unreachable "$HOST_FILE" "$DOMAIN" "${host_file_ips2[@]}" log "脚本执行完成。" # 清理旧的日志文件(保留3天的日志) find "$LOG_DIR" -name "*.log" -mtime +3 -exec rm {} \; } main
cat /etc/hosts_NC 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 61.14.138.136 sso.ccnhub.com 203.116.74.71 sso.ccnhub.com 58.185.85.222 sso.ccnhub.com You have new mail in /var/spool/mail/root
标签:domain,优选,DNS,ips,host,file,local,BASH,log From: https://www.cnblogs.com/aldary/p/18196795