|
rs369007
初级用户
积分 147
发帖 131
注册 2008-9-22
状态 离线
|
『楼 主』:
网络故障诊断bat
@echo off & setlocal enabledelayedexpansion
title %~n0. code by rs + 2010.5.3
set /a last_flag=1
::首先判断物理连接是否正常.
echo [零]判断物理连接是否正常.
ipconfig/all 2>nul | find /i "Media disconnected">nul 2>nul &&(echo.&echo 请检查物理连接.....&pause>nul)||(echo ----ok.)
::启动本地连接或重启本地连接
echo.&echo [一]启动本地连接中......
set num=0
for /f "tokens=2 delims=\" %%k in ('devcon listclass net ^| find /i "ethernet"') do (
if !num! equ 0 set deviceinfo=%%k
set /a num+=1
)
set num1=0
for /L %%j in (0,1,22) do (
if !num1! LSS 2 (
set devicevar=!deviceinfo!
set str=!devicevar:~0,1!
set devicevar=!devicevar:~1!
if "!str!"=="&" (
set /a num1+=1
)
set /a total+=1
)
)
set /a total=!total!-1
set deviceinfo=!deviceinfo:~0,17!
devcon status pci\!deviceinfo!* | find /i "disabled" >nul&&(devcon enable pci\!deviceinfo!*>nul&netsh winsock reset>nul&ping -w 1000 -n 10 127.1>nul)
echo ----ok.
::判断本地连接是否受限
echo.&echo [二]判断本地连接是否受限.
ipconfig/all | find /i "169.254" >nul 2>nul &&(set /a localconn=0)||(set /a localconn=1)
if !localconn! equ 0 (
netsh interface ip set address "本地连接" dhcp>nul
netsh interface ip set dns "本地连接" dhcp>nul
ping -w 1000 -n 6 127.1>nul
ipconfig/all | find /i "169.254" >nul 2>nul &&(set /a localconn=0)||(set /a localconn=1)
)
if !localconn! equ 0 (
echo.&echo 本地连接可能受限.并请先检查交换机接口是否松动...
echo 请确认交换机接口没有松动^!按任意键继续...
pause>nul
echo 执行一系列修复命令.....
echo 1 重新启动本地连接...
devcon restart pci\!deviceinfo!*>nul
echo 2 重置tcp/ip协议组件到干净状态...
netsh int ip reset c:\123.txt
echo 3 重置winsock...
netsh winsock reset >nul
echo 4 重新获取ip...
ipconfig /renew >nul
echo 5 清除地址缓存信息...
arp –d * >nul
echo 6 清空NetBIOS缓存内容...
nbtstat -R >nul
echo 7 清空本地计算机DNS缓存 ...
ipconfig /flushdns >nul
echo 8 WINS服务器上重新注册本地计算机...
nbtstat -RR >nul
echo 9 DNS服务器上重新注册本地计算机...
ipconfig /registerdns >nul
) else (echo ----ok.)
echo.&echo [三]层次化的测试.
echo.&echo 1,判断本地网卡是否工作正常.
ping -w 10 -n 1 127.1 | find /i "timed out" >nul &&(echo.&echo 网卡不能正常工作.&set /a last_flag=0&goto test)||(echo ----ok.)
::判断ip地址是否合法(如果为手动设置的话)
echo.
echo 2,判断ip地址设置是否正确.
::获取ip地址,网关地址.
for /f "tokens=2 delims=:" %%i in ('ipconfig/all ^| find /i "IP Address"') do (
set ipaddr=%%i
set ipaddr=!ipaddr:~1,-1!
)
for /f "tokens=2 delims=:" %%i in ('ipconfig/all ^| find /i "Default Gateway"') do (
set gateway=%%i
set gateway=!gateway:~1,-1!
)
set subnetID=!gateway:~0,-1!
set /a counter=0
ipconfig/all 2>nul | find /I "yes">nul &&(set /a flag=1)||(set /a flag=0)
if !flag! equ 0 (
::判断ip地址是否合法,通过ping测试.
echo !ipaddr! | find /i "!subnetID!">nul &&(set /a flag=1) || (set /a flag=0)
if !flag! equ 0 (
echo 本地ip地址与网关地址不匹配.改变设置中......
netsh interface ip set address "本地连接" dhcp>nul
netsh interface ip set dns "本地连接" dhcp>nul
) else (
for /f "tokens=2 delims=:" %%a in ('ipconfig/all ^|find "Subnet Mask"') do (
set mask=%%a
set mask=!mask:~1,-1!
if NOT "!mask!"=="255.255.255.0" (set /a flag=0) else (echo +掩码检查----ok.)
)
set subnetid=!gateway:~0,-2!
for /L %%j in (1,5,254) do (
ping -w 10 -n 1 !subnetid!.%%j>nul 2>&1
if !ERRORLEVEL! EQU 0 (
set /a counter+=1
)
)
if !counter! GEQ 2 (
if !flag! equ 0 (echo +子网掩码不是24位,可能不正确.但本地网络正常.) else (echo +手动配置ip地址有效----ok.)
) else (echo +请检查ip地址是否有误.&set /a last_flag=0&set /a flagip=0)
)
) else ( echo ----ok.&set /a flag=1&set /a flagip=1)
::进行网关测试
echo.&echo 3,进行网关测试.
if !flagip! equ 1 (
ping -w 30 -n 3 !gateway!>nul 2>nul && (echo ----ok.) || (echo +网关: !gateway! 没有回应.&set /a last_flag=0)
) else (echo +e 跳过此步.)
echo.&echo 4,判断DNS是否正常工作(用www.baidu.com测试).
if !flagip! equ 1 (
nslookup www.baidu.com 2>nul | find /i "timed out">nul 2>1 &&(
echo.&echo DNS服务器不能正确解析域名.&set flag=0 &set /a last_flag=0
) || (echo ----ok.&set /a flag=1)
) else (echo +e 跳过此步.)
echo.&echo 5,检查DNS服务器地址.
ipconfig/all |find /i "DNS Servers" >nul &&(set /a flag=1)||(set /a flag=0 & set /a last_flag=0)
if !flag! equ 0 (
echo DNS地址没有正确获取.改变设置中.....
netsh interface ip set address "本地连接" dhcp>nul
netsh interface ip set dns "本地连接" dhcp>nul
echo 设置完成.
) else (echo ----ok.)
echo.&echo 6,判断是否能够访问Internet.
ping -n 3 www.baidu.com >nul
if !errorlevel! NEQ 0 (
echo +不能连接到Internet.
if !flagip! equ 1 (
echo 路由测试中,请耐心等待...&echo.
set /a last_flag=0
for /f "skip=4 tokens=*" %%a in ('tracert -d -h 5 www.baidu.com') do (
set /a counter+=1
if !counter! LEQ 5 (echo %%a) else goto test
)
)
) else (echo ----ok.)
:test
echo.
if !last_flag! EQU 1 (
echo [+ok]没有发现任何异常,本地连接正常,各种设备工作正常.
) else (
echo [+]测试完成,发现异常.
)
pause>nul
[ Last edited by rs369007 on 2010-5-7 at 15:48 ]
|
|
2008-12-9 17:36 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
|
2008-12-9 18:33 |
|
|
holucan
初级用户
积分 26
发帖 25
注册 2008-11-7
状态 离线
|
『第
3 楼』:
呵呵,LZ又产出新的bat了,支持先!谢谢
|
|
2008-12-9 22:26 |
|
|
yishanju
银牌会员
[b]看你妹啊[/b]
积分 1488
发帖 1357
注册 2006-5-20
状态 离线
|
『第
4 楼』:
有时不能访问网络,禁用网络连接,再启用就好
禁用或者再启用网卡有时间也可以解决问题。
可以加上这样的代码,这样自动化的程序更加高一些
|
有问题请发论坛或者自行搜索,再短消息问我的统统是SB |
|
2008-12-10 00:06 |
|
|
rs369007
初级用户
积分 147
发帖 131
注册 2008-9-22
状态 离线
|
『第
5 楼』:
禁用在启用 网卡 我尝试了 但 无法实现 netsh interface set interface "本地连接“ DISABLED 不知道是不是参数弄错了 还是少了什么
|
|
2008-12-10 09:31 |
|
|
HAT
版主
积分 9023
发帖 5017
注册 2007-5-31
状态 离线
|
『第
6 楼』:
Re 5楼
@echo off
rem 适用于Win2003
rem 把Win2003的ifmon.dll拷贝到WinXP以后WinXP也可以用
rem 禁用本地连接
netsh interface set interface name="本地连接" admin=DISABLED
rem 启用本地连接
netsh interface set interface name="本地连接" admin=ENABLED
@echo off
rem 用批处理调用devcon.exe来实现
echo 请输入你的选择:(禁用网卡为N,启用为Y)
set/p SELECT=
if /i %SELECT%==Y set SELECT=ENABLE&GOTO :DO
if /i %SELECT%==N set SELECT=DISABLE
:DO
for /f "tokens=2 delims=&" %%i in ('devcon find pci\* ^| findstr "Fast Ethernet"') do devcon %SELECT% *%%i*>NUL
'XP下可以用VBS来实现停用/启用本地连接
Const ssfCONTROLS = 3
sConnectionName = "本地连接" '可改成需要控制的连接名称,如"无线网络连接"等
sEnableVerb = "启用(&A)"
sDisableVerb = "停用(&B)" 非xp 系统可能 是禁用
set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)
set oNetConnections = nothing
for each folderitem in oControlPanel.items
if folderitem.name = "网络连接" then
set oNetConnections = folderitem.getfolder: exit for
end if
next
if oNetConnections is nothing then
msgbox "未找到网络连接文件夹"
wscript.quit
end if
set oLanConnection = nothing
for each folderitem in oNetConnections.items
if lcase(folderitem.name) = lcase(sConnectionName) then
set oLanConnection = folderitem: exit for
end if
next
if oLanConnection is nothing then
msgbox "未找到 '" & sConnectionName & "' item"
wscript.quit
end if
bEnabled = true
set oEnableVerb = nothing
set oDisableVerb = nothing
s = "Verbs: " & vbcrlf
for each verb in oLanConnection.verbs
s = s & vbcrlf & verb.name
if verb.name = sEnableVerb then
set oEnableVerb = verb
bEnabled = false
end if
if verb.name = sDisableVerb then
set oDisableVerb = verb
end if
next
'debugging displays left just in case...
'
'msgbox s ': wscript.quit
'msgbox "Enabled: " & bEnabled ': wscript.quit
'not sure why, but invokeverb always seemed to work
'for enable but not disable.
'
'saving a reference to the appropriate verb object
'and calling the DoIt method always seems to work.
'
if bEnabled then
' oLanConnection.invokeverb sDisableVerb
oDisableVerb.DoIt
else
' oLanConnection.invokeverb sEnableVerb
oEnableVerb.DoIt
end if
'adjust the sleep duration below as needed...
'
'if you let the oLanConnection go out of scope
'and be destroyed too soon, the action of the verb
'may not take...
'
wscript.sleep 400
|
|
|
2008-12-10 09:53 |
|
|
yishanju
银牌会员
[b]看你妹啊[/b]
积分 1488
发帖 1357
注册 2006-5-20
状态 离线
|
『第
7 楼』:
禁用网卡 需要第三方工具,devcon.exe
|
有问题请发论坛或者自行搜索,再短消息问我的统统是SB |
|
2008-12-10 09:57 |
|
|
rs369007
初级用户
积分 147
发帖 131
注册 2008-9-22
状态 离线
|
『第
8 楼』:
先谢谢版主 但是我对vbs 一点也不熟 还麻烦 说清楚点
C:\a.vbs
行:6
字符:28
错误:无效字符
|
|
2008-12-10 10:03 |
|