VFP: Find IP by name

VfpNameLookup

For my network checks I want to ping some computers to see if they’re still active on my network. But before that, I have to lookup the IP numbers for the network names. Found this TekTips article on NSlookup from VFP6 and built my own function around the example.

* Lookup an IP number connected to a netwerk name
* (C)2011 Jasper de Graaf for J.A. Software
* Source: https://degraafonline.com 
* Inspiration from http://www.tek-tips.com/viewthread.cfm?qid=891380&page=338 

LPARAMETERS tcLookup && name to lookup

* No parameter, wrong type or empty 
IF PCOUNT()=0 OR !VARTYPE(tcLookup)=[C] OR EMPTY(tcLookup)
 ? [nslookup needs character parameter lookup network name or ip number]
 RETURN []
ENDIF 

* create scripting shell object and test
loShell = CreateObject("WScript.Shell")
IF VARTYPE(loShell)<>[O]
 ? [did not succeed in creating Wscript.Shell object]
 RETURN []
ENDIF 

* perform lookup and get results
loScriptExec = loShell.Exec("nslookup -debug "+tcLookup)
lcNsLookup = loScriptExec.StdOut.ReadAll()

IF AT([ANSWERS],lcNsLookup,2)>0 && we got a valid answer!
 lnIPpos = AT([Address:],lcNsLookup,2)+8
 lcNsIp = ALLTRIM(SUBSTR(lcNsLookup,lnIPpos))
ELSE
 lcNsIp = [] && could not resolve network name
ENDIF 

loScriptExec = null
loShell = null 
RETURN lcNsIp

VFP: Ping

Visual Foxpro: Ping an IP number and return weither it has responded. Uses Windows scripting shell.

* Ping function
* (C)2011 Jasper de Graaf for J.A. Software
* Source: https://degraafonline.com
LPARAMETERS tcIpNumber && IP number to ping

* No parameter, wrong type or empty 
IF PCOUNT()=0 OR !VARTYPE(tcIpNumber)=[C] OR EMPTY(tcIpNumber)
 ? [ping needs character parameter ip number (ie: "192.168.1.254")]
 RETURN []
ENDIF 

* create scripting shell object and test
loShell = CreateObject("WScript.Shell")
IF VARTYPE(loShell)<>[O]
 ? [did not succeed in creating Wscript.Shell object]
 RETURN []
ENDIF 

* perform short single ping and get results
loScriptExec = loShell.Exec("ping -n 1 -w 2 "+tcIpNumber)
lcPing = loScriptExec.StdOut.ReadAll()
llPing = ![time-out]$LOWER(lcPing)

loScriptExec = null
loShell = null 
RETURN llPing