Status einer Netzwerk-Verbindung auslesen

  • folgendes problem: ich hänge mit WLAN an meinem Router, nun möchte ich nach dem Windowsstart den Status meiner WLAN-Verbindung auslesen, damit ich weiß, ob die Verbindung schon steht und dementsprechend andere Programme geöffnet werden können...
    ich bräuchte jetzt nur noch eine Methode, um den Status der Verbindung auslesen zu können

    Danke schon im Vorraus

    • Offizieller Beitrag

    Hi,

    versuch es mal so:

    [autoit]

    If _getWlanStatus() Then
    MsgBox(64, 'info', 'online!')
    Else
    MsgBox(64, 'info', 'offline!')
    EndIf

    [/autoit][autoit][/autoit][autoit]

    Func _getWlanStatus()
    Local Const $STDOUT_CHILD = 2
    Local Const $STDERR_CHILD = 4
    Local $text = '', $command = 'ipconfig /all"'
    Local $stream = Run(@ComSpec & ' /c ' & $command, '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
    $text &= @CR & StdoutRead($stream)
    If @error Then ExitLoop
    WEnd
    Local $re = StringRegExp($text, '(?<=Medienstatus\. \. \. \. \. \. \. \. \. \. \. : ).*', 3)
    If @error Then Return -1
    If StringInStr($re[0], 'keine Verbindung') Then Return 0
    Return 1
    EndFunc ;==>_getWlanStatus

    [/autoit]

    Mega

    • Offizieller Beitrag

    Hallo

    Kleiner fehler in Xenos Funktion:
    If @error Then Return -1

    Also wenn @error dann gibt er -1 zurück. -1 ist aber seit neuerem True (-1 = True) also wenn ein Error kommt sagt er, dass die Verbindung online ist ;)
    So sagt er bescheid wenn ein Error zurückkommt:

    Spoiler anzeigen
    [autoit]

    Switch _getWlanStatus()
    Case 1
    MsgBox(0,"","online")
    case 0
    MsgBox(0,"","off")
    Case Else
    MsgBox(16,"","Fehler")
    EndSwitch

    [/autoit] [autoit][/autoit] [autoit]

    Func _getWlanStatus()
    Local Const $STDOUT_CHILD = 2
    Local Const $STDERR_CHILD = 4
    Local $text = '', $command = 'ipconfig /all"'
    Local $stream = Run(@ComSpec & ' /c ' & $command, '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
    $text &= @CR & StdoutRead($stream)
    If @error Then ExitLoop
    WEnd
    Local $re = StringRegExp($text, '(?<=Medienstatus\. \. \. \. \. \. \. \. \. \. \. : ).*', 3)
    If @error Then Return -1
    If StringInStr($re[0], 'keine Verbindung') Then Return 0
    Return 1
    EndFunc ;==>_getWlanStatus

    [/autoit]

    Mfg Spider

  • Nabend,

    da ich die Frage ganz interessant fand, hab ich mal versucht eine allgemeine Lösung zu basteln auf WMI Basis:

    Spoiler anzeigen
    [autoit][/autoit] [autoit][/autoit] [autoit]

    MsgBox(0,"",GetNetStatus("LAN-Verbindung"))

    [/autoit] [autoit][/autoit] [autoit]

    Func GetNetStatus($Device)
    #cs
    Device = Name so wie er in Netzwerkverbindungen angezeigt wird, z.B. "Drathtlose Neztwerkverbindung")
    Rückgabewerte:
    1 = online
    0 = offline
    -1 = Konnte Netzwerkkarte nicht finden
    -2 = Konnte IP nicht auslesen
    #ce


    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $colItems = ""
    Local $strComputer = "localhost"
    Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT Index FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & $Device & "'", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    Local $index = -1

    For $objItem In $colItems
    $index = $objItem.Index
    Next

    If $index = -1 Then Return -1

    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & $index, "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    Local $ip = -1

    For $objItem In $colItems
    $ip = $objItem.IPAddress(0)
    Next

    If $ip = -1 Then Return -2

    If $ip = "0" Or $ip = "0.0.0.0" Then
    Return 0
    Else
    Return 1
    EndIf
    EndFunc

    [/autoit]


    und da ich grade dabei war, eine grafische Anzeige für alle Netzwerkverbindungen:

    Spoiler anzeigen
    [autoit]

    #include <GUIConstants.au3>
    #include <array.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    $output = ""
    $strComputer = "localhost"

    [/autoit] [autoit][/autoit] [autoit]

    Dim $Caption[1]
    $Caption[0] = 0

    [/autoit] [autoit][/autoit] [autoit]

    Dim $Index[1]
    $Index[0] = 0

    [/autoit] [autoit][/autoit] [autoit]

    Dim $IP[1]
    $IP[0] = 0

    [/autoit] [autoit][/autoit] [autoit]

    Dim $Status[1]
    $Status[0] = 0

    [/autoit] [autoit][/autoit] [autoit]

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT Description,Index FROM Win32_NetworkAdapter WHERE NetConnectionStatus is not null", "WQL", _
    $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    [/autoit] [autoit][/autoit] [autoit]

    For $objItem In $colItems
    _ArrayAdd($Caption,$objItem.Description)
    $Caption[0] = $Caption[0] + 1

    _ArrayAdd($Index,$objItem.Index)
    $Index[0] = $Index[0] + 1
    Next

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $count = $Caption[0]

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 to $count
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & $Index[$i], "WQL", _
    $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    For $objItem In $colItems
    _ArrayAdd($IP,$objItem.IPAddress(0))
    $IP[0] = $IP[0] + 1
    Next
    Next

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $minus = 0

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 to $count

    Select
    Case $IP[$i] = '0'
    $minus += 1
    _ArrayAdd($Status,"loos")
    Case $IP[$i] = '0.0.0.0'
    _ArrayAdd($Status,"offline")
    Case Else
    _ArrayAdd($Status,"online")
    EndSelect
    Next

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    $minus = 25 * $minus
    $hoehe = 50 + 25 * $count
    $hoehe -= $minus
    #Region ### START Koda GUI section ### Form=
    $frmMain = GUICreate("Netzwerkstatus", 650, $hoehe, 443, 224, BitOR($WS_SYSMENU,$WS_CAPTION,$WS_POPUP,$WS_POPUPWINDOW,$WS_BORDER,$WS_CLIPSIBLINGS))
    $Label1 = GUICtrlCreateLabel("Bezeichnung", 232, 8, 81, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    $Label2 = GUICtrlCreateLabel("IP-Adresse", 88, 8, 71, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    $Label3 = GUICtrlCreateLabel("Status", 8, 8, 41, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    [/autoit] [autoit][/autoit] [autoit]

    $hoehe = 35
    For $i = 1 to $count
    If $IP[$i] <> '0' Then
    GUICtrlCreateLabel($Caption[$i], 232, $hoehe, 418, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel($IP[$i], 88, $hoehe, 71, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel($Status[$i], 8, $hoehe, 41, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    If $Status[$i] = "online" Then GUICtrlSetColor(-1,"0x00ff00") ;grün
    If $Status[$i] = "offline" Then GUICtrlSetColor(-1,"0xff0000") ;rot
    $hoehe += 25
    EndIf
    Next

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit

    [/autoit] [autoit][/autoit] [autoit]

    EndSwitch
    WEnd

    [/autoit]

    Mfg
    Jens (McPoldy)

    Twitter: jkroeger

    Denn die Dinge, die wir erst lernen müssen, bevor wir sie tun, lernen wir beim Tun.(Aristoteles)