BAT可手动选择网卡自动配置IP DHCP

1、手动IP配置

①单网卡时,无需选择网卡
②需要管理员权限
③可以根据不同的需要修改脚本实现不同的功能

@echo off

::管理员身份
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit


::查询所有网卡名称
for /f "tokens=4,5 delims= " %%i in ('netsh interface show interface') do (
	echo %%i %%j
	if "%%j" == "%kong%" (
		set int_temp=%%i
	) else (
		set int_temp=%%i %%j
	)
	set /a num1+=1
)

echo.
echo.

::判断是否只有一直张网卡
if %num1% == 2 (
	set network_name=%int_temp%
) else (
	set /p network_name=请输入网卡名称:
)

echo.
echo.

set kong=
::查找选择的网卡
for /f "tokens=4,5 delims= " %%i in ('netsh interface show interface ^| find "%network_name%"') do (
	::判断网卡名称中间是否存在空格
	if "%%j" == "%kong%" (
		set interface=%%i
	) else (
		set interface=%%i %%j
	)
)

echo ----------%interface%----------

echo.
echo.

if "%interface%" == "%kong%" (
	echo 未找到该网卡
	pause
	exit
)
::DHCP配置
::netsh interface ipv4 set address %interface% dhcp
::netsh interface ipv4 set dns %interface% validate=no dhcp

set /p ip=ip地址:
set mask=255.255.255.0
set /p gat=网关:
set dns1=192.168.1.1
set dns2=223.5.5.5

netsh interface ipv4 set address "%interface%" static %ip% %mask% %gat%
netsh interface ipv4 set dns "%interface%" validate=no static %dns1%
netsh interface ipv4 add dns "%interface%" validate=no %dns2%

echo IP配置完成

echo.
echo.
pause

2、全自动IP配置

①自动选择已连接的网卡
②前提网卡要开启并连接状态

@echo off

::管理员身份
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

set kong=
::查找选择的网卡
for /f "tokens=4,5 delims= " %%i in ('netsh interface show interface ^| find "已连接"') do (
	::判断网卡名称中间是否存在空格
	if "%%j" == "%kong%" (
		set interface=%%i
	) else (
		set interface=%%i %%j
	)
)

echo ----------%interface%----------

echo.
echo.

if "%interface%" == "%kong%" (
	echo 未找到网卡,请确保网卡已开启并连接
	pause
	exit
)

netsh interface ipv4 set address "%interface%" static 192.168.1.154 255.255.255.0 192.168.1.1
netsh interface ipv4 set dns "%interface%" validate=no static 192.168.1.1
netsh interface ipv4 add dns "%interface%" validate=no 223.5.5.5

echo IP配置完成

echo.
echo.
pause
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值