rz 1 年之前
父节点
当前提交
7dd7c9df21
共有 1 个文件被更改,包括 11 次插入11 次删除
  1. 11 11
      synchosts.sh

+ 11 - 11
synchosts.sh

@@ -1,46 +1,46 @@
 #!/bin/bash
 
-# 获取当前时间
+# 鑾峰彇褰撳墠鏃堕棿
 current_time=$(date "+%Y%m%d%H%M")
 
-# 备份/etc/hosts文件
+# 澶囦唤/etc/hosts鏂囦欢
 backup_file="/opt/host/hosts.$current_time"
 cp /etc/hosts "$backup_file"
 
 
-# 下载远程文件
+# 涓嬭浇杩滅▼鏂囦欢
 curl -s -o hosts_product.txt http://172.17.32.18:11082/BaseService/jyhosts/raw/master/hosts_product.txt 
 
-# 检查文件是否成功下载
+# 妫€鏌ユ枃浠舵槸鍚︽垚鍔熶笅杞�
 if [ $? -ne 0 ]; then
-  echo "无法下载远程文件"
+  echo "鏃犳硶涓嬭浇杩滅▼鏂囦欢"
   exit 1
 fi
 
-# 对比文件内容并替换
+# 瀵规瘮鏂囦欢鍐呭�骞舵浛鎹�
 
 add_count=0
 while IFS= read -r line || [[ -n "$line" ]]; do
   domain=$(echo "$line" | awk '{print $2}')
   ip=$(echo "$line" | awk '{print $1}')
   
-  # 检查是否存在域名
+  # 妫€鏌ユ槸鍚﹀瓨鍦ㄥ煙鍚�
   grep -q "$domain" /etc/hosts
   
   if [ $? -eq 0 ]; then
-    # 域名存在,检查IP是否一致
+    # 鍩熷悕瀛樺湪锛屾�鏌�P鏄�惁涓€鑷�
     existing_ip=$(grep "$domain" /etc/hosts | awk '{print $1}')
     if [ "$existing_ip" != "$ip" ]; then
-      # IP不一致,进行替换
+      # IP涓嶄竴鑷达紝杩涜�鏇挎崲
       add_count=$((add_count+1))
       sed -i "s/^$existing_ip[[:space:]]*$domain/$ip $domain/" /etc/hosts
     fi
   else
-    # 域名不存在,新增
+    # 鍩熷悕涓嶅瓨鍦�紝鏂板�
     add_count=$((add_count+1))
     echo "$ip $domain" >> /etc/hosts
   fi
 
 done < hosts_product.txt
 
-echo "替换完成: ${add_count}"
+echo "replace success: ${add_count}"