『楼 主』:
[原创][分享]内网自动搜索并设置IP
可通过配置文件设置要搜索的网络,若没有则提示输入,并且进行合法性检查,已通过多种条件测试,若你在使用过程中发现问题,欢迎指出。
::net.bat - 自动搜索网络并设置IP,同时具有检测冲突并修改IP的功能(需要将/rp *****改为/ru用户的密码)
::eventtriggers工具在xp pro系统下可用,也可通过windows安装盘上support\tools下获取。
::genteman - 2007-11-11 -CMD@WinXP Pro
::contact amdaround@163.com
@echo off
title 自动搜索并设置IP author:genteman
type nul>%~dp0notusedip.log
if not exist %~dp0net.ini goto setip
for /f %%i in (net.ini) do (
echo %%i | findstr /r "IPADDR=[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" && set %%i
echo %%i | findstr /r "NETMASK=255\.255\.[0-2][0-9]*\.[0-2][0-9]*" && set %%i
echo %%i | findstr /r "GATEWAY=[0-2][0-9]*\.[0-9]*\.[0-9]*\.[1-9][0-9]*" && set %%i
echo %%i | findstr /r "PRIMARYDNS=[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" && set %%i
echo %%i | findstr /r "SECONDARYDNS=[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" && set %%i
)
if not defined IPADDR (
goto setip
) else (
goto predo
)
:setip
set /p IPADDR=请输入内网起始IP?
if not defined IPADDR echo IP不能为空 & goto setip
:predo
call :isvalid IPADDR %IPADDR%
if not defined IPADDR goto setip
set IPADDR=%IPADDR: =%
for /f "delims=. tokens=1-4" %%i in ('echo %IPADDR%') do (
set i_1=%%i
set i_2=%%j
set i_3=%%k
set i_4=%%l
)
if not defined NETMASK (
goto setmask
) else (
setlocal
goto split
)
:setmask
set /p NETMASK=请输入子网掩码?
if not defined NETMASK set NETMASK=255.255.255.0 & goto subnet
set NETMASK | findstr /r "NETMASK=255\.255\.[0-2][0-9]*\.[0-2][0-9]*"
:split
if not errorlevel 1 (
for /f "delims=. tokens=1-4" %%i in ("%NETMASK%") do (
set m_1=%%i
set m_2=%%j
set m_3=%%k
set m_4=%%l
)
) else (
echo 非法掩码!请重新输入 & goto setmask
)
setlocal enableextensions enabledelayedexpansion
for /f "delims== tokens=1,2" %%a in ('set m_') do (
set var=%%b
if not "!var:~3!"=="" echo 非法掩码!请重新输入 & goto setmask
if %%b LSS 128 (
if %%b NEQ 0 (
echo 非法掩码!请重新输入
endlocal & goto setmask
)
)
)
set /a decm="(%m_3%<<8)+%m_4%"
if %decm%==0 goto subnet
set sss=0xffff
:loop2
set var2=%decm%
set var3=%sss%
set /a "decm>>=1"
set /a "sss>>=1"
set /a b=var2%%decm
if %b%==0 goto loop2
set /a ttt="var2&var3"
if not %var3%==%ttt% echo 非法掩码!请重新输入 & goto setmask
:subnet
setlocal enabledelayedexpansion
set /a int_ip="(%i_3%<<8)+%i_4%"
set /a int_mask="(%m_3%<<8)+%m_4%"
set /a network="int_ip&int_mask"
set /a end_ip="0xffff^int_mask|int_ip"
echo 正在搜索网络,请稍候....
for /l %%s in (%int_ip%,1,%end_ip%) do (
set /a i_3="(%%s&0xff00)>>8"
set /a i_4="%%s&0x00ff"
ping -l 1 -n 1 -w 0 %i_1%.%i_2%.!i_3!.!i_4!>nul
arp -a %i_1%.%i_2%.!i_3!.!i_4! | findstr /r /i /v "%i_1%.%i_2%.!i_3!.!i_4!.*[sd]">nul
if errorlevel==0 echo %i_1%.%i_2%.!i_3!.!i_4!>>%~dp0notusedip.log
)
echo 完成。开始设置主机
:setgw
if not defined GATEWAY set /p GATEWAY=请输入网关IP?
if not defined GATEWAY echo 网关不能为空 & goto setgw
call :isvalid GATEWAY %GATEWAY%
if not defined GATEWAY goto setgw
set GATEWAY=%GATEWAY: =%
if not defined PRIMARYDNS set /p PRIMARYDNS=请输入DNS IP?
echo 正在设置,请稍后....
for /f %%f in (%~dp0notusedip.log) do (
ping -l 1 -n 1 -w 0 %%i>nul
arp -a %%f | findstr /r /i /v "%%f.*[sd]">nul
if errorlevel 0 (
for /f "delims=. tokens=1-4" %%i in ('echo %%f') do (
set i_1=%%i
set i_2=%%j
set i_3=%%k
set i_4=%%l
)
if !i_4! NEQ 0 (
if not !i_4!==255 (
if not %%f==%GATEWAY% (
set IPADDR=%%f
goto setnet
)
)
)
)
)
:setnet
netsh int ip set addr name="本地连接" source=static addr=%IPADDR% mask=%NETMASK%
netsh int ip set addr name="本地连接" gateway=%GATEWAY% gwmetric=0
if defined PRIMARYDNS (
set PRIMARYDNS=%PRIMARYDNS: =%
netsh int ip set dns name="本地连接" static %PRIMARYDNS% PRIMARY
)
if defined SECONDARYDNS (
set SECONDARYDNS=%SECONDARYDNS: =%
netsh int ip add dns name="本地连接" %SECONDARYDNS%
)
echo 完成。
endlocal & goto :EOF
::/***************************************************
::以下代码判断IP是否合法
:isvalid
setlocal enabledelayedexpansion enableextensions
set %1 | findstr /r "%1=[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
if not errorlevel 1 (
for /f "delims=. tokens=1-4" %%i in ("%2") do (
set i_1=%%i
set i_2=%%j
set i_3=%%k
set i_4=%%l
)
) else (
echo 非法IP!请重新输入
endlocal & set %1=
goto :EOF
)
for /f "delims== tokens=1,2" %%a in ('set i_') do (
set var=%%b
if not "!var:~3!"=="" (
echo 非法IP!请重新输入
endlocal & set %1=
goto :EOF
)
call :del0 %%a %%b
)
for /f "delims== tokens=1,2" %%a in ('set i_') do (
if %%b GTR 255 (
echo 非法IP!请重新输入
endlocal & set %1=
goto :EOF
)
)
goto islan
:del0
set var=%2
:loop
if "%var:~0,1%"=="0" (
set var=%var:~1%
goto :loop
)
if "!var!"=="" set var=0
endlocal & set %1=%var% & goto :EOF
:islan
echo %i_1% | findstr /v "10 172 192" && set pos=第1段%i_1%
if %i_1%==172 (
set /a var="%i_2%&0x10"
if var==0 (
set pos=%pos%,第2段%i_2%
)
)
if %i_1%==192 (
if not %i_2%==168 (
set pos=%pos%,第2段%i_2%
)
)
if defined pos (
echo %1不是内网IP,%pos%输入错误,请重新输入
endlocal & set %1=
goto :EOF
)
endlocal & set %1=%i_1%.%i_2%.%i_3%.%i_4%
eventtriggers create /l system /eid 4199 /tk %~dp0ipguard /ru administrator /rp ******
goto :EOF
::以上代码判断IP是否合法
::*********************************************************/
rem set date=
rem set time=
rem set sdate=%date:~0,10%
rem set sdate=%sdate:-=/%
rem set sdate=%sdate:~5,2%/%sdate:~-2%/%sdate:~0,4%
rem set stime=%time:~0,8%
rem if %stime:~0,2% LEQ 12 (
rem set stime=%stime%AM
rem ) else (
rem if %stime:~0,2% GTR 12 (
rem set /a hh=%stime:~0,2%-12
rem set stime=%hh%%stime:~-6%PM
rem )
rem cscript %windir%\system32\eventquery.vbs /fi "source eq tcpip" /fi "type eq error" /fi "datetime gt %sdate%,%stime%" /fi "id eq 4199"/r 20 /v /l system | find ""
rem if not errorlevel 1 ()
|