怎样用批处理命令修改IP地址

怎样用批处理命令修改IP地址,第1张

批处理内容如下:

netsh

interface

ip

set

address

name="本地连接"

source=static

192.168.1.188

255.255.255.0

192.168.0.1

1

netsh

interface

ip

set

dns

"本地连接"

static

61.128.128.68 primary

netsh

interface

ip

add

dns

"本地连接" 61.128.192.68

第一行中“netsh

interface

ip

set

address”表示设置IP地址,name="本地连接"表示设置哪块网卡的地址,在网络连接中可以看到名称,一般中文版 *** 作系统为“本地连接”或“本地连接2”等,“source=static”表示设置静太IP,如果source=dhcp表示自动获取IP。当source=static时,后面要设置IP地址,子网掩码,网关,跃点数(一般设1),中间用空格分开,即批处理中的“192.168.1.188

255.255.255.0

192.168.0.1

1”。为使程序更加清晰,你也可以这样写“addr=192.168.1.188

mask=255.255.255.0

gateway=192.168.0.1

gwmetric=1”,效果一样。

第二行设置主要dns(域名服务器),static表示静太IP,primary表示主域名服务器。

第三行设置次要dns,“netsh

interface

ip

add

dns”表示添加。add

与set有区别,set会覆盖以前的设置,而add是添加一个设置,不会删除以前的设置。

@echo off

set 前缀=a

:begin

cls

set 序号=

set /p 序号=输入计算机数字序号:

echo%序号%|findstr "^[0-9][0-9]*$">nul||(echo输入无效&>nul ping -n 3 0&goto :begin)

::设置计算机名,重启后生效

set name=%前缀%%序号%

reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ActiveComputerName" /v ComputerName /t reg_sz /d %name% /f >nul 2>nul

reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v "NV Hostname" /t reg_sz /d %name% /f >nul 2>nul

reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters" /v Hostname /t reg_sz /d %name% /f >nul 2>nul

::设置ip

set /a 序号+=20

netsh interface ip set address name="本地连接" source=static addr=192.168.28.%序号% mask=255.255.252.0

netsh interface ip set address name="本地连接" gateway=192.168.28.1 gwmetric=0

netsh interface ip set dns name="本地连接" source=static addr=221.7.128.68 register=PRIMARY

netsh interface ip set wins name="本地连接" source=static addr=114.114.114.114

>nul ping -n 3 0

set /p choice= 您的选择:

echo.

if "%choice%"=="1" goto ip1

if "%choice%"=="2" goto ip2

if "%choice%"=="3" goto end

if "%choice%"=="4" goto test

goto main

:ip1

echo IP自动设置开始....

echo.

echo 正在设置IP及子网掩码

cmd /c netsh interface ip set address name="本地连接" source=static addr=192.168.1.1 mask=255.255.255.0 gateway=192.168.1.1 gwmetric=1

echo 正在设置DNS服务器

cmd /c netsh interface ip set dns name="本地连接" source=static addr=61.234.254.5 register=PRIMARY

echo 正在设置DNS服务器

cmd /c netsh interface ip add dns name="本地连接" addr=59.54.78.210

echo 设置完成

pause

exit

if errorlevel 2 goto main

if errorlevel 1 goto end

:ip2

echo IP自动设置开始....

echo.

echo 自动获取IP地址....

netsh interface ip set address name = "本地连接" source = dhcp

echo 自动获取DNS服务器....

netsh interface ip set dns name = "本地连接" source = dhcp

@rem 设置自动获取IP

echo 设置完成

pause

exit

if errorlevel 2 goto main

if errorlevel 1 goto end

:test

netsh inte***ce ip set address name = "本地连接" source = dhcp

netsh inte***ce ip set dns name = "本地连接" source = dhcp

netsh inte***ce ip set wins name = "本地连接" source = dhcp

CHOICE /C YN /M "退出请按 Y,返回菜单请按 N。"

if errorlevel 2 goto main

if errorlevel 1 goto end

:end

首先新建txt文本文档。将以上内容复制进去。

图中(1)是ip地址,(2)是子网掩码,(3)是默认网关,(4)是首选dns(5)是备用dns。

请根据自己要设置的ip地址进行相应改动。其他无需改动。

修改完成后保存退出,并修改文件名后缀为bat。

双击运行。

输出1 为修改本地连接为你设定好的ip地址。

设置过程中可能提示dns不存在,但不影响。不用理会,结果已成功设置玩ip地址。

按2设置自动获取IP地址。

一秒完成。

另外因为是通过注册列表来修改ip地址的。有的杀毒软件会拦截,拦截请准许。放心,自己写的代码那么简单是没毒的。最好设置白名单。

文件名修改为自动修改IP地址 放到桌面上就可以方便使用了


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6353382.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-20
下一篇 2023-03-20

发表评论

登录后才能评论

评论列表(0条)

保存