找回密码
 加入
搜索
查看: 8590|回复: 20

[系统综合] 如何判断网络电缆是否插好?(需判断电缆连通却未上网的状态)[已解决]

 火.. [复制链接]
发表于 2010-2-12 15:48:09 | 显示全部楼层 |阅读模式
本帖最后由 xym_xuyimin 于 2010-2-17 12:40 编辑

如题,如何使用脚本判断网络电缆是否插好?需区分以下三种状态:
1、电缆接通,已经上网。
2、电缆接通,暂未上网。(因使用PPPoE拨号)
3、电缆断开。

谢谢各位。PS:“已解决”标题分类在哪里?为什么在Firefox中连“标题分类”的复选框都看不到?
发表于 2010-2-12 16:51:56 | 显示全部楼层
Ping 应该可以吧!
 楼主| 发表于 2010-2-12 19:11:53 | 显示全部楼层
ping的确是可以判断网络是否连通,但无法区分出“电缆未插好”和“电缆接通却没有上网”的情况。下面的VB代码可以使用,但不知有没有Au3脚本的实现方法。
Private Sub Form_Load()

    Dim ljzt As String '连接状态
    Dim swzt As String '上网状态
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
    Set IPConfigSet = objWMIService.ExecQuery("Select   *   from   Win32_NetworkAdapterConfiguration   where   ipenabled=true")
    
    If IPConfigSet.Count = 1 Then
        swzt = "未上网"
    End If
    If IPConfigSet.Count = 2 Then
        swzt = "已上网"
    End If
    
    Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
    Set IPConfigSet = objWMIService.ExecQuery("Select   *   from   Win32_NetworkAdapter   where   NetConnectionStatus=2")
    If IPConfigSet.Count = 0 Then
        ljzt = "电缆断开"
    End If
    If IPConfigSet.Count = 1 Then
        ljzt = "电缆接通"
    End If
    
    MsgBox swzt
    MsgBox ljzt    

End Sub
发表于 2010-2-12 20:03:45 | 显示全部楼层
把LS的代码转成AU3就行了啊,但我不会,呵呵.
发表于 2010-2-12 21:24:45 | 显示全部楼层

Win32_NetworkAdapter

本帖最后由 lainline 于 2010-2-13 06:43 编辑

中文版打开  程序扩展工具
wmi Scriptomatic

wmi 名称空间 root\CIMV2


wmi 类 Win32_NetworkAdapter

出来一个代码 运行 注意NetConnectionStatus 再研究研究这些代码就行了
 楼主| 发表于 2010-2-13 09:08:55 | 显示全部楼层
回复 5# lainline
谢谢lainline,有时间好好研究一下这段代码!
发表于 2010-2-14 10:11:48 | 显示全部楼层
我也想知道因为我这段时间也要写这个东西
发表于 2010-2-15 19:32:34 | 显示全部楼层
本帖最后由 阿福 于 2010-2-15 20:03 编辑

;三楼的代码翻译成au3的:

$sState=""
$strComputer = "localhost"

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$IPConfigSet = $objWMIService.ExecQuery("Select   *   from   Win32_NetworkAdapterConfiguration   where   ipenabled=true")
Switch $IPConfigSet.Count
        Case 1
                $sState = "未上网"
        Case 3
                $sState = "未上网"
        Case 2
                $sState = "已上网"
        Case 4
                $sState = "已上网"
EndSwitch
MsgBox(0, "上网状态", $sState)


$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$IPConfigSet = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapter where NetConnectionStatus=2")
Switch $IPConfigSet.Count
        Case 0
                $sState = "电缆断开"
        Case 1
                $sState = "电缆接通"
EndSwitch
MsgBox(0, "连接状态", $sState)

评分

参与人数 2金钱 +10 贡献 +5 收起 理由
lynfr8 + 5
xym_xuyimin + 10 谢谢帮助

查看全部评分

发表于 2010-2-15 21:12:54 | 显示全部楼层
论坛的多着呢,建议dll和api,wmi容易被精简了
发表于 2010-2-16 07:33:25 | 显示全部楼层
谢谢阿福 ......
发表于 2010-2-16 09:45:40 | 显示全部楼层
学习到了,谢谢啊……
发表于 2010-2-16 17:11:01 | 显示全部楼层
试过了还是不行我运行后是未上网,电缆接通在看看
 楼主| 发表于 2010-2-17 12:10:46 | 显示全部楼层
谢谢各位!
 楼主| 发表于 2010-2-17 12:18:27 | 显示全部楼层
自己之前转化的Au3代码如下,不过比阿福的代码要繁琐很多。
MsgBox(0,"连接状态",WLZK_ljzt(),2)
MsgBox(0,"上网状态",WLZK_swzt(),2)

Func WLZK_ljzt()
        #region configure VB-Code ($VBCode)
                        Local $VBCode ='Public Function Rljzt()' & @CRLF
                        $VBCode &= 'Dim ljzt'& @CRLF
                        $VBCode &= ' strComputer = "."'& @CRLF
                        $VBCode &= ' Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")'& @CRLF
                        $VBCode &= ' Set IPConfigSet = objWMIService.ExecQuery("Select   *   from   Win32_NetworkAdapter   where   NetConnectionStatus=2")'& @CRLF
                        $VBCode &= ' If IPConfigSet.Count = 0 Then'& @CRLF
                        $VBCode &= '    ljzt = "电缆断开"'& @CRLF
                        $VBCode &= ' End If'& @CRLF
                        $VBCode &= ' If IPConfigSet.Count = 1 Then'& @CRLF
                         $VBCode &= '    ljzt = "电缆接通"'& @CRLF
                        $VBCode &= ' End If'& @CRLF
                        $VBCode &= 'Rljzt=ljzt'& @CRLF
                        $VBCode &= 'End Function' & @CRLF
                #endregion
        Local $vbscript = ObjCreate('ScriptControl')
        $vbscript.language='vbscript'
        $vbscript.addcode($VBCode)        
        Return $vbscript.run('Rljzt')
EndFunc

;获取网络状况并更新配置文件内的上网状态
Func WLZK_swzt()
        #region configure VB-Code ($VBCode)
                        Local $VBCode ='Public Function Rswzt()' & @CRLF
                        $VBCode &= 'Dim ljzt'& @CRLF
                        $VBCode &= ' Dim swzt'& @CRLF    
                        $VBCode &= ' strComputer = "."'& @CRLF
                        $VBCode &= ' Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")'& @CRLF
                        $VBCode &= ' Set IPConfigSet = objWMIService.ExecQuery("Select   *   from   Win32_NetworkAdapterConfiguration   where   ipenabled=true")'& @CRLF
                        $VBCode &= ' If IPConfigSet.Count = 1 Then'& @CRLF
                        $VBCode &= '    swzt = "未上网"'& @CRLF
                        $VBCode &= ' End If'& @CRLF
                        $VBCode &= ' If IPConfigSet.Count = 2 Then'& @CRLF
                        $VBCode &= '     swzt = "已上网"'& @CRLF
                        $VBCode &= ' End If'& @CRLF
                        $VBCode &= 'Rswzt=swzt'& @CRLF
                        $VBCode &= 'End Function' & @CRLF
                #endregion
        Local $vbscript = ObjCreate('ScriptControl')
        $vbscript.language='vbscript'
        $vbscript.addcode($VBCode)
         Return $vbscript.run('Rswzt')
EndFunc
非常感谢各位!
发表于 2010-2-17 23:40:35 | 显示全部楼层
嗯。 留个名。

谢谢大家。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-17 07:21 , Processed in 0.085078 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表