Check valid IP Address

I am making a program that needs to check for a valid IP address… Ie will do a ping on a device and check that it exists… if not it will error.

How would be the best way to do this

Hi Chris,

There are several ways to check if a public IP address is “alive” (I mean, announced in “Internet”), but doing a “ping” against it doesn’t always warranties that; for example, currently is very usual that some routers and other internet hardware (firewalls) simply doesn’t respond to these packets…

If this is just for checking against devices on a managed LAN that you are sure ping packets go through… then, consider using the Shell class for that.

Javier

Thanks.

Basically I am fairly new to this. What I have done is written an App that will go and pull some SNMP Information about a device,

I have a TextField where the IP Can be entered, but it does not exist the app just returns nothing, and basically I want it to say that the IP address does not exisit

This is what I have

[code]dim s as new NetSNMPMBS

s.Community = CommunityTextField.text
s.IP = IPADDRESSTextField.text

dim r as string = s.Query(“system.sysName.0”)

r = Mid(s.Query(“system.sysName.0”),8)

SystemNameTextField.Text = r

[/code]

I have tried to use this

 Dim z As String
z = System.Network.LookupIPAddress("s.IP")
If z <> "" Then
  MsgBox(z)
Else
  MsgBox("An error occurred")
End If

But that gives me the public IP Address, and what i want is the local address.

Does this make sense ?

AFAIK, Well… it depends what you’re querying against. If you’re running your app to get the answer from a remote device, then probably you’ll find some kind of router/switch at the other end that, if it has SNMP enabled, returns the requested info… but probably not the information from the internal devices connected to it (rotuer/switch). It mainly depends how the SNMP has been configured, and if all the devices from the LAN/Sub-network are added as nodes to the SNMP service.

I mean, under SNMP you have a SNMP manager (maybe an independent computer running other management systems) that is querying against all the SNMP Agents (these have to be enabled) in order to get/set information on them; these can be routers, switches, printers, computers…

If everything is correctly configured, it should be possible to access the local IP address walking the MIB. But I doubt this is usually publicly exposed to the WAN side.

Are you trying to implement and SNMP manager?

I found this video that explains really well all the components involved, if you’re interested on it.

Javier

Javier thank you for your information.

Basically at this point I am trying to query the router itself for SNMP. SO I have a text field where the IP Address can be entered, and then it finds information about it. So for example if I enter 192.168.1.254 (which is the router) I will get the SysName back and so I know the actual code is working. But If I enter 192.168.1.200 (which is not assigned to anything) i just get blank SysName.

So really waht i want is a way to say if the IP is not valid (or unknown) then to get a warning saying " Unknown IP Address"

Thanks

Chris

Ah, ok. On UNIX systems you can do a simple trick:

ping 192.168.1.255 //this is the last ip for the subnet
arp -a

The last command gives you all the entries of available nodes with their respective IP addresses.

Probably you can try this and parse the result from a Shell instance. Do this will work for you?

Javier

Javier thanks. Yes I know all about Ping and Arp -a … but how do I parse that information and get it into my app. Thats where i am struggling, is how to get that information into my app

Best means of detection is to actually attempt to connect. It’s the only way to know. For example, if the device is behind a firewall, you may get a response from the ping, but the firewall may not let the connection through. Or the reverse, the connection would be allowed, but the firewall does not respond to pings. There’s too many variables to try to predict, so you’re better off just attempting a connection to the address and port combination.

Try this:

[code]Dim ns As New Shell

ns.Execute(“ping -c 2 192.168.1.255”)

ns.Execute(“arp -a”)

Dim items() As String = ns.Result.Split(EndOfLine)
Dim ips() As String
Dim rightSide As String

For Each item As String In items

rightSide = NthField(item,"(",2)

ips.Append NthField(rightSide,")",1)

Next[/code]

The ips array will contain all the detected (responding) IPs on that subnetwork.

Javier

This works in all my projects:

[code]Function PublicIP() As string
// Obtain public IP
// Attention: do this within a separate thread since it takes a roundtrip to get ip

	  Dim pubip As String
	  Dim sck As New HTTPSocket
	  
	  Try
	    
	    // Dim js As New JSONItem(sck.Get("http://ifconfig.co/json", 10))
	    Dim js As New JSONItem(sck.Get("http://ipinfo.io/json", 10))
	    If js.hasname("ip") Then pubip = js.value("ip")   
	    
	  Catch RuntimeException
	    pubip = ""
	  End Try
	  
	  If sck <> Nil Then sck = Nil
	  Return pubip
	  
	End Function[/code]