unit ScanIPUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, StdCtrls, Mask, Spin, Buttons,Gauges,StrUtils,
SyncObjs, IdBaseComponent, IdComponent, IdIPWatch, Menus,ImgList, IPEdit;
type
TScanIPFm = class(TForm)
Panel2: TPanel;
Panel3: TPanel;
Splitter: TSplitter;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
sBtnStratScanIP: TSpeedButton;
sBtnStopScanIP: TSpeedButton;
Gauge2: TGauge;
Gauge3: TGauge;
RadioGroupIP: TRadioGroup;
Label3: TLabel;
edtPort: TEdit;
RadioGroupHost: TRadioGroup;
Panel6: TPanel;
Label4: TLabel;
edtHost: TEdit;
Label5: TLabel;
edtPortFrom: TEdit;
Label6: TLabel;
edtPortTo: TEdit;
Gauge1: TGauge;
sBtnStartScanHost: TSpeedButton;
sBtnStopScanHost: TSpeedButton;
Panel7: TPanel;
tvwScan: TTreeView;
Panel8: TPanel;
Timer1: TTimer;
Gauge4: TGauge;
SpinEditMaxThread: TSpinEdit;
Label9: TLabel;
Panel9: TPanel;
lstVwScan: TListView;
ComboBoxIP: TComboBox;
Label12: TLabel;
IdIPWatch1: TIdIPWatch;
PopupMenu1: TPopupMenu;
PopupMenu2: TPopupMenu;
mnulvwDeleteItem: TMenuItem;
mnulvwClearAll: TMenuItem;
ImageList1: TImageList;
Gauge5: TGauge;
mnulvwSelectAll: TMenuItem;
mnulvwCancelAll: TMenuItem;
mnutvwDelete: TMenuItem;
mnutvwDelAll: TMenuItem;
IPEditFrom: TIPEdit;
IPEditTo: TIPEdit;
Label1: TLabel;
Label2: TLabel;
plScanStatus: TPanel;
procedure RadioGroupIPClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure sBtnStratScanIPClick(Sender: TObject);
procedure sBtnStopScanIPClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure lstVwScanMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure SpeedButton15Click(Sender: TObject);
procedure lstVwScanClick(Sender: TObject);
procedure RadioGroupHostClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure mnulvwDeleteItemClick(Sender: TObject);
procedure mnulvwClearAllClick(Sender: TObject);
procedure sBtnStartScanHostClick(Sender: TObject);
procedure mnulvwSelectAllClick(Sender: TObject);
procedure mnulvwCancelAllClick(Sender: TObject);
procedure mnutvwDeleteClick(Sender: TObject);
procedure mnutvwDelAllClick(Sender: TObject);
procedure SpinEditMaxThreadChange(Sender: TObject);
private
{ Private declarations }
public
FromA_IP,ToA_IP : Byte;
FromB_IP,ToB_IP : Byte;
FromC_IP,ToC_IP : Byte;
FromD_IP,ToD_IP : Byte;
procedure FillLocalIP;
procedure GetTheScanIP;
procedure PingPorcess;
Procedure ScanPortPorcess;
procedure SingleHostScan;
procedure SingleHostScan2;
procedure MultiHostScan;
end;
var
ScanIPFm : TScanIPFm;
OldEditText : String;
LetPingSingle: TCriticalSection;
LetScanSingle: TCriticalSection;
SdPingCount : Integer;
SdScanCount : integer;
MultiPingCount:integer;
MultiScanCount:integer;
implementation
uses
PingThreadUnit,ScanPortThreadUnit,ScanHostPortThreadUnit{, UserInfo};
{$R *.dfm}
procedure TScanIPFm.GetTheScanIP;
begin
FromA_IP:=IPEditFrom.Field0; ToA_IP:= IPEditTo.Field0;
FromB_IP:=IPEditFrom.Field1; ToB_IP:= IPEditTo.Field1;
FromC_IP:=IPEditFrom.Field2; ToC_IP:= IPEditTo.Field2;
FromD_IP:=IPEditFrom.Field3; ToD_IP:= IPEditTo.Field3;
end;
procedure TScanIPFm.FillLocalIP;
var
i:integer;
TheIndex:integer;
TheIPStr:string;
begin
try
TheIPStr:=ComboBoxIP.Text;
For i:=1 to Length(TheIPStr) do
begin
if TheIPStr
='.' then
begin
TheIndex:=i;
IPEditFrom.Field0:=StrToInt(StrUtils.leftStr(TheIPStr,TheIndex-1));
IPEditTo.Field0:=IPEditFrom.Field0;
TheIPStr:=StrUtils.RightStr(TheIPStr,Length(TheIPStr)-TheIndex);
break;
end;
end;
For i:=1 to Length(TheIPStr) do
begin
if TheIPStr='.' then
begin
TheIndex:=i;
IPEditFrom.Field1:=StrToInt(StrUtils.leftStr(TheIPStr,TheIndex-1));
IPEditTo.Field1:=IPEditFrom.Field1;
TheIPStr:=StrUtils.RightStr(TheIPStr,Length(TheIPStr)-TheIndex);
break;
end;
end;
For i:=1 to Length(TheIPStr) do
begin
if TheIPStr='.' then
begin
TheIndex:=i;
IPEditFrom.Field2:=StrToInt(StrUtils.leftStr(TheIPStr,TheIndex-1));
IPEditTo.Field2:=IPEditFrom.Field2;
TheIPStr:=StrUtils.RightStr(TheIPStr,Length(TheIPStr)-TheIndex);
break;
end;
end;
IPEditFrom.Field3:=StrToInt(TheIPStr);
IPEditTo.Field3:=IPEditFrom.Field3;
except
end;
end;
procedure TScanIPFm.PingPorcess;
var
iA,iB,iC,iD:integer;
ThePingIP:String;
ThePingThreadingThread;
TheTimeStr :String;
TotalCount:int64;
begin
if SdPingCount>0 then exit;
Timer1.Enabled:=True;
TotalCount:=0;
GetTheScanIP;
TheTimeStr:=TimeToStr(Time);
For iA:=FromA_IP To ToA_IP do
For iB:=FromB_IP To ToB_IP do
For iC:=FromC_IP To ToC_IP do
For iD:=FromD_IP To ToD_IP do
begin
TotalCount:=TotalCount+1;
end;
Gauge4.MaxValue:=MultiPingCount;
Gauge2.Progress:=0;
try
Gauge2.MaxValue:=TotalCount;
except
beep;
sBtnStartScanHost.Down:=True;
plScanStatus.Caption:='搜索初始化错误!';
Timer1.Enabled:=False;
exit;
end;
For iA:=FromA_IP To ToA_IP do
For iB:=FromB_IP To ToB_IP do
For iC:=FromC_IP To ToC_IP do
For iD:=FromD_IP To ToD_IP do
begin
ThePingIP:=IntToStr(iA)+'.'+IntToStr(iB)+'.'+ IntToStr(iC)+'.'+IntToStr(iD);
IPEditFrom.Field0:=iA;
IPEditFrom.Field1:=iB;
IPEditFrom.Field2:=iC;
IPEditFrom.Field3:=iD;
Repeat //设置阻塞操作
Application.ProcessMessages;
Gauge4.Progress:=SdPingCount;
plScanStatus.Caption:='当前共发出'+IntToStr(SdPingCount)+'个搜索线程';
if sBtnStopScanIP.Down then
begin
Repeat
Application.ProcessMessages;
Gauge4.Progress:=SdPingCount;
plScanStatus.Caption:='当前还有'+IntToStr(SdPingCount)
+'个搜索线程未返回,请稍候...';
until SdPingCount=0;
sBtnStopScanIP.Down:=True;
Timer1.Enabled:=False;
Gauge4.Progress:=0;
TheTimeStr:='扫描时间从:'+TheTimeStr+' 到:'+TimeToStr(Time);
Application.MessageBox(PChar(TheTimeStr),'扫描中断',
MB_OK+MB_ICONSTOP);
plScanStatus.Caption:='搜索线程已经全部返回!';
exit;
end;
until SdPingCount<MultiPingCount;
try
ThePingThread:=PingThread.Create(ThePingIP,lstVwScan);
except
beep;
sBtnStopScanIP.Down:=True;
plScanStatus.Caption:='搜索线程创建错误错误!';
end;
Gauge2.Progress:=Gauge2.Progress+1;
end;
Repeat
Application.ProcessMessages;
Gauge4.Progress:=SdPingCount;
plScanStatus.Caption:='当前还有'+IntToStr(SdPingCount)+'个搜索线程未返回,请稍候...';
until SdPingCount=0;
sBtnStopScanIP.Down:=True;
Timer1.Enabled:=False;
TheTimeStr:='扫描时间从:'+TheTimeStr+' 到:'+TimeToStr(Time);
plScanStatus.Caption:='搜索线程已经全部返回!';
Gauge4.Progress:=0;
Application.MessageBox(PChar(TheTimeStr),'扫描完毕',
MB_OK+MB_ICONINFORMATION);
end;
Procedure TScanIPFm.ScanPortPorcess;
var
iA,iB,iC,iD:integer;
ThePingIP:String;
TheScanThread:ScanPortThread;
TheTimeStr :String;
TotalCount:int64;
begin
if SdPingCount>0 then exit;
Timer1.Enabled:=True;
TotalCount:=0;
GetTheScanIP;
TheTimeStr:=TimeToStr(Time);
For iA:=FromA_IP To ToA_IP do
For iB:=FromB_IP To ToB_IP do
For iC:=FromC_IP To ToC_IP do
For iD:=FromD_IP To ToD_IP do
begin
TotalCount:=TotalCount+1;
end;
Gauge4.MaxValue:=MultiPingCount;
Gauge2.Progress:=0;
try
Gauge2.MaxValue:=TotalCount;
except
beep;
sBtnStopScanIP.Down:=True;
plScanStatus.Caption:='搜索初始化错误!';
Timer1.Enabled:=False;
exit;
end;
For iA:=FromA_IP To ToA_IP do
For iB:=FromB_IP To ToB_IP do
For iC:=FromC_IP To ToC_IP do
For iD:=FromD_IP To ToD_IP do
begin
ThePingIP:=IntToStr(iA)+'.'+IntToStr(iB)+'.'+
IntToStr(iC)+'.'+IntToStr(iD);
IPEditFrom.Field0:=iA;
IPEditFrom.Field1:=iB;
IPEditFrom.Field2:=iC;
IPEditFrom.Field3:=iD;
Repeat //设置阻塞操作
Application.ProcessMessages;
Gauge4.Progress:=SdPingCount;
plScanStatus.Caption:='当前共发出'+IntToStr(SdPingCount)+'个搜索线程';
if sBtnStopScanIP.Down then
begin
Repeat
Application.ProcessMessages;
Gauge4.Progress:=SdPingCount;
plScanStatus.Caption:='当前还有'+IntToStr(SdPingCount)
+'个搜索线程未返回,请稍候...';
until SdPingCount=0;
sBtnStopScanIP.Down:=True;
Timer1.Enabled:=False;
Gauge4.Progress:=0;
TheTimeStr:='扫描时间从:'+TheTimeStr+' 到:'+TimeToStr(Time);
Application.MessageBox(PChar(TheTimeStr),'扫描中断',
MB_OK+MB_ICONSTOP);
plScanStatus.Caption:='搜索线程已经全部返回!';
exit;
end;
until SdPingCount<MultiPingCount;
try
TheScanThread:=ScanPortThread.Create(ThePingIP,
StrToInt(edtPort.Text),lstVwScan);
except
beep;
sBtnStopScanIP.Down:=True;
plScanStatus.Caption:='搜索线程创建错误错误!';
end;
Gauge2.Progress:=Gauge2.Progress+1;
end;
Repeat
Application.ProcessMessages;
Gauge4.Progress:=SdPingCount;
plScanStatus.Caption:='当前还有'+IntToStr(SdPingCount) +'个搜索线程未返回,请稍候...';
until SdPingCount=0;
sBtnStopScanIP.Down:=True;
Timer1.Enabled:=False;
TheTimeStr:='扫描时间从:'+TheTimeStr+' 到:'+TimeToStr(Time);
plScanStatus.Caption:='搜索线程已经全部返回!';
Gauge4.Progress:=0;
Application.MessageBox(PChar(TheTimeStr),'扫描完毕',
MB_OK+MB_ICONINFORMATION);
end;
procedure TScanIPFm.SingleHostScan;
var
i:integer;
ThePortScan:ScanHostPortThread;
TheNewNode:TTreeNode;
FromPort,ToPort:integer;
begin
if SdScanCount<>0 then exit;
TheNewNode:=tvwScan.Items.Add(nil,edtHost.Text);
TheNewNode.ImageIndex:=0;
TheNewNode.StateIndex:=0;
TheNewNode.SelectedIndex:=0;
Gauge1.MaxValue:=StrToInt(edtPortTo.Text);
Gauge1.Progress:=0;
Gauge5.MaxValue:=MultiScanCount;
Gauge5.Progress:=0;
FromPort:=StrToInt(edtPortFrom.text);
ToPort:=StrToInt(edtPortTo.text);
For i:=FromPort To ToPort do
begin
repeat
Application.ProcessMessages;
Gauge5.Progress:=SdScanCount;
if sBtnStopScanHost.Down then
begin
repeat
Application.ProcessMessages;
Gauge5.Progress:=SdScanCount;
until SdScanCount=0;
Application.MessageBox('扫描由您主动结束','扫描中断',
MB_OK+MB_ICONSTOP);
Gauge5.Progress:=0;
exit;
end;
until SdScanCount<MultiScanCount;
Gauge1.Progress:=i;
Gauge5.Progress:=SdScanCount;
Panel8.Caption:='扫描'+edtHost.Text+':'+IntToStr(i);
Try
ThePortScan:=ScanHostPortThread.Create(edtHost.Text,i,tvwScan);
except
end;
end;
repeat
Application.ProcessMessages;
Gauge5.Progress:=SdScanCount;
until SdScanCount=0;
sBtnStopScanHost.Down:=True;
Gauge5.Progress:=0;
Application.MessageBox('扫描任务完成!','扫描完毕',
MB_OK + MB_ICONINFORMATION);
end;
procedure TScanIPFm.SingleHostScan2;
var
i:integer;
ThePortScan:ScanHostPortThread;
FromPort,ToPort:integer;
begin
Gauge1.MaxValue:=StrToInt(edtPortTo.Text);
Gauge1.Progress:=0;
Gauge5.MaxValue:=MultiScanCount;
Gauge5.Progress:=0;
FromPort:=StrToInt(edtPortFrom.text);
ToPort:=StrToInt(edtPortTo.text);
For i:=FromPort To ToPort do
begin
repeat
Application.ProcessMessages;
Gauge5.Progress:=SdScanCount;
if sBtnStopScanHost.Down then
begin
repeat
Application.ProcessMessages;
Gauge5.Progress:=SdScanCount;
until SdScanCount=0;
Application.MessageBox('扫描由您主动结束','扫描中断',
MB_OK+MB_ICONSTOP);
Gauge5.Progress:=0;
exit;
end;
until SdScanCount<MultiScanCount;
Gauge1.Progress:=i;
Gauge5.Progress:=SdScanCount;
Panel8.Caption:='扫描'+edtHost.Text+':'+IntToStr(i);
Try
ThePortScan:=ScanHostPortThread.Create(edtHost.Text,i,tvwScan);
except
end;
end;
repeat
Application.ProcessMessages;
Gauge5.Progress:=SdScanCount;
until SdScanCount=0;
Gauge5.Progress:=0;
end;
procedure TScanIPFm.MultiHostScan;
var
i:integer;
TheNewNode:TTreeNode;
begin
SdScanCount:=0;
For i:=0 to lstVwScan.Items.Count-1 do
begin
if lstVwScan.Items.Checked then
begin
edtHost.Text:=lstVwScan.Items.Caption;
TheNewNode:=tvwScan.Items.Add(nil,edtHost.Text);
TheNewNode.ImageIndex:=0;
TheNewNode.StateIndex:=0;
TheNewNode.SelectedIndex:=0;
Repeat
Application.ProcessMessages;
if sBtnStopScanHost.Down then exit;
until SdScanCount=0;
SingleHostScan2;
end;
end;
Application.MessageBox('扫描任务完成!','扫描完毕',
MB_OK+MB_ICONINFORMATION);
sBtnStopScanHost.Down:=True;
end;
procedure TScanIPFm.RadioGroupIPClick(Sender: TObject);
begin
if RadioGroupIP.ItemIndex=0 then
edtPort.Enabled:=False else
edtPort.Enabled:=True;
end;
procedure TScanIPFm.Timer1Timer(Sender: TObject);
var
MidColor:TColor;
begin
Gauge3.Progress:=Gauge3.Progress+10;
if Gauge3.Progress=100 then
begin
Gauge3.Progress:=0;
MidColor:=Gauge3.BackColor;
Gauge3.BackColor:=Gauge3.ForeColor;
Gauge3.ForeColor:=MidColor;
end;
end;
procedure TScanIPFm.sBtnStratScanIPClick(Sender: TObject);
begin
if RadioGroupIp.ItemIndex=1 then
ScanPortPorcess else PingPorcess;
end;
procedure TScanIPFm.sBtnStopScanIPClick(Sender: TObject);
begin
Timer1.Enabled:=false;
end;
procedure TScanIPFm.FormCreate(Sender: TObject);
begin
OldEditText:='0';
SdPingCount :=0;
SdScanCount :=0;
MultiPingCount:=100;
MultiScanCount:=100;
LetPingSingle:=TCriticalSection.Create;
LetScanSingle:=TCriticalSection.Create;
end;
procedure TScanIPFm.FormDestroy(Sender: TObject);
begin
LetPingSingle.Free;
LetScanSingle.Free;
end;
procedure TScanIPFm.lstVwScanMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
lstVwScan.Hint:='共搜索到'+IntToStr(lstVwScan.Items.Count)+'个';
end;
procedure TScanIPFm.SpeedButton15Click(Sender: TObject);
begin
Close;
end;
procedure TScanIPFm.lstVwScanClick(Sender: TObject);
begin
if lstVwScan.Selected<>nil then
edtHost.Text:=lstVwScan.Selected.Caption;
end;
procedure TScanIPFm.RadioGroupHostClick(Sender: TObject);
begin
if RadioGroupHost.ItemIndex=0 then
edtHost.Enabled:=True
else
edtHost.Enabled:=False;
end;
procedure TScanIPFm.FormShow(Sender: TObject);
begin
IdIPWatch1.Active:=True;
ComboBoxIP.Text:=IdIPWatch1.CurrentIP;
ComboBoxIP.Items.Text:=IdIPWatch1.IPHistoryList.Text;
IdIPWatch1.Active:=False;
FillLocalIP;
end;
procedure TScanIPFm.mnulvwDeleteItemClick(Sender: TObject);
begin
if lstVwScan.Selected<>nil then
lstVwScan.Selected.Delete;
end;
procedure TScanIPFm.mnulvwClearAllClick(Sender: TObject);
begin
if lstVwScan.Selected<>nil then
lstVwScan.Items.Clear;
end;
procedure TScanIPFm.sBtnStartScanHostClick(Sender: TObject);
begin
if RadioGroupHost.ItemIndex=0 then
SingleHostScan
else
MultiHostScan;
end;
procedure TScanIPFm.mnulvwSelectAllClick(Sender: TObject);
var
i : integer;
begin
For i:=0 to lstVwScan.Items.Count-1 do
lstVwScan.Items.Checked:=True;
end;
procedure TScanIPFm.mnulvwCancelAllClick(Sender: TObject);
var
i : integer;
begin
For i:=0 to lstVwScan.Items.Count-1 do
lstVwScan.Items.Checked:=False;
end;
procedure TScanIPFm.mnutvwDeleteClick(Sender: TObject);
begin
if tvwScan.Selected<>nil then
tvwScan.Selected.Delete;
end;
procedure TScanIPFm.mnutvwDelAllClick(Sender: TObject);
begin
tvwScan.Items.Clear;
end;
procedure TScanIPFm.SpinEditMaxThreadChange(Sender: TObject);
begin
MultiPingCount:=SpinEditMaxThread.Value;
MultiScanCount:=SpinEditMaxThread.Value;
end;
end.