Add Dns Record Into Hosts
– 2025-08-22
- 重新写了一个,添加了日志记录,换了一个免费的网站,还不必申请API。希望可以多用用,如果好用就捐赠一点。
#!/bin/ash
# Set the domains
DOMAINS="api.themoviedb.org pf.us.dell.com github.com objects.githubusercontent.com"
LOG_FILE="/var/log/dnschecker.log"
log() {
MSG="$1"
echo "$MSG" | tee -a "$LOG_FILE"
}
update_hosts() {
domain=$1
log "🔍 Checking DNS records for $domain ..."
# Perform DNS lookup using new API (with timeout)
API_RESPONSE=$(wget -qO- --timeout=10 "https://networkcalc.com/api/dns/lookup/$domain")
# Check if API response is empty
if [ -z "$API_RESPONSE" ]; then
log "⚠️ Failed to fetch DNS records for $domain (empty response). Skipping."
return
fi
# Check API status
STATUS=$(echo "$API_RESPONSE" | jq -r '.status')
if [ "$STATUS" != "OK" ]; then
log "⚠️ API returned error for $domain (status=$STATUS). Skipping."
return
fi
# Parse JSON response to extract A and AAAA records
A_RECORDS=$(echo "$API_RESPONSE" | jq -r '.records.A[]?.address')
AAAA_RECORDS=$(echo "$API_RESPONSE" | jq -r '.records.AAAA[]?.address')
# If no records found, skip
if [ -z "$A_RECORDS" ] && [ -z "$AAAA_RECORDS" ]; then
log "⚠️ No A/AAAA records found for $domain. Skipping."
return
fi
# Remove existing records for the domain
sed -i "/$domain/d" /etc/myhosts
# Create or append to /etc/myhosts file with a timestamp
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
log "# DNS records for $domain (Updated: $TIMESTAMP)."
for IP in $A_RECORDS; do
echo "$IP $domain" | tee -a /etc/myhosts | tee -a "$LOG_FILE" >/dev/null
done
for IPV6 in $AAAA_RECORDS; do
echo "$IPV6 $domain" | tee -a /etc/myhosts | tee -a "$LOG_FILE" >/dev/null
done
log "✅ DNS records for $domain added to /etc/myhosts"
}
# Rotate log if bigger than 1MB
if [ -f "$LOG_FILE" ] && [ $(stat -c%s "$LOG_FILE") -gt 1048576 ]; then
mv "$LOG_FILE" "$LOG_FILE.$(date +%Y%m%d%H%M%S)"
touch "$LOG_FILE"
fi
# Iterate over each domain and update /etc/myhosts
for domain in $DOMAINS; do
update_hosts "$domain"
done
——————-破网站开始收费了———————————————————————-
通过 https://api.api-ninjas.com/v1/dnslookup 查询DNS解析,并写入/etc/hosts
因为我这个是放在openwrt里面,所以第一行是/bin/ash
API KEY可以自己申请
DOMAINS填写自己需要的,支持多域名,空格分开即可。
#!/bin/ash
# Set the domain and API key
DOMAINS="api.themoviedb.org themoviedb.org"
API_KEY="Your API Key"
update_hosts() {
# Perform DNS lookup using API
API_RESPONSE=$(wget --header="X-Api-Key:$API_KEY" "https://api.api-ninjas.com/v1/dnslookup?domain=$domain" -O-)
# Parse JSON response to extract DNS records
A_RECORDS=$(echo "$API_RESPONSE" | jq -r '.[] | select(.record_type == "A") | .value')
AAAA_RECORDS=$(echo "$API_RESPONSE" | jq -r '.[] | select(.record_type == "AAAA") | .value')
sed -i "/$domain/d" /etc/hosts
# Create or append to /etc/hosts file with a timestamp
TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
# Create or append to /etc/hosts file
echo "# DNS records for $domain (Updated: $TIMESTAMP)." | tee -a /etc/hosts
for IP in $A_RECORDS; do
echo "$IP $domain" | tee -a /etc/hosts
done
for IPV6 in $AAAA_RECORDS; do
echo "$IPV6 $domain" | tee -a /etc/hosts
done
echo "DNS records added to /etc/hosts (Updated: $TIMESTAMP)."
}
# Iterate over each domain and update /etc/hosts
for domain in $DOMAINS; do
update_hosts "$domain"
done