Network cable unplugged

G

Greg Brown

I need to be able to determine if the network cable is unplugged. I've been
through various news groups and the MS API, and cannot find anything. I
have a headless device, so I have network setup happening on a non-desktop
interface. I need to be able to determine the difference between
000.000.000.000 as not set up yet, and 000.000.000.000 as no cable plugged
in. DHCP may or may not be present (one possible user configuration would
be two users connected with a crossover cable and manual IP's).

Thanks,

Greg
 
S

Sean Gahan

Greg,
I would try a script something like this:

do
strComputer = "."
Set objWMIService = GetObject _
("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled =
True")
For Each objAdapter in colAdapters
If Not IsNull(objAdapter.IPAddress) Then
For i = LBound(objAdapter.IPAddress) To
UBound(objAdapter.IPAddress)
if objAdapter.IPAddress(i) = "" then
Wscript.Echo "Dude: you network cable is unplugged." & vbcrlf & _
"Description: " & objAdapter.Description & vbcrlf & _
"IP address: " & objAdapter.IPAddress(i)
End if
Next
End If
next
Wscript.Sleep 1000
loop

Regards,

Sean Gahan
 
D

Doug Hoeffel

Sean:

How about this:

Function GetLinkStatus()

Dim queryString
Set WMILinkInterface = WMIInterface.ConnectServer(".", "Root\wmi", "", "")

queryString = "select * from MSNdis_MediaConnectStatus where InstanceName =
'"
queryString = queryString & top.netAdapterName & "'"

Set mediaCollection = WMILinkInterface.ExecQuery(queryString)

For Each ObjItem In mediaCollection
GetLinkStatus = ObjItem.NdisMediaConnectStatus
Next
End Function

where top.netAdapterName would need to be set for your NIC adapter name.

HTH... Doug
 
G

Greg Brown

Thanks for the responses...

I am enumerating thru GetAdaptersInfo(), but that wasn't telling me anything
except that the IP was not set. Using the index member returned from
GetAdaptersInfo() in GetIfEntry() will give you dwOperStatus something other
than MIB_IF_OPER_STATUS_OPERATIONAL when the cable is unplugged.
 
K

KM

Greg,

Could be.. I've heard of some problems with dwOperStatus return from GetIfEntry.

You can also check out WinDDK code under \src\setup\enable. This may be of some help to you.
 
L

Lasse

Greg said:
I need to be able to determine if the network cable is unplugged. I've been
through various news groups and the MS API, and cannot find anything. I
have a headless device, so I have network setup happening on a non-desktop
interface. I need to be able to determine the difference between
000.000.000.000 as not set up yet, and 000.000.000.000 as no cable plugged
in. DHCP may or may not be present (one possible user configuration would
be two users connected with a crossover cable and manual IP's).

Thanks,

Greg
Write a C program and ...
Use API: GetIfTable(pIfTable, &Size, FALSE);
Then look at: pIfTable->table.dwOperStatus
 
S

Sean Gahan

Doug,
I wonder how many different ways this can be done. How about one more:

do
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c ping " &
IPConfig.IPAddress(i) )
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
If Instr(strText, "Reply") > 0 Then
Wscript.Echo "Reply received from " & IPConfig.IPAddress(i)
Exit Do
End If
Loop
Next
End If
Next
Wscript.Sleep 1000
loop

Regards,

Sean Gahan
 
D

Doug Hoeffel

cool... go old ping ;-)

Sean Gahan said:
Doug,
I wonder how many different ways this can be done. How about one more:

do
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c ping " &
IPConfig.IPAddress(i) )
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
If Instr(strText, "Reply") > 0 Then
Wscript.Echo "Reply received from " & IPConfig.IPAddress(i)
Exit Do
End If
Loop
Next
End If
Next
Wscript.Sleep 1000
loop

Regards,

Sean Gahan



InstanceName vbcrlf anything.
 
S

Slobodan Brcin \(eMVP\)

Ok so far we had API, WMI, how about going low level to NDIS?

IOCTL_NDIS_QUERY_GLOBAL_STATS
OID_GEN_MEDIA_CONNECT_STATUS

Best regards,
Slobodan
 
K

KM

Or just enumerating adapter and interface info in registry. :)
Btw, really easy to implement (I've done that once for setting IP address.. don't remember the reason, though)
 
S

Slobodan Brcin \(eMVP\)

Right :)

This could be done trough SetupApi functions if we want to play nicely and not to parse registry directly.

Regards,
Slobodan
 
K

KM

Yup.
A good start would be the mentioned WinDDK \src\setup\enable sample. The sample uses (wraps up) SetupDi API.
 
S

Slobodan Brcin \(eMVP\)

Hi Sean,

If you want only to detect if network adapter interface is up and running then the most easier and lighter approach would be the one
suggested by Konstantin in thread above regarding the registry read.

Regarding the lines for method I suggested. The core can be written in four lines, but obtaining the right filename for network
adapter is little trickier.

If would be something like:

unsigned long OID=OID_GEN_MEDIA_CONNECT_STATUS, res=0;
HANDLE h=CreateFile(DeviceFileName, .......
DeviceIOControl(h, IOCTL_NDIS_QUERY_GLOBAL_STATS, &OID, 4, &res, 4, ...
CloseHandle(h);

For the more appropriate info read:
http://msdn.microsoft.com/library/d..._5b1aa4cf-0df5-4a2c-bba6-7ca35bcc99cf.xml.asp
And follow surrounding pages for more info about various query types for statistics, etc.

BTW: There might be errors in code that I have posted above I have not tested it from user mode.

Best regards,
Slobodan

PS: It always ends up with using SetupApi functions :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top