buymeapc Posted November 16, 2007 Share Posted November 16, 2007 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 Link to comment Share on other sites More sharing options...
weaponx Posted November 16, 2007 Share Posted November 16, 2007 Why don't you just ping your router at like 10 second intervals? Link to comment Share on other sites More sharing options...
buymeapc Posted November 16, 2007 Author Share Posted November 16, 2007 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 Link to comment Share on other sites More sharing options...
ptrex Posted November 16, 2007 Share Posted November 16, 2007 @all You can use WMI to check the ADAPTOR status. When the cable is unplugged that status changes Regards ptrex Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New Link to comment Share on other sites More sharing options...
buymeapc Posted November 16, 2007 Author Share Posted November 16, 2007 Ok, that sounds cool, can you please give me an example? Thanks Link to comment Share on other sites More sharing options...
weaponx Posted November 16, 2007 Share Posted November 16, 2007 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. Link to comment Share on other sites More sharing options...
buymeapc Posted November 16, 2007 Author Share Posted November 16, 2007 (edited) 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 November 16, 2007 by buymeapc Link to comment Share on other sites More sharing options...
ptrex Posted November 16, 2007 Share Posted November 16, 2007 @buymeapc Ofcourse expandcollapse popup$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 Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New Link to comment Share on other sites More sharing options...
Nahuel Posted November 16, 2007 Share Posted November 16, 2007 This kind of things always fail with Windows 2000 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 Link to comment Share on other sites More sharing options...
buymeapc Posted November 16, 2007 Author Share Posted November 16, 2007 @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. Thanks Jason Link to comment Share on other sites More sharing options...
weaponx Posted November 16, 2007 Share Posted November 16, 2007 (edited) From Microsoft Technet: http://www.microsoft.com/technet/scriptcen...05/hey0321.mspxDetect 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 November 16, 2007 by weaponx Link to comment Share on other sites More sharing options...
buymeapc Posted November 16, 2007 Author Share Posted November 16, 2007 @weaponx: Thanks Link to comment Share on other sites More sharing options...
buymeapc Posted November 16, 2007 Author Share Posted November 16, 2007 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 Link to comment Share on other sites More sharing options...
weaponx Posted November 16, 2007 Share Posted November 16, 2007 Just put a Sleep(10) above WEnd Link to comment Share on other sites More sharing options...
SkinnyWhiteGuy Posted November 16, 2007 Share Posted November 16, 2007 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. Link to comment Share on other sites More sharing options...
buymeapc Posted November 16, 2007 Author Share Posted November 16, 2007 @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.ThanksJason Link to comment Share on other sites More sharing options...
SkinnyWhiteGuy Posted November 16, 2007 Share Posted November 16, 2007 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. Link to comment Share on other sites More sharing options...
buymeapc Posted November 16, 2007 Author Share Posted November 16, 2007 Very nice, thank you very much, Skinny! Link to comment Share on other sites More sharing options...
Darktan Posted March 16, 2009 Share Posted March 16, 2009 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now