-- Insert PING Data Into Temp Table
INSERT INTO #temp1
EXEC master..xp_cmdshell @Sql
SET @t1 = (SELECT top 1 t from #temp1 order by t DESC)
IF @T1 = ('Request timed out.'
BEGIN
RAISERROR(@Err, 16, 1)
RETURN(-1)
END
IF (@@ERROR = 0)
BEGIN
-- Sends Email if An Email is Desired.
IF (@DesireEmail = 1)
BEGIN
EXEC xp_sendmail @Recipient = 'YourRecipient',
@Subject = 'YourSubject',
@query = 'SELECT TOP 1 SUBSTRING(t, 12, CHARINDEX('':'', t, 12)-12) from #temp1 ORDER BY t DESC'
END
-- Simply Returns The IP Address of the Server
SELECT TOP 1 SUBSTRING(t, 12, (CHARINDEX(':', t, 12)-12)) FROM #temp1 ORDER BY t DESC
END