Using Microsoft SQL Server 2000 and some ASP script, Mitch creates a function that will ping any remote server, digest and process the results, and then return a summary of that ping session through an ASP function.
Using ASP & SQL To Ping A Remote Server - Creating the stored procedure (Page 3 of 6 )
As mentioned above, our function will connect to the SQL server and execute the sp_PingServer stored-procedure. To create the procedure, fire up Query Analyser on the SQL Server, login, and enter the following commands into the query window:
CREATE PROC sp_PingServer
DECLARE @strCmd VARCHAR(60)
SELECT @strCmd = 'ping ' + @strServerIP
EXEC Master..xp_cmdShell @strCmd
NB: This example creates the stored procedure in the "MYDATABASE" database. You should change this to match the name of your database.
If all goes well, Query Analyser will respond with:
The command(s) completed successfully.
Our newly created stored procedure, sp_PingServer, simply calls an extended stored procedure, xp_cmdShell, which is part of the master database.
Extended stored procedures do not reside in the database like other stored procedures. Instead, they are part of a library of DLL’s. Xp_cmdShell is part of the xplog70.dll library, typically located in the \Program Files\Microsoft SQL Server\MSSQL\Binn directory of the SQL Server.
The xp_cmdShell extended procedure takes one parameter, which is a windows shell command to be executed locally on the server. Any command or program that can be executed in a normal DOS prompt window, such as xcopy, dir, mem, ftp, del, etc can be passed as this parameter.
When our stored procedure is ran, it executes the command and returns the results. Each line returned by the command is added to the result set as a new row. Here is an example of running the stored procedure:
If you open a new DOS prompt window and execute the ping command against localhost (i.e. “ping localhost”), then you will see that the results are exactly the same.
Now that we’ve covered the details of creating the stored procedure that will actually execute the ping command for us, let’s create the ASP function, which will do all of the processing work.