用ASP探测远程主机是否开机的三種方法

方法一: 使用組件來探測

Ping常用的属性与方法:  
  1.建立Asping组件:Ser   Pinger=Server.CreateObject("AspPing.conn")  
  其中Aspping.conn是此Ping组件在计算机上所注册的名称  
   
  2.RemoteHost   :设置所有测试的Ip地址或主机名称:  
  Pinger.RemoteHost="IP地址或主机名称"  
   
  3.PingCount:指定所有测试(Ping主机)的次数  
  Pinger.PingCount=所要测试的次数  
   
  4.Ping   :获得测试结果(返回的信息)  
  Pinger.ping  
   
  更多的语法可以在帮助中得到相关信息,这里不多说了。下面举个例子来说明吧!  
 
 
  
< html >   
  
< title > 我要Ping你! </ title >   
  
< body >   
  
<%   
  
// 创建ping对像  
  Set   Myping = Server.CreateObject( " AspPing.Conn " )  
   
  
// 设置要测试的网址  
  MyPing.RemoteHost = " www.1studio.net "     // 豪哥可别怪我哦.....  
   
  
// 设置所要测试的次数  
   
  Myping.pingCount
= 4     // 如果很多人Ping,设置的次数为:100000的时候,,嘿嘿........  
   
  
// 显示测试结果,获得相关信息.  
   
  Response.Write
" <pre> " &    MyPing.ping    & " </pre> "   
   
  
// 运行完成后记得释放对像  
   
  Set   Myping
= Nothing  
  
%>   
  
</ body >   
  
</ html >     

方法二: 用 WSH 中的Ping 命令

使用WSH呼叫系統的Ping指令,將Ping的結果寫入到一個純文字文件中去,再把純文字文件顯示到網頁中。具體做法如下:

建立一個.BAT文件(例:myPing.BAT),這個文件要在ASP中呼叫,文件程式碼如下:
ping -a %1 > d:/INetPub/cgi-bin/%2.txt

(%1)是將來要ping的地址, (%2)是儲存ping結果的文件. 以下是ASP的程式碼:

<  %
Set  FileSys  =  Server.CreateObject( " Scripting.FileSystemObject " )
FileName 
=  FileSys.GetTempName
Set  WShShell  =  Server.CreateObject( " WScript.Shell " )
IP 
=   " xxx.xxx.xxx.xxx "  ’你要ping的地址
RetCode 
=  WShShell.Run( " d:Inetpubcgi-binmyPing.bat  "   &  IP  &   "   "   &  FileName,  1 True )
if  RetCode  =   0   Then
’沒有錯誤
else
Response.Redirect 
" PingErrors.htm "
end   if
Set  TextFile  =  FileSys.OpenTextFile( " d:InetPubcgi-bin "   &  FileName  &   " .txt " 1 )
TextBuffer 
=  TextFile.ReadAll
For  i  =   1   to   Len (TextBuffer)
If   Mid (TextBuffer,i, 1 =   chr ( 13 Then
Response.Write(
"
"
)
else
Response.Write(
Mid (TextBuffer,i, 1 ))
end   if
Next
TextFile.Close
FileSys.DeleteFile 
" d:Inetpubcgi-bin "   &  FileName  &   " .txt "
>


方法三 : 用 WinSock 來探測
以下是连接失败的例子(等待时间较长),把RemotePort改成80是连接成功的例子。  
 


  
< !DOCTYPE   HTML    PUBLIC     " -//W3C//DTD   HTML   4.0   Transitional//EN " >   
  
< html >   
  
< head >   
  
< title > Winsock </ title >   
   
  
< SCRIPT   ID = clientEventHandlersJS   LANGUAGE = javascript >   
  
< ! --   
   
  
function    ws_Connect()   {  
  alert(
" 连接成功! " );  
  }  
   
  
function    ws_Error()   {  
  alert(
" 连接失败! " );  
  }  
   
  
//-->   
  
</ SCRIPT >   
  
< SCRIPT   LANGUAGE = javascript    FOR = ws   EVENT = Connect >   
  
< ! --   
    ws_Connect()  
  
//-->   
  
</ SCRIPT >   
  
< SCRIPT   LANGUAGE = javascript    FOR = ws   EVENT = Error >   
  
< ! --   
    ws_Error()  
  
//-->   
  
</ SCRIPT >   
  
</ head >   
  
< body >   
  
< OBJECT    id = " ws "    classid = " clsid:248DD896-BB45-11CF-9ABC-0080C7E7B78D "    VIEWASTEXT >   
  
< PARAM   NAME = " _ExtentX "    VALUE = " 741 " >   
  
< PARAM   NAME = " _ExtentY "    VALUE = " 741 " >   
  
< PARAM   NAME = " _Version "    VALUE = " 393216 " >   
  
< PARAM   NAME = " Protocol "    VALUE = " tcp/ip " >   
  
< PARAM   NAME = " RemoteHost "    VALUE = " www.263.net " >   
  
< PARAM   NAME = " RemotePort "    VALUE = " 81 " >   
  
< PARAM   NAME = " LocalPort "    VALUE = " 0 " >   
  
</ OBJECT >   
  
< script >   
  ws.Connect();  
  
</ script >   
  
</ body >   
  
</ html >   

        總結: 以上三種方法就安全性來考慮, 第三種最為合適
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值