📄 route.dpr
字号:
else
WriteLn(SysErrorMessage(GetLastError));
end;
end;
//------------------------------------------------------------------------------
// Delete a row from the routing table
function DeleteIpEntry(const Destination, GateWay: string): boolean;
var
ForwardTable: PMibIpForwardTable;
ForwardRow: TMibIpForwardRow;
Dest: string;
Gate: string;
Mask: string;
Size: ULONG;
I: Integer;
begin
Size := 0;
Result:=False;
if not GetIpForwardTable(nil, Size, True) = ERROR_BUFFER_OVERFLOW then Exit;
ForwardTable := AllocMem(Size);
try
if GetIpForwardTable(ForwardTable, Size, True) = ERROR_SUCCESS then
begin
for I := 0 to ForwardTable^.dwNumEntries - 1 do
begin
ForwardRow := ForwardTable^.Table[I];
Dest := IpAddrToString(ForwardRow.dwForwardDest);
Gate := IpAddrToString(ForwardRow.dwForwardNextHop);
if ((Destination = '') or (Pos(Destination, Dest) > 0)) and
((GateWay = '') or (Pos(GateWay, Dest) > 0)) then
begin
Result := True;
Mask := IpAddrToString(ForwardRow.dwForwardMask);
DeleteIpForwardEntry(ForwardRow);
DeletePersistentRoute(Dest + ',' + Mask + ',' + Gate + ',' + IntToStr(ForwardRow.dwForwardMetric1));
end;
end;
end;
finally
FreeMem(ForwardTable);
end;
end;
//------------------------------------------------------------------------------
procedure ModifyIpEntry(aDestination, aMask, aGateway, aMetric, aIfNo: string;
aPersistent: boolean);
var
ForwardTable: PMibIpForwardTable;
ForwardRow: TMibIpForwardRow;
Dest: string;
Gate: string;
Mask: string;
Size: ULONG;
I: Integer;
Found: boolean;
begin
Size := 0;
Found := False;
if not GetIpForwardTable(nil, Size, True) = ERROR_BUFFER_OVERFLOW then Exit;
ForwardTable := AllocMem(Size);
try
if GetIpForwardTable(ForwardTable, Size, True) = ERROR_SUCCESS then
for I := 0 to ForwardTable^.dwNumEntries - 1 do
begin
ForwardRow := ForwardTable^.Table[I];
Dest := IpAddrToString(ForwardRow.dwForwardDest);
if (Pos(aDestination, Dest) > 0) then
begin
Mask := IpAddrToString(ForwardRow.dwForwardMask);
Gate := IpAddrToString(ForwardRow.dwForwardNextHop);
Found := True;
DeleteIpForwardEntry(ForwardRow);
DeletePersistentRoute(Dest + ',' + Mask + ',' + Gate + ',' + IntToStr(ForwardRow.dwForwardMetric1));
break;
end;
end;
finally
FreeMem(ForwardTable);
end;
if Found then
CreateIpEntry(Dest, aMask, aGateway, aMetric, aIfNo, aPersistent)
else
WriteLn(sRouteNotFound);
end;
//------------------------------------------------------------------------------
// How is this program to be used by the end user?
procedure Usage;
begin
WriteLn;
WriteLn('Manipulates network routing tables.');
WriteLn;
WriteLn('ROUTE [-f] [-p] [command [destination]');
WriteLn(' [MASK netmask] [gateway] [METRIC metric] [IF interface]');
WriteLn;
WriteLn(' -f Clears the routing tables of all gateway entries. If this is');
WriteLn(' used in conjunction with one of the commands, the tables are');
WriteLn(' cleared prior to running the command.');
WriteLn(' -p When used with the ADD command, makes a route persistent across');
WriteLn(' boots of the system. By default, routes are not preserved');
WriteLn(' when the system is restarted. ');
WriteLn(' Ignored for all other commands, which always affect the appropriate');
WriteLn(' persistent routes.');
WriteLn(' command One of these:');
WriteLn(' PRINT Prints a route');
WriteLn(' ADD Adds a route');
WriteLn(' DELETE Deletes a route');
WriteLn(' CHANGE Modifies an existing route');
WriteLn(' destination Specifies the host.');
WriteLn(' MASK Specifies that the next parameter is the ''netmask'' value.');
WriteLn(' netmask Specifies a subnet mask value for this route entry.');
WriteLn(' If not specified, it defaults to 255.255.255.255.');
WriteLn(' gateway Specifies gateway.');
WriteLn(' interface the interface number for the specified route.');
WriteLn('Displays protocol statistics and current TCP/IP connections.');
WriteLn(' METRIC specifies the metric, ie. cost for the destination.');
WriteLn;
WriteLn('Diagnostic Notes:');
WriteLn(' Invalid MASK generates an error, that is when (DEST & MASK) != DEST.');
WriteLn(' Example> route ADD 157.0.0.0 MASK 155.0.0.0 157.55.80.1 IF 1');
WriteLn(' The route addition failed: 87');
WriteLn;
WriteLn('Examples:');
WriteLn;
WriteLn(' > route PRINT');
WriteLn(' > route ADD 157.0.0.0 MASK 255.0.0.0 157.55.80.1 METRIC 3 IF 2');
WriteLn(' destination^ ^mask ^gateway metric^ ^');
WriteLn(' Interface^');
WriteLn(' If IF is not given, it tries to find the best interface for a given');
WriteLn(' gateway.');
WriteLn(' > route PRINT');
WriteLn(' > route PRINT 157* .... Only prints those matching 157*');
WriteLn(' > route DELETE 157.0.0.0');
WriteLn(' > route PRINT');
WriteLn;
end;
//------------------------------------------------------------------------------
var
ParamIndexOffset: Integer; // Used to offset access to ParamStr in case -p is used
ClearTable: Boolean; // -f switch Clear routing table before operation;
Persistent: Boolean; // -p makes or displays persistent routes
PrintTable: Boolean; // print routing table
AddRoute: Boolean;
DeleteRoute: Boolean;
ModifyRoute: Boolean;
Destination: string; // Destination parameter;
S: string;
Mask, GateWay, Metric: string;
IfNo: string; // Number of the interface (input parameter)
begin
WriteLn('');
WriteLn('Windows 2000&NT Route');
WriteLn('Copyright (C) 2000 Vladimir Vassiliev');
WriteLn('');
if FindCmdLineSwitch('?', ['/', '-'], True) then
begin
Usage;
Exit;
end;
ParamIndexOffset := 0;
ClearTable := False;
if FindCmdLineSwitch('f', ['/', '-'], True) then
ClearTable := True;
Persistent := False;
if FindCmdLineSwitch('p', ['/', '-'], True) then
Persistent := True;
SkipCmdSwitchs(ParamIndexOffset);
if (ParamCount <= ParamIndexOffset) then
begin
Usage;
Exit;
end;
Inc(ParamIndexOffset);
S :=LowerCase(ParamStr(ParamIndexOffset));
PrintTable := (S = 'print');
AddRoute := (S = 'add');
DeleteRoute := (S = 'delete');
ModifyRoute := (S = 'change');
if not (PrintTable or AddRoute or DeleteRoute or ModifyRoute) then
begin
Usage;
Exit;
end;
SkipCmdSwitchs(ParamIndexOffset);
if (ParamCount > ParamIndexOffset) and
(LowerCase(ParamStr(ParamIndexOffset + 1)) <> 'mask') and
(LowerCase(ParamStr(ParamIndexOffset + 1)) <> 'metric') and
(LowerCase(ParamStr(ParamIndexOffset + 1)) <> 'if') then
begin
Inc(ParamIndexOffset);
Destination := ParamStr(ParamIndexOffset)
end
else
Destination := '';
SkipCmdSwitchs(ParamIndexOffset);
Mask := '';
if (ParamCount > ParamIndexOffset) and
(LowerCase(ParamStr(ParamIndexOffset + 1)) = 'mask') then
begin
Inc(ParamIndexOffset);
if (ParamCount > ParamIndexOffset) then
begin
Inc(ParamIndexOffset);
Mask := ParamStr(ParamIndexOffset);
end;
end;
SkipCmdSwitchs(ParamIndexOffset);
GateWay := '';
if (ParamCount > ParamIndexOffset) and
(LowerCase(ParamStr(ParamIndexOffset + 1)) <> 'metric') and
(LowerCase(ParamStr(ParamIndexOffset + 1)) <> 'if') then
begin
Inc(ParamIndexOffset);
GateWay := ParamStr(ParamIndexOffset)
end;
SkipCmdSwitchs(ParamIndexOffset);
Metric := '';
if (ParamCount > ParamIndexOffset) and
(LowerCase(ParamStr(ParamIndexOffset + 1)) = 'metric') then
begin
Inc(ParamIndexOffset);
if (ParamCount > ParamIndexOffset) then
begin
Inc(ParamIndexOffset);
Metric := ParamStr(ParamIndexOffset)
end;
end;
SkipCmdSwitchs(ParamIndexOffset);
IfNo := '';
if (ParamCount > ParamIndexOffset) and
(LowerCase(ParamStr(ParamIndexOffset + 1)) = 'if') then
begin
Inc(ParamIndexOffset);
if (ParamCount > ParamIndexOffset) then
begin
Inc(ParamIndexOffset);
IfNo := ParamStr(ParamIndexOffset)
end;
end;
if PrintTable then
begin
DisplayInterfaceList;
DisplayRoutingTable(Destination, GateWay);
Exit;
end;
if AddRoute then
begin
if (Destination = '') or (Mask = '') or (GateWay = '') then
begin
Usage;
Exit;
end;
if ClearTable then
DoClearTable;
CreateIpEntry(Destination, Mask, Gateway, Metric, IfNo, Persistent);
Exit;
end;
if DeleteRoute then
begin
if (Destination = '') and (GateWay = '') then
begin
Usage;
Exit;
end;
if ClearTable then
DoClearTable;
if not DeleteIpEntry(Destination, GateWay) then
WriteLn(sRouteNotFound);
Exit;
end;
if ModifyRoute then
begin
if (Destination = '') or (Mask = '') or (GateWay = '') then
begin
Usage;
Exit;
end;
if ClearTable then
DoClearTable;
ModifyIpEntry(Destination, Mask, Gateway, Metric, IfNo, Persistent);
Exit;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -