Using ASP & SQL To Ping A Remote Server - The GetPingStats() ASP function
(Page 4 of 6 )
Create a new ASP page in a web-accessible directory on your web server. Name it ping.asp.
Ping.asp will contain two functions: The first, ShowForm() will display a standard HTML form with a text field and a submit button. When a host name is entered into the text field, clicking on the "Ping Server" button will submit the form back to the same page, ping.asp.
When the form is submitted, a hidden value, getIP, is set to true. When getIP is true, another function, DoPing() is called. The DoPing() function takes one parameter, strHost, which is the value retrieved from the form. It returns a string of text that is the value returned from the GetPingStats function:
function DoPing(strHost)
On Error Resume Next
dim objConn
dim objRS
set objConn = Server.CreateObject("ADODB.Connection")
set objRS = Server.CreateObject("ADODB.Recordset")
objConn.Open "Provider=SQLOLEDB; Data Source = 127.0.0.1; Initial Catalog = MYDATABASE; User Id = sa; Password = ;"
objRS.ActiveConnection = objConn
DoPing = GetPingStats(objRS, strHost)
end functionSimple, right? Good. The GetPingStats function takes two parameters: a recordset object, which should already have its ActiveConnection property set to an ADODB.Connection object, and a string variable, strHost.
As mentioned earlier, the sp_PingServer stored procedure that we created on our SQL Server is called as the only parameter for the recordsets open method:
function GetPingStats(rsObject, strHost)
...
if rsObject.State = 1 then rsObject.Close
rsObject.Open "sp_PingServer '" & strHost & "'"If the ping was successful, 13 rows will be returned. If the host name was invalid, 1 line will be returned. Some basic error handling will work out whether or not the host name was valid:
if Instr(1, rsObject.Fields(0).value, "Unknown host") > 0 then 'Bad Address
GetPingStats = "<b>" & strHost & "</b>: Bad Host Name"
exit function
else 'The address is ok, get the ping details
...If the host name is valid, however, the second row returned will contain the resolved I.P. address of the host:
Pinging localhost [127.0.0.1] with 32 bytes of dataWe extract this value into a variable, strIP, like this:
strIP = Mid(rsObject.Fields(0).value, Instr(1, rsObject.Fields(0).value, "[") + 1, Instr(1, rsObject.Fields(0).value, "]") - (Instr(1, rsObject.Fields(0).value, "[") + 1))Now that we have the resolved I.P. address, we will analyse the four ping attempts that the ping command executed.
Next: Analyzing the ping attempts >>
More ASP Articles
More By Mitchell Harper