| View previous topic :: View next topic |
| Author |
Message |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Sun Jun 28, 2009 6:37 pm Post subject: Detect local network? |
|
|
I'm trying to determine if a windows computer is connected in one or both scenarios here. First, if there is a local network available, second if there is a connection to the internet.
I have the testing of the internet connection taken care of, but have not figured out how to detect a local network. I scoured the forum search, but haven't found anything yet.
Does anyone know if there is a registry entry, or a windows command line that would tell me if my computer is connected to a local network only?
Thanks,
~Garrett _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Sun Jun 28, 2009 6:56 pm Post subject: |
|
|
Ok, found DrDread's dll.
I'm looking at checking DNS, DHCP and GATEWAY.
Which of these three would reliably prove whether a computer is currently connected to a network or not?
Right now, I'm checking all three to see if they are null or not and on my current setup all 3 are null if I am not connected to my home network. Would I or should I check all 3 of these to see if there's any connection, or would merely one of them suffice? _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
DaveR Valued Contributor


Joined: 03 Sep 2005 Posts: 413 Location: Australia
|
Posted: Mon Jun 29, 2009 10:47 am Post subject: |
|
|
| Garrett wrote: | I'm looking at checking DNS, DHCP and GATEWAY.
Which of these three would reliably prove whether a computer is currently connected to a network or not? |
None of those. I have my network settings manually configured (DHCP disabled) and when I run the dreadnet.dll's demo script I get with my network cable unplugged I get:
- Networked: Yes
- DNS: 192.168.1.1
- DHCP: 192.168.1.1
- Gateways: 192.168.1.1
So you can see that checking DNS, DHCP and GATEWAY is useless if the user is not using DHCP.
I noticed that when connected to the network, both 'Local IP' and 'Get IPs' in the demo are the same 192.168.1.2. But when offline, 'Get IPs' was 127.0.0.1 and 'Local IP' was my hard coded IP address of 192.168.1.2.
But when I tested it again, both were 127.0.0.1. This broke my idea of using the following: | Code: | if @greater(@pos(@Dnet(GetIP),@Dnet(GetIPs),0))
info online
else
warn offline
end
| And it would not have worked for users who get their IP settings via DHCP.
I suppose you could just check if either @Dnet(GetIP) or @Dnet(GetIPs) equal 127.0.0.1 then the PC is offline.
EDIT: In fact after testing it, it works really well. | Code: | if @equal(@Dnet(GetIP),127.0.0.1) @equal(@Dnet(GetIPs),127.0.0.1)
warn offline
else
info online
end
|
Nice. I've been looking for a reliable way to absolutely determine if a PC was connected to a local network (even if it did not have Internet access). _________________ cheers
Dave |
|
| Back to top |
|
 |
DaveR Valued Contributor


Joined: 03 Sep 2005 Posts: 413 Location: Australia
|
Posted: Mon Jun 29, 2009 11:03 am Post subject: |
|
|
| DaveŽ wrote: | | Garrett wrote: | I'm looking at checking DNS, DHCP and GATEWAY.
Which of these three would reliably prove whether a computer is currently connected to a network or not? |
None of those. I have my network settings manually configured (DHCP disabled) and when I run the dreadnet.dll's demo script I get with my network cable unplugged I get:
- Networked: Yes
- DNS: 192.168.1.1
- DHCP: 192.168.1.1
- Gateways: 192.168.1.1 |
Hmm, after more testing, I have noticed that I did not wait long enough after pulling the Ethernet cable out. After 30 seconds DNS returns null (even though I have manually set a DNS server IP). So using DNS would seem to be a good solution for testing if the PC is connected to a local network. Just be aware that for the first 20 or 30 seconds after losing the network connection DNS still returns the previous DNS IP. _________________ cheers
Dave |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Mon Jun 29, 2009 2:01 pm Post subject: |
|
|
Right, I noticed the delay in the refreshing of the data also when I was testing.
Here's the code I am currently using to detect network and internet:
| Code: | IF @equal(%%CommandOne,INTERNET)@equal(%%CommandOne,WWW)
WRITELINE CONSOLE,
IF @file(http://www.microsoft.com/)
WRITELINE CONSOLE,@tab()Internet detected.
ELSE
WRITELINE CONSOLE,@tab()Internet *not* detected.
END
END
IF @equal(%%CommandOne,NETWORK)@equal(%%CommandOne,NET)
WRITELINE CONSOLE,
EXTERNAL @path(%0)dreadnet.dll
#DEFINE FUNCTION,Dnet
%A = @Dnet(Servers,DNS)
%B = @Dnet(Servers,DHCP)
%C = @Dnet(Servers,Gateways)
IF @null(%A)@null(%B)@null(%C)
WRITELINE CONSOLE,@tab()Network *not* detected.
ELSE
WRITELINE CONSOLE,@tab()Network detected.
END
END |
The first, checking the internet is VDS 6 only, will not work on VDS 5, the second uses DrDread's dll. I haven't tried this on any other versions of Windows yet, but sure hope it works on 95 and on up. I'm using the code on Vista.
Sorry about the code too... I use eConsole glued to my desktop and love using it, so was making a little console program that checks these things.
BTW, Dread's dll always said I was "Networked" even if I was not networked. This is why I tried the DHCP, DNS and GATEWAY. _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
Aslan Valued Contributor


Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Mon Jun 29, 2009 2:59 pm Post subject: |
|
|
Garrett,
You could try checking if the user is logged on locally or on a domain.
| Code: | %%Logonserver = @env(Logonserver)
%%ComputerName = @env(ComputerName)
If @equal(%%Logonserver,%%ComputerName)
info Local Logon
else
info Domain Logon
end |
|
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Mon Jun 29, 2009 9:53 pm Post subject: |
|
|
| Aslan wrote: | Garrett,
You could try checking if the user is logged on locally or on a domain.
| Code: | %%Logonserver = @env(Logonserver)
%%ComputerName = @env(ComputerName)
If @equal(%%Logonserver,%%ComputerName)
info Local Logon
else
info Domain Logon
end |
|
Does not work for me on Vista. When I turned off the network hub, your code did not reflect the disconnected network. But the code I used from Dread's DLL did reflect the disconnected network it.
Mind you, it would've been great to have a solution to this that was pure VDS itself. I always try to accomplish what I can without the use of externals. So far though, it seems that an external is the only reliable way to detect the network status. _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
Rubes_sw Valued Contributor


Joined: 11 Jun 2001 Posts: 625 Location: Northern Ireland
|
Posted: Mon Jun 29, 2009 10:19 pm Post subject: |
|
|
Hi G
Did you try:
using ipconfig /all under CMD in windows ?
You can search the returned data @pipe()
for
Ethernet adapter Local Area Connection:
Media State ............ Media disconnected
Im sure you can tidy this code up.....
| Quote: | RUN ipconfig /all,PIPE,WAIT 1
%P = @PIPE()
list create,1
list assign,1,%P
if @match(1,Media disconnected)
INFO No Local Area Connection!
else
INFO We've got a connection!
end
list close,1 |
Nathan |
|
| Back to top |
|
 |
Aslan Valued Contributor


Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Mon Jun 29, 2009 11:27 pm Post subject: |
|
|
| Garrett wrote: |
Does not work for me on Vista. When I turned off the network hub, your code did not reflect the disconnected network. But the code I used from Dread's DLL did reflect the disconnected network it.
|
I was trying to solve your question as to being on a network or not only. You are correct that my code won't give you the media state. I meant for it to be used along with Dnet.dll.
For connection status you could try pinging a common ip or the gateway.
As for Rubes code above, the wait option with pipe is unneccessary and you may want to check all adapters (especially on Vista) because you might have a multiple adapter configuration. |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Tue Jun 30, 2009 12:24 am Post subject: |
|
|
| Aslan wrote: | | Garrett wrote: |
Does not work for me on Vista. When I turned off the network hub, your code did not reflect the disconnected network. But the code I used from Dread's DLL did reflect the disconnected network it.
|
I was trying to solve your question as to being on a network or not only. You are correct that my code won't give you the media state. I meant for it to be used along with Dnet.dll. |
Well heck, I already have that ability, but was merely trying to find out if I needed to check all of those, or just one. You're trying to add more code to confuse me old brain now :p _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Tue Jun 30, 2009 12:34 am Post subject: |
|
|
| Rubes_sw wrote: | Hi G
Did you try:
using ipconfig /all under CMD in windows ?
You can search the returned data @pipe()
for
Ethernet adapter Local Area Connection:
Media State ............ Media disconnected
Im sure you can tidy this code up..... |
Now this.. This! This might just be what we need here!
When connected the "Media State" doesn't exists, but when the network is not there, that entry does show up under the local connection.
| Code: | Ethernet adapter Local Area Connection:
Media State . . . . . . . . . . . : Media disconnected |
But of course as mentioned by Aslan, there are several adapters showing... not that I actually have physical adapters though, just that Vista seems to create these virtual adapters above and beyond the one single physical adapter I do have.
I'm worried now that if someone does actually have two physical adapters, that if we merely check the Local connection that we may still have another connection working if the local is not connected. Is that even possible? To have 2 physical adapters operating and both connected to the same network, or different networks? _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
Aslan Valued Contributor


Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Tue Jun 30, 2009 2:26 am Post subject: |
|
|
Ummm, I have two physical adapters (both active) on my Vista machine.
You will need to loop through the output of IPCONFIG and check each adapter.
BTW - you may want to ignore virtual adapters with the seemingly high usage of virtual machines now-a-days. One issue though might be that the user may try to use your app on a virtual machine... hehehe
I would be more apt to do a ping test to check connectivity. (For VDS 5.x users)
| Code: | List create,2
runh ping www.google.com -n 1,pipe,wait
list assign,2,@pipe()
if @greater(@count(2),0)
list seek,2,0
end
if @not(@match(2,"Request timed out")) @not(@match(2,"Ping request could not find host"))
info Connected to Internet
else
info No Internet Connection
end |
Last edited by Aslan on Tue Jun 30, 2009 3:58 am; edited 1 time in total |
|
| Back to top |
|
 |
Aslan Valued Contributor


Joined: 31 May 2001 Posts: 589 Location: Memphis, TN USA
|
Posted: Tue Jun 30, 2009 3:57 am Post subject: |
|
|
| Garrett wrote: | Mind you, it would've been great to have a solution to this that was pure VDS itself. I always try to accomplish what I can without the use of externals. So far though, it seems that an external is the only reliable way to detect the network status.
|
Here is some pure VDS net info code for your old brain Gleen from it what you need.
| Code: | List create,1
#Lets get the GUID for an active adapter
runh net config workstation,pipe
list assign,1,@pipe()
if @greater(@count(1),0)
list seek,1,0
end
If @match(1,NetBT_Tcpip_)
%%Adapter_GUID = @trim(@item(1))
If %%Adapter_GUID
option fieldsep,_
parse ";;%%AGUID", %%Adapter_GUID
option fieldsep,@chr(32)
parse "%%Adapter_GUID;", %%AGUID
end
end
If %%Adapter_GUID
# Now we can get most of the adapter info from the registry using it's GUID
# For static info
%%IpAddress = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,IpAddress)
%%SubnetMask = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,SubnetMask)
%%DefaultGateway = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,DefaultGateway)
%%NameServer = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,NameServer)
# For DHCP info
%%DhcpIpAddress = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,DhcpIpAddress)
%%DhcpSubnetMask = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,DhcpSubnetMask)
%%DhcpDefaultGateway = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,DhcpDefaultGateway)
%%DhcpNameServer = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,DhcpNameServer)
%%DhcpServer = @regread(HLM,System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\%%Adapter_GUID,DhcpServer)
end
# Check internet connection (VDS 5.x or higher)
List create,2
runh ping www.microsoft.com -n 1,pipe,wait
list assign,2,@pipe()
if @greater(@count(2),0)
list seek,2,0
end
if @not(@match(2,"Request timed out")) @not(@match(2,"Ping request could not find host"))
%%InfoParams = Connected to Internet@cr()@cr()
else
%%InfoParams = No Internet Connection@cr()@cr()
end
# Check internet connection (VDS 6.x or higher)
# If @file(http://www.microsoft.com)
# %%InfoParams = Connected to Internet@cr()@cr()
# else
# %%InfoParams = No Internet Connection@cr()@cr()
# end
# This last part is just to build the info dialog data
If %%IpAddress
%%InfoParams = IpAddress: @tab()@tab()@trim(%%IpAddress)
end
If %%SubnetMask
%%InfoParams = %%InfoParams@cr()SubnetMask: @tab()@tab()@trim(%%SubnetMask)
end
If %%DefaultGateway
%%InfoParams = %%InfoParams@cr()DefaultGateway: @tab()@tab()@trim(%%DefaultGateway)
end
If %%NameServer
%%InfoParams = %%InfoParams@cr()NameServer: @tab()@tab()@trim(%%NameServer)
end
If %%DhcpIpAddress
%%InfoParams = %%InfoParams@cr()DhcpIpAddress: @tab()@tab()@trim(%%DhcpIpAddress)
end
If %%DhcpDefaultGateway
%%InfoParams = %%InfoParams@cr()DhcpDefaultGateway: @tab()@trim(%%DhcpDefaultGateway)
end
If %%DhcpSubnetMask
%%InfoParams = %%InfoParams@cr()DhcpSubnetMask: @tab()@tab()@trim(%%DhcpSubnetMask)
end
If %%DhcpNameServer
%%InfoParams = %%InfoParams@cr()DhcpNameServer: @tab()@tab()@trim(%%DhcpNameServer)
end
If %%DhcpServer
%%InfoParams = %%InfoParams@cr()DhcpServer: @tab()@tab()@trim(%%DhcpServer)
end
info %%InfoParams
exit |
|
|
| Back to top |
|
 |
DaveR Valued Contributor


Joined: 03 Sep 2005 Posts: 413 Location: Australia
|
Posted: Tue Jun 30, 2009 10:40 am Post subject: |
|
|
For me, checking registry keys has proven unreliable.
From previous experience, the only reliable way to check if a PC is online in VDS is to ping something that is known to be on the network. The only problem with pinging is that each failed ping takes up to 5 seconds. Pinging just once is not 100% reliable... but 2 failed pings while offline takes 10 seconds!
Piping ipconfig holds promise... as long as pinging is not also required.
I have also found that the only reliable way to determine if a server is online is to download a small file (VDS 6 makes this easy).
I often use the InternetGetConnectedState API function to check if the PC has internet access. And I use the InternetGetConnectionA API function to check if a specific IP:Port combo is online. Or use a single ping to check if a specific IP is alive. Here's my function that I use:
| Code: | :IsOnline
#-------------------------------------------------
# DreadNet.dll check if PC is on local network:
# SYNTAX %x = @IsOnline(Local)
#
# API check if a specified IP:Port is online:
# SYNTAX %x = @IsOnline(<IP>,<port>)
#
# Ping to check if a specified IP is online:
# SYNTAX %x = @IsOnline(<url/IP>)
#
# API check if this PC is online:
# SYNTAX %x = @IsOnline()
#
# Returns 1 if online
#------------------------------------------------
%x =
if @equal(%1,Local)
# Check if this PC is on Local Network
if @equal(@Dnet(GetIP),127.0.0.1) @equal(@Dnet(GetIPs),127.0.0.1)
else
# online
%x = 1
end
elsif @both(%1,%2)
# Very slow if IP is not online (15 seconds!)
# Check if specified IP:Port is online
loadlib wininet.dll
if @ok()
# STR: requires 'http://' and trailing '/'
%x = @lib(WININET,InternetCheckConnectionA,BOOL:,STR:http://%1:%2/,INT:1,INT:0)
freelib wininet.dll
end
elsif %1
# Ping to check if specified IP is online
# Pinging is slow if IP is not online (5 seconds)
runh cmd /c ping -n 1 %1 ,pipe
if @ok()
%p = @pipe()
if @greater(@pos(Lost = 0,%p),0)
# online
%x = 1
end
end
else
# Check if this PC is connected to Internet
loadlib wininet.dll
if @ok()
%x = @lib(WININET,InternetGetConnectedState,BOOL:,INT:,INT:0)
freelib wininet.dll
end
end
exit %x |
I too prefer not to use 3rd party libraries if possible, but for speed and reliability I have not found a better way of checking if a PC is connected to a local network than:
| Code: | if @equal(@Dnet(GetIP),127.0.0.1) @equal(@Dnet(GetIPs),127.0.0.1)
# offline
%x =
else
# online
%x = 1
end |
_________________ cheers
Dave |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Tue Jun 30, 2009 1:14 pm Post subject: |
|
|
Well, @equal(@Dnet(GetIP),127.0.0.1) @equal(@Dnet(GetIPs),127.0.0.1) is the current reliable solution then. _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|