用asp实现ping(转载+翻译+原创)

Posted by Smeagol | Posted in Uncategorized | Posted on 20-06-2008

This article presents a simple way to ping an address and get the results of the ping using ASP. The idea was supplied by Bart Silverstein.

这是介绍用asp来ping一个域名并获得结果的文章。作者是:Bart Silverstein。

First, a .BAT file needs to be created that will be run from the Active Server Page. Let’s call this file DoPing.BAT. It will contain only one statement, which will ping a passed in IP address. Here is the code for DoPing.BAT:

首先,我们需要建立一个.bat文件,然后从asp里运行它。我们给这个bat文件取名为doping。它只包含一个语句。我们要传递2个参数给它。一个是域名,一个是输出结果的文件。下面就是这个文件的代码:
ping -a %1 > d:\\INetPub\\cgi-bin\\%2.txt

This will, if you can’t tell, ping the address passed in as the first command line argument (%1), and redirect the results to a text file named by the second command line argument (%2). Now, let’s look how we would call this from an ASP file:

这里有两个参数需要传递,一个是%1,他代表了ping的地址,可以是域名或者ip。一个是%2,代表了目标文件的文件名。现在我们来看看这个asp文件是怎么写的。

<%

Set FileSys = Server.CreateObject(“Scripting.FileSystemObject”)
FileName = FileSys.GetTempName

Set WShShell = Server.CreateObject(“WScript.Shell”)

IP = “204.123.54.1″ ‘ or whatever you want to ping
RetCode = WShShell.Run(“d:\\Inetpub\\cgi-bin\\DoPing.bat ” & IP & ” ” & FileName, 1, True)

if RetCode = 0 Then

‘There were no errors

else

Response.Redirect “PingErrors.htm”

end if

Set TextFile = FileSys.OpenTextFile(“d:\\InetPub\\cgi-bin\\” & FileName & “.txt”, 1)
TextBuffer = TextFile.ReadAll

For i = 1 to Len(TextBuffer)

If Mid(TextBuffer,i,1) = chr(13) Then

Response.Write(“<BR>”)

else

Response.Write(Mid(TextBuffer,i,1))

end if

Next

TextFile.Close

FileSys.DeleteFile “d:\\Inetpub\\cgi-bin\\” & FileName & “.txt”

%>
Before you go hog wild and implement this code or use similar techniques on your site, there are a few things you should be wary of. From a secutiry standpoint, this is really dangerous, for any time you let someone run an application on your server there is always the potential that it will come back to haunt you. One suggestion to lessen the threat: make a separate folder with no script or execute priviledges, and have your DoPing.bat output its results to that folder.

 当你要将这些代码插入你的网页时,有几个地方是需要注意的。一个是安全。wscript.shell是一个很危险的组件。还有一个建议就是,建一个特殊的文件夹,取消它的可执行权限。应用程序保护选”无”,让它无法执行脚本。然后将doping.bat的输入结果放在这个文件夹里。

I hope this article was informative an interesting. Happy Programming!

(翻译结束)

给出我修改后的程序。

这里要说的是,当asp调用cmd命令时,其执行的页面本身不会等待ping命令执行完成。他们是异步的。也就是说,如果ping的时候超时,需要一段时间ping命令才结束。而asp页面已经在读取.txt文件的内容了。但这时.txt还没有生成。这样就会出现错误。
而我要实现的是群ping的功能。我把程序改成两个页面分别执行。

一个是ping.asp:

<%
const ForReading = 1
dim f,fso
Set fso = server.CreateObject(“Scripting.FileSystemObject”)
Set f = fso.OpenTextFile(server.Mappath(“domain.txt”),ForReading)
alltext = f.readall
alltext=replace(alltext,chr(13),”")
dim myarr
myarr=split(alltext,chr(10))
Set WShShell = Server.CreateObject(“WScript.Shell”)
Set FileSys = Server.CreateObject(“Scripting.FileSystemObject”)
for i = 0 to ubound(myarr)
 ’ or whatever you want to ping
 RetCode = WShShell.Run(“D:\\ping\\doping.bat ” & “www.” & myarr(i) & ” ” & myarr(i))

 if RetCode = 0 Then

‘There were no errors
‘ response.write “运行成功”

 else

 Response.Redirect “PingErrors.htm”
 
 end if

next

response.write “运行完成”
%>
程序写的不是很好啊,呵呵,不过功能是有的。。。。

domain.txt文件里是要ping的域名

格式是要每个域名一行。就行。

执行这个页面时可以看到系统进程里多了很多的cmd.exe,等这些进程都消失,说明都ping完了。

我自己的机器一次只能ping 25个域名左右。ping多了会出现ping应用程序初始化失败。(高手帮忙解决啊~呵呵)

下面只要用get.asp读取刚才ping完生成的.txt文件就可以了

<%
const ForReading = 1
dim f,fso
Set fso = server.CreateObject(“Scripting.FileSystemObject”)
Set f = fso.OpenTextFile(server.Mappath(“domain.txt”),ForReading)
alltext = f.readall
alltext=replace(alltext,chr(13),”")
dim myarr
myarr=split(alltext,chr(10))
Set WShShell = Server.CreateObject(“WScript.Shell”)
Set FileSys = Server.CreateObject(“Scripting.FileSystemObject”)
for i = 0 to ubound(myarr)

 Set TextFile = FileSys.OpenTextFile(“d:\\ping\\” & myarr(i) & “.txt”, 1)

 TextBuffer = TextFile.ReadAll
 begin= instr(textbuffer,”[")
 if begin = false then
 response.write chr(13)
 textfile.close
 else
 iresule = Mid(textbuffer,begin+1,instr(textbuffer,"]“)-begin-1)
 TextFile.Close
 response.write iresule & chr(13)
 response.flush
 end if

next
%>

公司里有很多域名要ping,所以想了个土方法。呵呵。起码手工的部分减少了很多。不过还是要等待第一个文件把域名都ping完,这里需要点时间。如果程序能自动延时,自动判断是否ping完了。这样就更好了。可惜本人才疏学浅,只能写到这种地步了。

Post your comment