Jump to content
Sign in to follow this  
buymeapc

Monitor Network Cable Removal?

Recommended Posts

buymeapc

Is there a way I could monitor the NIC to see if the network cable has been unplugged and then plugged back in again? I am currently doing an 'ipconfig' and sending the results to a txt file then reading that file to see if the cable's been unplugged.

Just want a better (less sloppy) way...

Thanks

Jason

Share this post


Link to post
Share on other sites
weaponx

Why don't you just ping your router at like 10 second intervals?

Share this post


Link to post
Share on other sites
buymeapc

I need to monitor the physical unplugging/plugging of a cable on the PC. Unfortunately, a ping won't give me the results I'm looking for.

Thanks

Share this post


Link to post
Share on other sites
ptrex
buymeapc

Ok, that sounds cool, can you please give me an example?

Thanks

Share this post


Link to post
Share on other sites
weaponx

Any time I query Win32_NetworkAdapter, the Status value is blank for all network cards.

What do you mean a ping won't give the results you are looking for? If a ping to your router fails then the media is most likely disconnected.

Share this post


Link to post
Share on other sites
buymeapc

If a ping fails, it could mean that the computer is using dhcp with no dhcp server set on the router or is statically set for the incorrect ip and/or gateway which means a ping would fail resulting in a false positive. Unfortunately, I need to look for the physical unplugging of the cable.

I basically need to show a gui when someone yanks the network cable and then hide the gui when they plug it back in.

Thanks

Jason

Edited by buymeapc

Share this post


Link to post
Share on other sites
ptrex

@buymeapc

Ofcourse

$strComputer = "." 
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") 
 $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter",Default,48) 
 
 For $objItem in $colItems 
    ConsoleWrite(@LF & @LF)
    consolewrite ("-----------------------------------"&@CR)
    consolewrite ("Win32_NetworkAdapter instance"&@CR)
    consolewrite ("-----------------------------------"&@CR)
    Consolewrite ("Description: " & $objItem.Description&@CR)
    Consolewrite ("AdapterType: " & $objItem.AdapterType&@CR)
    Consolewrite ("AdapterTypeId: " & $objItem.AdapterTypeId&@CR)
    
    Switch $objItem.NetConnectionStatus
        Case 0 
            $strStatus = "Disconnected"
        Case 1 
            $strStatus = "Connecting" 
        Case 2 
            $strStatus = "Connected" 
        Case 3 
            $strStatus = "Disconnecting" 
        Case 4 
            $strStatus = "Hardware not present" 
        Case 5 
            $strStatus = "Hardware disabled" 
        Case 6 
            $strStatus = "Hardware malfunction" 
        Case 7 
            $strStatus = "Media disconnected" 
        Case 8 
            $strStatus = "Authenticating" 
        Case 9 
            $strStatus = "Authentication succeeded" 
        Case 10 
            $strStatus = "Authentication failed" 
        Case 11 
            $strStatus = "Invalid address" 
        Case 12 
            $strStatus = "Credentials required"
    EndSwitch 
    ConsoleWrite("Net Connection Status: " & $strStatus & @LF) 
Next

regards

ptrex

Share this post


Link to post
Share on other sites
Nahuel

This kind of things always fail with Windows 2000 :P

Description: 3Com EtherLink XL 10/100 PCI TX NIC (3C905B-TX)
AdapterType: 
D:\Scripts\AutoIt Tray\Nuevo Script.au3 (12) : ==> The requested action with this object has failed.: 
Consolewrite ("AdapterTypeId: " & $objItem.AdapterTypeId&@CR) 
Consolewrite ("AdapterTypeId: " & $objItem.AdapterTypeId^ ERROR

Share this post


Link to post
Share on other sites
buymeapc

@ptrex Thank you! That's very handy. Now, I just have to find a way to see which in the list is the primary network adapter. :P

Thanks

Jason

Share this post


Link to post
Share on other sites
weaponx

From Microsoft Technet: http://www.microsoft.com/technet/scriptcen...05/hey0321.mspx

Detect if cable is plugged in:

$strComputer = "."

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\wmi")
$colMonitoredEvents = $objWMIService.ExecNotificationQuery("Select * from MSNdis_StatusMediaConnect") 

While True 
    $strLatestEvent = $colMonitoredEvents.NextEvent 
    MsgBox(0,"","A network connection has been made: " & $strLatestEvent.InstanceName)
WEndoÝ÷ Ù°Þµç-÷nW¢²ééè yßÛjëh×6$strComputer = "."

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\wmi")
$colMonitoredEvents = $objWMIService.ExecNotificationQuery("Select * from MSNdis_StatusMediaDisconnect") 

While True 
    $strLatestEvent = $colMonitoredEvents.NextEvent 
    MsgBox(0,"","A network connection has been made: " & $strLatestEvent.InstanceName)
WEnd
Edited by weaponx

Share this post


Link to post
Share on other sites
buymeapc

I created a version of what you gave me ptrex and it seems to be taking up quite a bit of resources - 82% CPU. I need to constantly check the adapter connection, but at the same time, I cannot tax the CPU. Is there something I'm doing wrong?

Here's the code I used:

CODE
$strComputer = "."

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter",Default,48)

While 1

For $objItem in $colItems

If StringInStr($objItem.NetConnectionID, "Local", 1) Then

Switch $objItem.NetConnectionStatus

Case 0

$strStatus = "Disconnected"

Case 1

$strStatus = "Connecting"

Case 2

$strStatus = "Connected"

Case 3

$strStatus = "Disconnecting"

Case 4

$strStatus = "Hardware not present"

Case 5

$strStatus = "Hardware disabled"

Case 6

$strStatus = "Hardware malfunction"

Case 7

$strStatus = "Media disconnected"

Case 8

$strStatus = "Authenticating"

Case 9

$strStatus = "Authentication succeeded"

Case 10

$strStatus = "Authentication failed"

Case 11

$strStatus = "Invalid address"

Case 12

$strStatus = "Credentials required"

EndSwitch

TrayTip($objItem.NetConnectionID, $strStatus, 1)

;ToolTip($strStatus, 0, 0, $objItem.NetConnectionID)

;MsgBox(0, $objItem.NetConnectionID, $strStatus)

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter",Default,48)

ExitLoop

EndIf

Next

WEnd

Thanks

Jason

Share this post


Link to post
Share on other sites
weaponx

Just put a Sleep(10) above WEnd

Share this post


Link to post
Share on other sites
SkinnyWhiteGuy

Not to throw too much at you, but this will let you do it as events, so you can watch for it to go down/up while doing something else.

;WMI monitoring

Dim $strComputer, $SINK1, $SINK2, $objWMIService
$strComputer = "127.0.0.1"
$SINK1 = ObjCreate("WbemScripting.SWbemSink")
$SINK2 = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($SINK1, "SINK_")
ObjEvent($SINK2, "SINK_")
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\wmi")
If Not @error Then
    $objWMIService.ExecNotificationQueryAsync ($SINK1, "SELECT * FROM MSNdis_StatusMediaConnect")
    $objWMIService.ExecNotificationQueryAsync ($SINK2, "SELECT * FROM MSNdis_StatusMediaDisconnect")
EndIf
ConsoleWrite("In monitoring mode. Press Ctrl+C to exit." & @CRLF)
While 1
    Sleep(10000)
WEnd
;******************************************************************************
Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    ;Trap asynchronous events.
    Switch $objLatestEvent.Path_.Class()
        Case "MSNdis_StatusMediaDisconnect"
            ConsoleWrite("A network connection has been lost: " & $objLatestEvent.InstanceName & @CRLF)
        Case "MSNdis_StatusMediaConnect"
            ConsoleWrite("A network connection has been made: " & $objLatestEvent.InstanceName & @CRLF)
    EndSwitch
EndFunc   ;==>SINK_OnObjectReady

I coded that to specifically look for both connecting and disconnecting. If you only need disconnecting, you can simplify it a lot by removing all the duplicate sink stuff and just use disconnect.

Share this post


Link to post
Share on other sites
buymeapc

@warriorx: I'm sorry, but adding the Sleep(10) to the While loop did not solve the eating of CPU time.

@skinny: I like the code you posted. But, I'm curious about the Sleep(10000) in the While loop. Does this affect the ability to catch the disconnect the moment it happens? That's what I'm really after; catch the disconnect as it happens and catch the reconnect as it happens.

Thanks

Jason

Share this post


Link to post
Share on other sites
SkinnyWhiteGuy

Nah, the Sleep(10000) is just there for it to have something to continuously do, and not eat up a lot of CPU (something you seem to be looking for). As soon as the cable is unplugged/plugged, the event triggers. As far as that sleep is concerned, you could put Sleep(10) in there, 10000 was just in the example I based that script from. That's the beauty of Asynchronous events: they fire as you need them, whenever you need them.

Share this post


Link to post
Share on other sites
Darktan

Sorry for the old bump, but I'm curious about something dealing with this.

I stumbled upon this thread looking for a similar tool. I'm writing a specialized server information utility and I need to check if there's a network cable plugged into all the NIC's on the server, both the on board NIC's and technically the team (but that's separate). I'm just starting to delve into WMI (and finding it damn handy) so I'm still learning. Can you query individual adapter? I assume you can and will probably find the answer shortly. Now to figure out if there's a cable in the DRAC's too

Peace

--DT

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×