不要说我光说不练,VB PING IP的代码如下: 如果你只知道机器名,那你想办法把它转换成IP吧,如果有VB转换的代码,记得发我一份,TKS。Option Explicit Private Type ip_option_information TTl As Byte Tos As Byte Flags As Byte OptionsSize As Byte OptionsData As Long End TypePrivate Type icmp_echo_reply Address As Long Status As Long RoundTripTime As Long DataSize As Integer Reserved As Integer DataPointer As Long Options As ip_option_information Data As String * 250 End TypePrivate Declare Function IcmpCreateFile Lib "icmp.dll" () As Long Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long Private Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, ByVal DestinationAddress As Long, _ ByVal RequestData As String, ByVal RequestSize As Integer, _ RequestOptions As ip_option_information, ReplyBuffer As icmp_echo_reply, _ ByVal ReplySize As Long, ByVal TimeOut As Long) As LongPrivate Const WSADESCRIPTION_LEN = 256 Private Const WSASYSSTATUS_LEN = 256 Private Const WSADESCRIPTION_LEN_1 = WSADESCRIPTION_LEN + 1 Private Const WSASYSSTATUS_LEN_1 = WSASYSSTATUS_LEN + 1 Private Const SOCKET_ERROR = -1Type PingReturn Discriptionn As String Ping As Long Errors As Boolean End TypeDim CurIp As Long Dim CurIpDes As StringPrivate Type tagWSAData wVersion As Integer wHighVersion As Integer szDescription As String * WSADESCRIPTION_LEN_1 szSystemStatus As String * WSASYSSTATUS_LEN_1 iMaxSockets As Integer iMaxUdpDg As Integer lpVendorInfo As String * 200 End Type Private Declare Function WSAStartup Lib "wsock32" (ByVal wVersionRequested As Integer, lpWSAData As tagWSAData) As Integer Private Declare Function WSACleanup Lib "wsock32" () As Integer Sub main() Dim myIP$ Dim Ret As PingReturn Dim StrPing$
Ret = Pinger(20, Val(1000)) '此处1000ms是超时时长。 StrPing = Ret.Discriptionn MsgBox StrPing
End SubFunction Pinger(TTl As Long, TimeOut As Long) As PingReturn Dim hFile As Long Dim lRet As Long Dim lIPAddress As Long Dim strMessage As String Dim pOptions As ip_option_information Dim pReturn As icmp_echo_reply Dim iVal As Integer Dim lPingRet As Long Dim pWsaData As tagWSAData
CurIp = lAddress CurIpDes = strAddress End FunctionFunction ErrorCodeToDes(Code As Long) As String Dim Ret As String Select Case Code Case 0 Ret = "成功" Case 11001 Ret = "缓冲区太小" Case 11000 Ret = "Status base" Case 11002 Ret = "目标网络无法解析" Case 11003 Ret = "目标主机无法解析" Case 11004 Ret = "目标协议无法解析" Case 11005 Ret = "目标端口无法解析" Case 11006 Ret = "资源不存在" Case 11007 Ret = "参数错误" Case 11008 Ret = "hw 错误" Case 11009 Ret = "包太大" Case 11010 Ret = "超时" Case 11011 Ret = "错误的请求" Case 11012 Ret = "路由错误" Case 11013 Ret = "传输超时" Case 11014 Ret = "重联超时" Case 11015 Ret = "参数有误" Case 11016 Ret = "源已不可用" Case 11017 Ret = "参数太大" Case 11018 Ret = "目标错误" Case 11019 Ret = "地址已删除" Case 11020 Ret = "Spec mtu changed" Case 11021 Ret = "Mtu changed" Case 11022 Ret = "卸载" Case 11023 Ret = "地址已添加" Case 11050 Ret = "一般性错误" Case 11255 Ret = "Ip pending" Case Else Ret = "未知错误" End Select ErrorCodeToDes = Ret End Function
如果你只知道机器名,那你想办法把它转换成IP吧,如果有VB转换的代码,记得发我一份,TKS。Option Explicit
Private Type ip_option_information
TTl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End TypePrivate Type icmp_echo_reply
Address As Long
Status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
DataPointer As Long
Options As ip_option_information
Data As String * 250
End TypePrivate Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
Private Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, ByVal DestinationAddress As Long, _
ByVal RequestData As String, ByVal RequestSize As Integer, _
RequestOptions As ip_option_information, ReplyBuffer As icmp_echo_reply, _
ByVal ReplySize As Long, ByVal TimeOut As Long) As LongPrivate Const WSADESCRIPTION_LEN = 256
Private Const WSASYSSTATUS_LEN = 256
Private Const WSADESCRIPTION_LEN_1 = WSADESCRIPTION_LEN + 1
Private Const WSASYSSTATUS_LEN_1 = WSASYSSTATUS_LEN + 1
Private Const SOCKET_ERROR = -1Type PingReturn
Discriptionn As String
Ping As Long
Errors As Boolean
End TypeDim CurIp As Long
Dim CurIpDes As StringPrivate Type tagWSAData
wVersion As Integer
wHighVersion As Integer
szDescription As String * WSADESCRIPTION_LEN_1
szSystemStatus As String * WSASYSSTATUS_LEN_1
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As String * 200
End Type
Private Declare Function WSAStartup Lib "wsock32" (ByVal wVersionRequested As Integer, lpWSAData As tagWSAData) As Integer
Private Declare Function WSACleanup Lib "wsock32" () As Integer
Sub main()
Dim myIP$
Dim Ret As PingReturn
Dim StrPing$
'设置IP
myIP$ = "220.181.18.114" '百度的IP
Call SetIp(myIP$)
Ret = Pinger(20, Val(1000)) '此处1000ms是超时时长。
StrPing = Ret.Discriptionn
MsgBox StrPing
End SubFunction Pinger(TTl As Long, TimeOut As Long) As PingReturn
Dim hFile As Long
Dim lRet As Long
Dim lIPAddress As Long
Dim strMessage As String
Dim pOptions As ip_option_information
Dim pReturn As icmp_echo_reply
Dim iVal As Integer
Dim lPingRet As Long
Dim pWsaData As tagWSAData
strMessage = "Echo this string of data"
iVal = WSAStartup(&H101, pWsaData)
lIPAddress = CurIp
hFile = IcmpCreateFile()
pOptions.TTl = TTl
lRet = IcmpSendEcho(hFile, lIPAddress, strMessage, Len(strMessage), pOptions, pReturn, Len(pReturn), TimeOut) If lRet = 0 Then
Pinger.Discriptionn = "Ping失败: " & CurIpDes & " 错误代码 : " & ErrorCodeToDes(pReturn.Status)
Pinger.Errors = True
Pinger.Ping = TimeOut
Else
If pReturn.Status <> 0 Then
Pinger.Discriptionn = "Ping失败: " & CurIpDes & " 错误描述 : " & ErrorCodeToDes(pReturn.Status)
Pinger.Errors = True
Pinger.Ping = TimeOut
Else
Pinger.Discriptionn = "Ping成功: " & CurIpDes & " - 时长:" & pReturn.RoundTripTime & "毫秒."
Pinger.Ping = pReturn.RoundTripTime
End If
If pReturn.RoundTripTime > TimeOut Then
Pinger.Discriptionn = "Ping失败: " & CurIpDes & " 错误代码 : " & ErrorCodeToDes(11010)
Pinger.Errors = True
Pinger.Ping = TimeOut
End If
End If lRet = IcmpCloseHandle(hFile)
iVal = WSACleanup()
End FunctionFunction SetIp(strAddress As String) Dim strTemp As String
Dim lAddress As Long
Dim iValCount As Integer
Dim lDotValues(1 To 4) As String
strTemp = strAddress
iValCount = 0
While InStr(strTemp, ".") > 0
iValCount = iValCount + 1
lDotValues(iValCount) = Mid(strTemp, 1, InStr(strTemp, ".") - 1)
strTemp = Mid(strTemp, InStr(strTemp, ".") + 1)
Wend
iValCount = iValCount + 1
lDotValues(iValCount) = strTemp
If iValCount <> 4 Then
CurIp = 0
Exit Function
End If
lAddress = Val("&H" & Right("00" & Hex(lDotValues(4)), 2) & _
Right("00" & Hex(lDotValues(3)), 2) & _
Right("00" & Hex(lDotValues(2)), 2) & _
Right("00" & Hex(lDotValues(1)), 2))
CurIp = lAddress
CurIpDes = strAddress
End FunctionFunction ErrorCodeToDes(Code As Long) As String
Dim Ret As String
Select Case Code
Case 0
Ret = "成功"
Case 11001
Ret = "缓冲区太小"
Case 11000
Ret = "Status base"
Case 11002
Ret = "目标网络无法解析"
Case 11003
Ret = "目标主机无法解析"
Case 11004
Ret = "目标协议无法解析"
Case 11005
Ret = "目标端口无法解析"
Case 11006
Ret = "资源不存在"
Case 11007
Ret = "参数错误"
Case 11008
Ret = "hw 错误"
Case 11009
Ret = "包太大"
Case 11010
Ret = "超时"
Case 11011
Ret = "错误的请求"
Case 11012
Ret = "路由错误"
Case 11013
Ret = "传输超时"
Case 11014
Ret = "重联超时"
Case 11015
Ret = "参数有误"
Case 11016
Ret = "源已不可用"
Case 11017
Ret = "参数太大"
Case 11018
Ret = "目标错误"
Case 11019
Ret = "地址已删除"
Case 11020
Ret = "Spec mtu changed"
Case 11021
Ret = "Mtu changed"
Case 11022
Ret = "卸载"
Case 11023
Ret = "地址已添加"
Case 11050
Ret = "一般性错误"
Case 11255
Ret = "Ip pending"
Case Else
Ret = "未知错误"
End Select
ErrorCodeToDes = Ret
End Function
然后读文件,搜索字符串~如果有timed out,就说明断了~~~此方法有一定误差~~
但是够简单啊~~~呵呵
就算这样又怎么判断是死机了还是根本就没开机呢?
判断机器状态比较复杂,ping只是判断它有没有和你连网,如果它断开网络连接你也没办法知道它有没有死机,甚至它连网了但装个天网之类的软件拒绝你ping你也没办法确认它是否死机
其实机器死机了有时候网卡正常工作的话ping还是通的