返回列表 发帖

[网络连接] [分享]单网卡环境,ip地址自动获取,或者固定ip地址的批处理

win系统测试通过。

ip地址-固定修改为(192.168.8.20)
@echo off
title IP设置
mode con lines=7 cols=50
setlocal enabledelayedexpansion
color a
set ip=192.168.8.20
set mask=255.255.255.128
set "gateway=192.168.8.1"
set  "DNS1= "
set  "DNS2= "
set isIP=false
set "online="
set "tips="
set "s="
:input_ip
cls
echo.&echo.
call :check_ip
if "%isIP%" == "false" cls &echo.&echo.&echo.&echo %s%%s%IP地址格式不正确,按任意键返回重新输入! & pause >nul & goto input_ip
for /f "delims=" %%i in ('ipconfig^|find "以太网"') do (
  set name=%%i
  set name=!name:~7,-1!
  for /f "delims=" %%i in ('netsh interface show interface "!name!" ^| find "已连接"') do set "online=!online! ^"!name!^""
)
for %%i in (!online!) do (
  set /a c+=1
  set "lan!c!=%%i"
)
if !c! == 1 call :set_ip !lan1! & goto :eof
:choose
set /a c+=5
mode con lines=!c! cols=50
cls
echo.&echo %s%已连网的网卡:
for %%i in (!online!) do (set /a n+=1&echo %s%%s%!n!.%%~i)
echo.
set /p choose=%s%请选择给哪个网卡设置IP地址:
if "%choose%" == "" set "n=" & goto :choose
if not defined lan%choose% set "n=" & goto :choose
call :set_ip !lan%choose%!
:check_ip
if "%ip%" == "" set "isIP=false" & exit /b
for /f "tokens=1-5 delims=." %%i in ("%ip%") do (
  if "%ip:~-1%" == "." set "isIP=false" & exit /b
  if "%%m" neq "" set "isIP=false" & exit /b
  if %%i  leq 0 set "isIP=false" & exit /b
  if %%i  gtr 252 set "isIP=false" & exit /b
  if %%j  leq 0 set "isIP=false" & exit /b
  if %%j  gtr 252 set "isIP=false" & exit /b
  if %%k  leq 0 set "isIP=false" & exit /b
  if %%k gtr 252 set "isIP=false" & exit /b
  if %%l  leq 1 set "isIP=false" & exit /b
  if %%l  gtr 252 set "isIP=false" & exit /b
  set "isIP=true" & exit /b
)
:set_ip
netsh interface ipv4 set address "%~1" static %ip% %mask% %gateway% 1 >nul
netsh interface ipv4 add dns "%~1" %DNS1% index=1 >nul
netsh interface ipv4 add dns "%~1" %DNS2% index=2 >nul
cls&echo.&echo.&echo %s%%s%%s%%s%IP设置完成,按任意键退出! & pause >nul & goto :eofCOPY

ip地址-自动获取
@echo off & setlocal enabledelayedexpansion
echo 代码开始
echo 65001编码获取: > nul
chcp 65001 >nul
for /f "delims=" %%a in ('netsh interface show interface^|more +3') do (
    set "str=%%a"
    for /f "tokens=*" %%b in ("!str:~46!") do set interfaceName=%%b
)
rem 代码保存为ansi编码的bat文件--显示乱码
rem 代码保存为编码utf-8编码的bat文件--正常
echo,网卡名称: "%interfaceName%"
pause & cls
chcp 936 > nul
echo 936编码输出: > nul
rem 代码保存为ansi编码的bat文件---正常
rem 代码保存为编码utf-8编码的bat文件--显示乱码
echo,网卡名称: "%interfaceName%"
pause & cls
set "macname=%interfaceName%"
netsh int ip set dns "%macname%" dhcp >nul
netsh int ip set address name="%macname%" source=dhcp
netsh interface ip set wins name="%macname%" source=dhcp
ipconfig /renew & cls & ipconfig /flushdns & cls
echo 设置成功。& 1pause & cls & exit /bCOPY

TOP

ip地址-自动获取
@ECHO off
echo 65001编码获取: > nul
chcp 65001 >nul
reg query "HKU\S-1-5-19" >nul 2>&1 || (echo 请右键“以管理员身份运行”&echo.&pause&exit /b 1)
REM 获取联网网卡名称,并重置ip-dns等地址为自动获取
for /f tokens^=2^ delims^=^" %%i in ('netsh interface ip show address') do (set "macname=%%i"
goto:Next)
:Next
chcp 936 > nul
echo 936编码输出: > nul
echo 网卡名称:"%macname%"
pause & cls
netsh int ip set dns "%macname%" dhcp >nul
netsh int ip set address name="%macname%" source=dhcp
netsh interface ip set wins name="%macname%" source=dhcp
ipconfig /renew & cls & ipconfig /flushdns & cls
echo 设置成功。& 1pause & cls & exit /bCOPY

TOP

ip地址-自动获取
@ECHO off
echo 65001编码获取: > nul
chcp 65001 >nul
:Netsh_Interface_Show_Interface
set "Netsh_N="&&for /f "skip=3 tokens=1-3,* delims= " %%w in ('"Netsh.exe Interface Show Interface"')do set/a "Netsh_N+=1"&&for /f %%n in ('"call echo,%%Netsh_N%%"')do set "Netsh_Connect_%%n=%%w %%x %%y %%z"
for /f "tokens=1,* delims==" %%i in ('set Netsh_Connect_')do for /f "tokens=1-3,* delims= " %%w in ("%%j")do (
  Netsh.exe interface Ip Set Address Name="%%z" Source=dhcp
  Netsh.exe interface Ip Set DnsServers Name="%%z" Source=dhcp
)
pause
ipconfig /renew & cls & ipconfig /flushdns & cls
goto:eofCOPY

TOP

显示IP-DNS地址等信息。
@ECHO off
REM 显示IP-DNS等配置信息
ipconfig /all|findstr "[0-9]*\.[0-9]*\."
echo    计算机名--------------------------: %computername%
echo ----------------------------------------------------------
echo                                         ↑ 本机网络配置信息
pauseCOPY

TOP

计算机名-手动修改
@echo off
set /p name=输入计算机名,按回车键:
echo %name%
1pause
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /t reg_sz /d %name% /f >nul 2>nul
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
reg add "HKLM\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName" /v "ComputerName" /t REG_SZ /d "%name%" /f
reg add "HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "NV Hostname" /t REG_SZ /d "%name%" /f
reg add "HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters" /v "Hostname" /t REG_SZ /d "%name%" /f
reg add "HKLM\SYSTEM\ControlSet002\Control\ComputerName\ComputerName" /v "ComputerName" /t REG_SZ /d "%name%" /f
exitCOPY

TOP

返回列表