前段时间因为工作,需要反复修改笔记本的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)