⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 u_frmsearchnetip.pas

📁 一个扫描局域网内的IP地址和MAC物理地址之间的对应表。根据上述功能做成了远程开启计算机。
💻 PAS
📖 第 1 页 / 共 2 页
字号:
         str_ip:=inttohex(ComputerIP,8);
         str_ip:=inttostr(strtoint('$' + copy(str_ip,7,2)))
                +'.' + inttostr(strtoint('$' + copy(str_ip,5,2)))
                +'.' + inttostr(strtoint('$' + copy(str_ip,3,2)))
                +'.' + inttostr(strtoint('$' + copy(str_ip,1,2)));
         LabIP.caption:=str_ip;  {目前扫描的IP}

         FSockAddrIn.SIn_Addr.S_addr := ComputerIP;//inet_addr(pchar(IP));
         str_ip:=inttohex(ComputerIP,8);    //01 00 A8 C0
         str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2);   //==> C0 A8 00 01
         str_ip:=inttohex( strtoint(('$' + str_ip))+1,8);      //==> 加一C0 A8 00 02
         IP_now:=strtoint('$'+str_ip);
         str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2);  //==>02 00 A8 C0
         ComputerIP:=strtoint('$'+str_ip);
         sendto(sockfd, NbtstatPacket,50, 0, FSockAddrIn, sizeof(FSockAddrIn));
         //发送数据后
         waitforsingleobject(WAIT_ACK_EVENT,trunc(  RxSEdDelay.Value)); // 等待事件变成有信号
         ResetEvent(WAIT_ACK_EVENT);   		  // 设为没有信号
      end;
      MakeEnd();
  end;
 {关闭SOCKET}
  WinSockClose();
end;


{ TFrmSearchNetIp }
{消息接送}
procedure TFrmSearchNetIp.ReadData(var Message: TMessage);
var
   Buffer: Array [1..500] of byte;
   flen,len,i,j,pos,name_num: integer;
   Event: word;
   IP:string;
   str_tp,str:string;
   str_list_IP,str_list_name,str_list_disp,str_list_group,str_list_MAC:string;
   StrTp:string;
begin
     flen:=sizeof(FSockAddrIn);
     Event := WSAGetSelectEvent(Message.LParam);
     if Event = FD_READ then
     begin
          len := recvfrom(sockfd, buffer, sizeof(buffer), 0, FSockAddrIn, flen);
          if len> 0 then
          begin
            //FSockAddrIn.sin_addr.S_un_b.s_b1
            with FSockAddrIn.sin_addr.S_un_b  do
              IP:=format('%d.%d.%d.%d',[ord(s_b1),ord(s_b2),ord(s_b3),ord(s_b4)]);
              str_list_IP:=IP;
              //数据分析
              name_num:=0;
              str_tp:='';
	      for i:=1 to len do
	      begin
	        if((buffer[i]=$21)and(buffer[i+1]=$00)and(buffer[i+2]=$01)) then
                begin
	            name_num:=buffer[i+9];
	            break;
	        end;
	      end;
	      if name_num=0 then exit;
	      pos:=i+10;
              str:='';
	      for i:=pos to (pos+18*name_num-1) do
	      begin
	         if (((i-pos)mod 18) =0) then
	         begin
	           for j:=0 to 14 do
	           begin
	             if trim(char(buffer[i+j]))='' then buffer[i+j]:=ord(' ');
	             str:=str+char(buffer[i+j]);
	           end;
	           if (buffer[i+16] and $80)=$80 then
	           begin
                      if buffer[i+15]=$0 then str_list_GROUP:=trim(str);  //网络组
	              str:=str+format('<%x>',[buffer[i+15]]);
		      str:=str+'<GROUP>';
		   end
		   else begin
  	                if buffer[i+15]=$20 then str_list_name:=trim(str) //计算机名
  		        else
		           if buffer[i+15]=$3 then str_list_disp:=trim(str);//计算机说明
		        str:=str+format('<%x>',[buffer[i+15]]);
		        str:=str+'<UNIQUE>';
		   end;
		    str_tp:=str_tp + str +#13+#10;
		    str:='';
		 end;
	      end;
	      for i:=0 to 5 do
	      begin
	          str:=str+format('%.2x.',[buffer[i+pos+18*name_num]]);
	      end;
	      delete(str,length(str),1);
              str_list_MAC:=trim(str); //网卡的MAC码
	      str:='MAC:     '+str;
              str_tp:=str_tp + str + #13+#10;
              {网络信息}
              RxRedDispinf.Text :=RxRedDispinf.Text +IP +  #13+#10+  str_tp+ #13+#10;
              {Grid}
              I:=SgDispNetIP.RowCount;
              if I=2 then
              begin
                 if trim(SgDispNetIP.Cells[0,1])<>'' then
                 begin
                    Inc(i);
                    SgDispNetIP.RowCount:=I;
                 end;
              end
              else begin
                   Inc(i);
                   SgDispNetIP.RowCount:=I;
              end;
              Dec(i);
              SgDispNetIP.Cells[0,i]:=str_list_IP;//'TP地址'
              SgDispNetIP.Cells[1,i]:=str_list_name;//'计算机名称'
              SgDispNetIP.Cells[2,i]:=str_list_disp;//'计算机描述'
              SgDispNetIP.Cells[3,i]:=str_list_GROUP;//'网络组'
              SgDispNetIP.Cells[4,i]:=str_list_MAC;//'网卡MAC码'
              {
              RichEdit1.Text :=RichEdit1.Text + str_list_IP + ' ' + str_list_name + '  ' +
              str_list_disp + '  ' + str_list_GROUP +'    '  + str_list_MAC+ #13+#10
              }
              setlength(TGroupNet,high(TGroupNet)+2);
              TGroupNet[high(TGroupNet)].IP:=str_list_IP;//'TP地址'
              TGroupNet[high(TGroupNet)].ComputerName :=str_list_name;//'计算机名称'
              TGroupNet[high(TGroupNet)].ComputerDes:=str_list_disp;//'计算机描述'
              TGroupNet[high(TGroupNet)].NetGroup:=str_list_GROUP;//'网络组'
              TGroupNet[high(TGroupNet)].NetMacCode :=str_list_MAC;//'网卡MAC码'

              StrTp:=str_list_IP + '   ' +str_list_name+ '   ' +
                str_list_disp + '   ' + str_list_GROUP + '   ' +
                str_list_MAC;
              RxRichEdit1.Lines.Add(StrTp);
          end;
          SetEvent(WAIT_ACK_EVENT);//有信号
     end;
end;
{Grid清除 ClearGrid}
procedure TFrmSearchNetIp.ClearGrid;
begin
   SgDispNetIP.RowCount:=2;
   SgDispNetIP.Rows[1].Text:='';
   SgDispNetIP.Cells[0,0]:='TP地址' ;
   SgDispNetIP.Cells[1,0]:='计算机名称' ;
   SgDispNetIP.Cells[2,0]:='计算机描述' ;
   SgDispNetIP.Cells[3,0]:='网络组' ;
   SgDispNetIP.Cells[4,0]:='网卡MAC码' ;
end;

 
{排序}
{
'TP地址'0
'计算机名称'1
'计算机描述' 2
'网络组'3
'网卡MAC码'4
}
procedure TFrmSearchNetIp.MakeGrid(const IntIndex: integer=3);
Var
  X,XCount:integer;
  GroupNet:TtGroupNet;
  IntMin:integer;
    Procedure GetMinTGroupNet();{获取最小TGroupNet}
    Var
       Y:integer;
       NetMinIP, NetIP:integer;
       DWxMin,DWx:Dword;
       str_ip:string;       
    begin
       GroupNet:= tGroupNet[x] ;
       IntMin:=x;
       for Y:=x+1 to XCount do
       begin
         case Intindex of
           0:begin //'TP地址'0

                NetMinIP:=inet_addr(PChar(GroupNet.IP)) ;  // 192.168.0.1 ===>01 00 A8 C0
                str_ip:=inttohex(NetMinIP,8);    //01 00 A8 C0
                str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2);   //==> C0 A8 00 01
                DWxMin:=strtoint('$'+str_ip);   {转换成为一个长整形数}

                NetIP:=inet_addr(PChar(tGroupNet[y].IP)) ;  // 192.168.0.1 ===>01 00 A8 C0
                str_ip:=inttohex(NetIP,8);    //01 00 A8 C0
                str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2);   //==> C0 A8 00 01
                DWx:=strtoint('$'+str_ip);   {转换成为一个长整形数}

                if DWxMin> DWx then
//                if inet_addr(PChar(GroupNet.IP))> inet_addr(PChar(tGroupNet[y].IP)) then
                begin
                  IntMin:=y;
                  GroupNet:=tGroupNet[IntMin];
                end;
             end;
           1:begin //'计算机名称'1
                if GroupNet.ComputerName > tGroupNet[y].ComputerName then
                begin
                  IntMin:=y;
                  GroupNet:=tGroupNet[IntMin];
                end;
             end;
           2:begin //'计算机描述' 2
                if GroupNet.ComputerDes > tGroupNet[y].ComputerDes then
                begin
                  IntMin:=y;
                  GroupNet:=tGroupNet[IntMin];
                end;
             end;
           3:begin //'网络组'3
                if GroupNet.NetGroup > tGroupNet[y].NetGroup then
                begin
                  IntMin:=y;
                  GroupNet:=tGroupNet[IntMin];
                end;
             end;
           4:begin //'网卡MAC码'4
                if GroupNet.NetMacCode > tGroupNet[y].NetMacCode then
                begin
                  IntMin:=y;
                  GroupNet:=tGroupNet[IntMin];
                end;
             end;
         end;{case}
       end;{For}
    end;
    Procedure MakeGrid();{重写Grid}
    Var
      Y:integer;
      StrTp:string;
    begin
       SgDispNetIP.RowCount:=Xcount+2;
       RxRichEdit1.Text :='';
       StrTp:='';
       For y:=0 to Xcount do
       begin
         SgDispNetIP.Cells[0,Y+1]:=TGroupNet[y].IP;//'TP地址' ;
         SgDispNetIP.Cells[1,Y+1]:=tGroupNet[y].ComputerName;//'计算机名称' ;
         SgDispNetIP.Cells[2,Y+1]:=tGroupNet[y].ComputerDes;//'计算机描述' ;
         SgDispNetIP.Cells[3,Y+1]:=tGroupNet[y].NetGroup;//'网络组' ;
         SgDispNetIP.Cells[4,Y+1]:=tGroupNet[y].NetMacCode;//'网卡MAC码' ;
         StrTp:=TGroupNet[y].IP + '   ' +tGroupNet[y].ComputerName+ '   ' +
                tGroupNet[y].ComputerDes + '   ' + tGroupNet[y].NetGroup + '   ' +
                tGroupNet[y].NetMacCode;
         RxRichEdit1.Lines.Add(StrTp);
       end;{for}
    end;
begin
    XCount:=high(TGroupNet);
    if XCount<0 then exit;
    {清除Grid}
    ClearGrid;
    for x:=0 to XCount do
    begin
        GetMinTGroupNet();
        if x<>IntMin then
        begin
           GroupNet:=TGroupNet[x];
           TGroupNet[x]:=TGroupNet[IntMin];
           TGroupNet[IntMin]:=GroupNet;
        end;
    end;{for}
    MakeGrid();{重写Grid}
end;

procedure TFrmSearchNetIp.SgDispNetIPSelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
    IntGridCol:=ACol;
end;

procedure TFrmSearchNetIp.SgDispNetIPDblClick(Sender: TObject);
begin
   MakeGrid(IntGridCol);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -