Hosts檔同步程式
出自洄瀾資訊人
目錄 |
前言
主要目的為同步電腦教室裡各電腦的hosts檔,讓所有電腦裡的hosts檔在開機時會自動去和Server端電腦裡的hosts檔同步,如此,我們只需要維護Server端電腦裡的那一份就好了,不必一台一台電腦去處理,方便管理者管理,同時也減少電腦教室電腦連線到可疑網站的機會。
環境需求
- 主機端:
- 主機端提供網芳服務,並分享一目錄,權限設定為everyone可讀取,必將欲同步的hosts檔放至此目錄下。為了解說方便在此先假定主機端ip為192.168.0.1,且以網芳分享synhost目錄,目錄下放置hosts檔,權限為everyone可讀取,此ip與目錄請依實際環境來設定。
- Client端:
- Client端電腦必需要有用指令模式(command line)能讀取主機端網芳的能力(同一個區域網路下),即我們在client端的電腦可以用下面指令來讀取檔案。
type \\192.168.0.1\synhost\hosts
流程解說
1.client端電腦會檢查今天是否更新過(利用date指令)
- 有 就跳過同步,至第6步結束程式
- 沒有 先產生date記錄(註明今天更新過),至第2步
2.client端電腦先測試與主機是否能連線(利用dir指令)
- 能 至第3步
- 不能 產生error log記錄,至第6步結束程式
3.client看主機端hosts是否存在 (利用if exist指令)
- 在 至第4步
- 不在 產生error log記錄,至第6步結束程式
4.比較client電腦與主機端hosts是否一樣(利用fc指令)
- 同 不同步,至第6步結束程式
- 不同 至第5步
5.開始同步,把hosts檔copy回本機,至第6步
6.結束程式
程式使用說明與解說
在windows的 command line模式下,執行
synhosts.bat [1 [remote path]]
此程式有支援二種參數,以下為說明:
[1] debug模式,會show出執行指令,且能使用指定路徑,程式最後不執行exit
[remote path] 遠端主機網芳路徑 ex:\\192.168.0.1\hello
以下對程式做細部解說,解說文字部份用-!-包起來。
-!-先預設不讓執行之指令顯示出來-!- @echo off -!-程式註解-!- REM filename: synhosts.bat REM version: bata.0.1 REM 程式目的: REM 同步主機上的hosts檔案 REM 全部利用dos指令完成,不需另外的程式。 REM 主機需求啟用網芳,並hosts檔必需有讀取權限。 REM REM 使用方式: REM synhosts.bat [1 [remote path]] REM 參數說明: REM [1] debug模式,會show出執行指令,且能使用指定路徑,程式最後不執行exit REM [remote path] 遠端主機網芳路徑 REM 主要流程: REM 1.client端電腦會檢查今天是否更新過(利用date指令) REM 有 就跳過同步,至第6步結束程式 REM 沒有 先產生date記錄(註明今天更新過),至第2步 REM 2.client端電腦先測試與主機是否能連線(利用dir指令) REM 能 至第3步 REM 不能 產生error log記錄,至第6步結束程式 REM 3.client看主機端hosts是否存在 (利用if exist指令) REM 在 至第4步 REM 不在 產生error log記錄,至第6步結束程式 REM 4.比較client電腦與主機端hosts是否一樣(利用fc指令) REM 同 不同步,至第6步結束程式 REM 不同 至第5步 REM 5.開始同步,把hosts檔copy回本機,至第6步 REM 6.結束程式 REM -- REM update 20090107 REM create by cafetw@gmail.com -!-若第一個參數有值且為1時,表示進入debug模式-!- -!-若沒有參數時,會跳至label show-!- goto show%1 -!-debug模式,設定執行指令必需顯示出來-!- :show1 @echo on :show -!-預設主機路徑設定,請依實際環境更改myserver的值-!- REM -myserver變數為設定預設server路徑 REM -當沒有指定server路徑時,會使用這個路徑 set myserver=\\192.168.0.1\synhost -!-判斷是否有第二個參數,若有第二個參數時,defaultserver值會改變為不為"no"-!- set defaultserver=no set defaultserver=%defaultserver%%2 -!-判斷,若沒有第二個參數則跳至 label defserver-!- -!-若有第二個參數則跳至label cosserver-!- if %defaultserver% == no goto defserver goto cosserver -!-沒有第二個參數時,設定defaultserver為預設主機路徑-!- :defserver set defaultserver=%myserver% goto main -!-有第二個參數,則將defaultserver設為第二個參數-!- :cosserver set defaultserver=%2 -!-設定與判斷參數結束,進入同步主程式-!- :main -!-同步前會先查看之前是不是有同步過-!- -!-同步過的電腦會在電腦上的%systemroot%\system32\drivers\etc\目錄中產生update.log檔案-!- -!-檔案update.log的內容為執行同步當天的日期-!- -!--!- -!-若update.log檔案存在,表示此電腦曾經同步過,並且再判斷update.log內記錄的日期與目前日期是否相同-!- -!-若相同則表示今天有同步過,故不執行同步,若不同則進入hosts檔案的比較判斷-!- -!-(此程式最短同步為一天,當天有同步過,就不打算再同步,以免造成太多網路連線)-!- -!--!- -!-若update.log檔不存在,則表沒有同步過,是第一次執行,則產生update.log檔,跳至hosts檔的比較判斷-!- -!--!- -!-判斷update.log是否存在,存在則跳至label comparelog,不存在則跳至label createlog-!- if exist %systemroot%\system32\drivers\etc\update.log goto comparelog goto createlog -!-比較update.log檔案,利用date指令在tmp目錄下產生有今天日期的update.log檔,以此比較之-!- :comparelog date /T > %tmp%\update.log fc %tmp%\update.log %systemroot%\system32\drivers\etc\update.log /A >NUL -!-用fc比較,若得到的結果是一樣則表今天有同步過,故跳至label end結束程式-!- -!-若得到的結果是不一樣,則表示要執行同步動作,跳至label check執行-!- if %errorlevel% equ 0 goto end -!-更新update.log檔-!- echo. |date /T > %systemroot%\system32\drivers\etc\update.log -!-若執行過程有誤,則跳至label errorjump 產生錯誤記錄並結束程式-!- if %errorlevel% equ 1 goto errorjump goto check -!-利用echo與date /T指令產生update.log記錄檔,檔案內容為當天日期-!- :createlog echo. |date /T > %systemroot%\system32\drivers\etc\update.log -!-若執行過程有誤,則跳至label errorjump 產生錯誤記錄並結束程式-!- if %errorlevel% equ 1 goto errorjump goto check -!-在同步前,先利用dir \\192.168.0.1\synhost\ 指令測試是否連至server-!- -!-若不能連則跳至label errorjump 產生錯誤記錄並結束程式-!- -!-若執行結果為0(表示正常)則跳至 label start 執行hosts檔的比較-!- :check dir/w %defaultserver%\ > NUL IF %ERRORLEVEL% EQU 0 goto start goto errorjump -!-比較前先看server端的hosts檔是否存在,若不存在則跳至label errorjump 產生錯誤記錄並結束程式-!- -!-若存在則跳至label dosyn 開始比較hosts-!- :start IF EXIST %defaultserver%\hosts goto dosyn goto errorjump -!-比較主機端的hosts檔與本地端的hosts檔是否一樣-!- -!-若一樣就結束程式,若不同則跳至label copying 執行同步hosts檔動作-!- :dosyn fc %defaultserver%\hosts %systemroot%\system32\drivers\etc\hosts /A >NUL IF %ERRORLEVEL% EQU 1 goto copying goto end -!-執行同步動作-!- -!-先把本地端hosts檔屬性拿掉,再copy備份一份為hosts_old-!- -!-並把主機端的檔案copy至本地端,最後再把本地端hosts檔屬性加回-!- :copying attrib -R %systemroot%\system32\drivers\etc\hosts copy %systemroot%\system32\drivers\etc\hosts %systemroot%\system32\drivers\etc\hosts_old copy %defaultserver%\hosts %systemroot%\system32\drivers\etc\hosts attrib +R %systemroot%\system32\drivers\etc\hosts goto end -!-只要執行有錯誤都會由此產生錯誤記錄error.log,此檔產生在c:\下-!- -!-(目前並沒有把錯誤分類,所有錯誤都只會產生內容為errorjump的檔案)-!- -!-只要產生錯誤,則update.log檔就會被刪掉,以便下次執行時會直更新hosts檔-!- :errorjump echo errorjump > %systemdrive%\error.log del %systemroot%\system32\drivers\etc\update.log >NUL goto exit1 -!-若第一個參數有加(debug模式),則會跳至label exit1執行-!- -!-不執行exit讓dos畫面留下,以方便debug-!- :end goto exit%1 :exit exit :exit1
完整程式
@echo off REM filename: synhosts.bat REM version: bata.0.1 REM 程式目的: REM 同步主機上的hosts檔案 REM 全部利用dos指令完成,不需另外的程式。 REM 主機需求啟用網芳,並hosts檔必需有讀取權限。 REM REM 使用方式: REM synhosts.bat [1 [remote path]] REM 參數說明: REM [1] debug模式,會show出執行指令,且能使用指定路徑,程式最後不執行exit REM [remote path] 遠端主機網芳路徑 REM 主要流程: REM 1.client端電腦會檢查今天是否更新過(利用date指令) REM 有 就跳過同步,至第6步結束程式 REM 沒有 先產生date記錄(註明今天更新過),至第2步 REM 2.client端電腦先測試與主機是否能連線(利用dir指令) REM 能 至第3步 REM 不能 產生error log記錄,至第6步結束程式 REM 3.client看主機端hosts是否存在 (利用if exist指令) REM 在 至第4步 REM 不在 產生error log記錄,至第6步結束程式 REM 4.比較client電腦與主機端hosts是否一樣(利用fc指令) REM 同 不同步,至第6步結束程式 REM 不同 至第5步 REM 5.開始同步,把hosts檔copy回本機,至第6步 REM 6.結束程式 REM -- REM update 20090107 REM create by cafetw@gmail.com goto show%1 :show1 @echo on :show REM -myserver變數為設定預設server路徑 REM -當沒有指定server路徑時,會使用這個路徑 set myserver=\\192.168.0.1\synhost set defaultserver=no set defaultserver=%defaultserver%%2 if %defaultserver% == no goto defserver goto cosserver :defserver set defaultserver=%myserver% goto main :cosserver set defaultserver=%2 :main if exist %systemroot%\system32\drivers\etc\update.log goto comparelog goto createlog :comparelog date /T > %tmp%\update.log fc %tmp%\update.log %systemroot%\system32\drivers\etc\update.log /A >NUL if %errorlevel% equ 0 goto end echo. |date /T > %systemroot%\system32\drivers\etc\update.log if %errorlevel% equ 1 goto errorjump goto check :createlog echo. |date /T > %systemroot%\system32\drivers\etc\update.log if %errorlevel% equ 1 goto errorjump goto check :check dir/w %defaultserver%\ > NUL IF %ERRORLEVEL% EQU 0 goto start goto errorjump :start IF EXIST %defaultserver%\hosts goto dosyn goto errorjump :dosyn fc %defaultserver%\hosts %systemroot%\system32\drivers\etc\hosts /A >NUL IF %ERRORLEVEL% EQU 1 goto copying goto end :copying attrib -R %systemroot%\system32\drivers\etc\hosts copy %systemroot%\system32\drivers\etc\hosts %systemroot%\system32\drivers\etc\hosts_old copy %defaultserver%\hosts %systemroot%\system32\drivers\etc\hosts attrib +R %systemroot%\system32\drivers\etc\hosts goto end :errorjump echo errorjump > %systemdrive%\error.log del %systemroot%\system32\drivers\etc\update.log >NUL goto exit1 :end goto exit%1 :exit exit :exit1
