1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
| program WMIPing;
002
003
{$APPTYPE CONSOLE}
004
005
uses
006
SysUtils,
007
ActiveX,
008
ComObj,
009
Variants;
010
011
function GetStatusCodeStr(statusCode:integer) : string;
012
begin
013
case statusCode of
014
0 : Result:='Success';
015
11001 : Result:='Buffer Too Small';
016
11002 : Result:='Destination Net Unreachable';
017
11003 : Result:='Destination Host Unreachable';
018
11004 : Result:='Destination Protocol Unreachable';
019
11005 : Result:='Destination Port Unreachable';
020
11006 : Result:='No Resources';
021
11007 : Result:='Bad Option';
022
11008 : Result:='Hardware Error';
023
11009 : Result:='Packet Too Big';
024
11010 : Result:='Request Timed Out';
025
11011 : Result:='Bad Request';
026
11012 : Result:='Bad Route';
027
11013 : Result:='TimeToLive Expired Transit';
028
11014 : Result:='TimeToLive Expired Reassembly';
029
11015 : Result:='Parameter Problem';
030
11016 : Result:='Source Quench';
031
11017 : Result:='Option Too Big';
032
11018 : Result:='Bad Destination';
033
11032 : Result:='Negotiating IPSEC';
034
11050 : Result:='General Failure'
035
else
036
result:='Unknow';
037
end;
038
end;
039
040
041
//The form of the Address parameter can be either the computer name (wxyz1234), IPv4 address (192.168.177.124), or IPv6 address (2010:836B:4179::836B:4179).
042
procedure Ping(const Address:string;Retries,BufferSize:Word);
043
var
044
FSWbemLocator : OLEVariant;
045
FWMIService : OLEVariant;
046
FWbemObjectSet: OLEVariant;
047
FWbemObject : OLEVariant;
048
oEnum : IEnumvariant;
049
iValue : LongWord;
050
i : Integer;
051
052
PacketsReceived : Integer;
053
Minimum : Integer;
054
Maximum : Integer;
055
Average : Integer;
056
begin;
057
PacketsReceived:=0;
058
Minimum :=0;
059
Maximum :=0;
060
Average :=0;
061
Writeln('');
062
Writeln(Format('Pinging %s with %d bytes of data:',[Address,BufferSize]));
063
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
064
FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
065
//FWMIService := FSWbemLocator.ConnectServer('192.168.52.130', 'root\CIMV2', 'user', 'password');
066
for i := 0 to Retries-1 do
067
begin
068
FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_PingStatus where Address=%s AND BufferSize=%d',[QuotedStr(Address),BufferSize]),'WQL',0);
069
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
070
if oEnum.Next(1, FWbemObject, iValue) = 0 then
071
begin
072
if FWbemObject.StatusCode=0 then
073
begin
074
if FWbemObject.ResponseTime>0 then
075
Writeln(Format('Reply from %s: bytes=%s time=%sms TTL=%s',[FWbemObject.ProtocolAddress,FWbemObject.ReplySize,FWbemObject.ResponseTime,FWbemObject.TimeToLive]))
076
else
077
Writeln(Format('Reply from %s: bytes=%s time=<1ms TTL=%s',[FWbemObject.ProtocolAddress,FWbemObject.ReplySize,FWbemObject.TimeToLive]));
078
079
Inc(PacketsReceived);
080
081
if FWbemObject.ResponseTime>Maximum then
082
Maximum:=FWbemObject.ResponseTime;
083
084
if Minimum=0 then
085
Minimum:=Maximum;
086
087
if FWbemObject.ResponseTime<Minimum then
088
Minimum:=FWbemObject.ResponseTime;
089
090
Average:=Average+FWbemObject.ResponseTime;
091
end
092
else
093
if not VarIsNull(FWbemObject.StatusCode) then
094
Writeln(Format('Reply from %s: %s',[FWbemObject.ProtocolAddress,GetStatusCodeStr(FWbemObject.StatusCode)]))
095
else
096
Writeln(Format('Reply from %s: %s',[Address,'Error processing request']));
097
end;
098
FWbemObject:=Unassigned;
099
FWbemObjectSet:=Unassigned;
100
//Sleep(500);
101
end;
102
103
Writeln('');
104
Writeln(Format('Ping statistics for %s:',[Address]));
105
Writeln(Format(' Packets: Sent = %d, Received = %d, Lost = %d (%d%% loss),',[Retries,PacketsReceived,Retries-PacketsReceived,Round((Retries-PacketsReceived)*100/Retries)]));
106
if PacketsReceived>0 then
107
begin
108
Writeln('Approximate round trip times in milli-seconds:');
109
Writeln(Format(' Minimum = %dms, Maximum = %dms, Average = %dms',[Minimum,Maximum,Round(Average/PacketsReceived)]));
110
end;
111
end;
112
113
114
begin
115
try
116
CoInitialize(nil);
117
try
118
//Ping('192.168.52.130',4,32);
119
Ping('theroadtodelphi.wordpress.com',4,32);
120
finally
121
CoUninitialize;
122
end;
123
except
124
on E:Exception do
125
Writeln(E.Classname, ':', E.Message);
126
end;
127
Readln;
128
end. |
Partager