windows批處理設置ip地址


  前段時間因為工作,需要反復修改筆記本的ip地址,因為使用的是windows系統,每次修改ip地址都得點擊鼠標半天。幾經修改心中萬馬奔騰。后來想着寫個批處理文件替代每次點擊鼠標設置ip地址,因為之前沒學過批處理,所以批處理是寫一步網上查一步,最后總算是寫好了。雖然算不上完美 ,但是工作中湊合也還行。(其實在網上也找到過批處理的腳本,但是覺得不太實用,本着自己動手豐衣足食的精神,還是自己寫的符合自己的需求)

######以下為代碼部分

@ echo off
::author:舊事凝
::定義本地網卡名稱
set local_lan=以太網
::無線網卡名稱
set local_wlan=wlan
echo            “ This script is used to quickly set up local connections and wireless network IP addresses.”
set /p interface="wlan or lan ?(w/l): "
if  "%interface%"=="w"  goto WLAN
if  "%interface%"=="l"  goto LOCAL

:LOCAL
    echo “local_lan”
    set /p protocl="dhcp or static ?(d/s): "
    if "%protocl%"=="d" goto dhcp
    if "%protocl%"=="s" goto static
        :static
            set /p ip_address="please input ip address: "
            set /p netmask="please input netmask: "
            set /p gateway="please input gateway: "
            netsh interface ip set address %local_lan% static %ip_address% %netmask% %gateway%
            set /p dns="input dns server: "
            if %dns%==""  goto nx
            netsh interface ip set dns %local_lan% %dns%
            :nx
            netsh interface ip set dns %local_lan% static %dns%
            exit 0
        :dhcp
            netsh interface ip set address %local_lan% dhcp
            netsh interface ip set dns %local_lan% dhcp

exit 0
:WLAN
    echo “local_wlan”
    set /p protocl="dhcp or static ?(d/s): "
    if "%protocl%"=="d" goto dhcp
    if "%protocl%"=="s" goto static
        :static
        echo “static”
        set /p ip_address="please input ip address: "
        set /p netmask="please input netmask: "
        set /p gateway="please input gateway: "
            netsh interface ip set address %local_wlan% static %ip_address% %netmask% %gateway%

            set /p dns="input dns server: "
            if "%dns%"==""  goto nx
            netsh interface ip set dns %local_wlan% static %dns%
            exit 0
            :nx
            netsh interface ip set dns %local_wlan% static 114.114.114.114
            exit 0
        :dhcp
        netsh interface ip set address %local_wlan% dhcp
        netsh interface ip set dns %local_wlan% dhcp

exit 0
pause
##########end

  代碼部分解釋一下,set local_lan和set local_wlan部分是定義網卡名稱的(我自己筆記本上本地連接的網卡名稱,根據自己需求修改)

我的筆記本上本地連接是“以太網”,無線網連接是“wlan”。其他部分可以不同修改。

另外的說下,windows中批處理修改計算機ip地址需要管理員權限,所以執行該批處理需要管理員權限,建議將該批處理做成.bat文件,然后

創建一個快捷方式,將快捷方式執行權限設置為管理員,這樣就不同每次右擊管理員運行了。

使用步驟,執行該批處理

提示wlan or lan,表示你是需要設置本地連接還是無線網連接的IP地址,(批處理中需要先定義網卡名稱),設置本地連接就輸入“l”,然后回車,無線網就是“w”。

輸入網卡之后,會提示ip地址是設置dhcp還是static,如果是dhcp,輸入“d” 回車即可。如果是靜態ip則輸入“s”

靜態設置ip,會提示你輸入ip地址,子網掩碼和網關。

 

回車后即可設置ip,子網掩碼和網關。

 另外還會提示讓你設置dns,如果你有想要設置的dns,則手動輸入,如果不想輸入dns,回車即可,會默認設置為114.114.114.114的dns

 

 

 可以將該批處理的快捷方式放在PATH環境變量里,或者新增一個快捷方式所在的目錄到PATH中,這樣每次設置ip都可以通過win+r調出運行窗口,輸入批處理快捷方式的名稱就可以了(我自己將快捷方式命令為ipset)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM