如何获得局域网内全部电脑的动态IP地址 ( 积分: 200 )

  • 主题发起人 主题发起人 盖徐岩
  • 开始时间 开始时间

盖徐岩

Unregistered / Unconfirmed
GUEST, unregistred user!
本人单位有13台电脑通过一部路由器和猫实现自动拨号上网,每台电脑由路由器的DHCP
自动分配IP地址  问如何用 DELPHI 编程实现 获取每台电脑的动态IP地址
 
本人单位有13台电脑通过一部路由器和猫实现自动拨号上网,每台电脑由路由器的DHCP
自动分配IP地址  问如何用 DELPHI 编程实现 获取每台电脑的动态IP地址
 
根据网段一个一个ping了
 
我说的是 用编程实现 例如  财务01  192.168.1.X
               财务02  192.168.1.Y
               总务01  192.168.1.Z
               拓展01  192.168.1.T
帮帮忙,少灌水
 
你必须要知道每一个计算机的MAC地址,然后采用扫描的方式,来确定其IP地址
 
在 68端口上监听 DHCP Server 广播的 DHCPOFF 和 DHCPACK 包
 
详见RFC2131 和 RFC2132
 
说说干什么用的,然后可以找个比较折衷的办法
 
在大富翁上找的,原来的贴忘了:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, WinSock;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
function GetServerList(var List: TStringList): Boolean;
function GetUsers(GroupName: string; var List: TStringList): Boolean;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
function Tform1.GetServerList(var List: TStringList): Boolean;
type
TNetResourceArray = ^TNetResource; //网络类型的数组
var
NetResource: TNetResource;
Buf: Pointer;
Count, BufSize, Res: DWORD;
lphEnum: THandle;
p: TNetResourceArray;
i, j: SmallInt;
NetworkTypeList: TList;
begin
Result := False;
NetworkTypeList := TList.Create;
List.Clear;
//获取整个网络中的文件资源的句柄,lphEnum为返回名柄
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, nil, lphEnum);
if Res <> NO_ERROR then exit; //Raise Exception(Res);//执行失败
//获取整个网络中的网络类型信息
Count := $FFFFFFFF; //不限资源数目
BufSize := 8192; //缓冲区大小设置为8K
GetMem(Buf, BufSize); //申请内存,用于获取工作组信息
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if (Res = ERROR_NO_MORE_ITEMS) //资源列举完毕
or (Res <> NO_ERROR) //执行失败
then Exit;
P := TNetResourceArray(Buf);
for I := 0 to Count - 1 do //记录各个网络类型的信息
begin
NetworkTypeList.Add(p);
Inc(P);
end;

//WNetCloseEnum关闭一个列举句柄
Res := WNetCloseEnum(lphEnum); //关闭一次列举
if Res <> NO_ERROR then exit;
for J := 0 to NetworkTypeList.Count - 1 do //列出各个网络类型中的所有工作组名称
begin //列出一个网络类型中的所有工作组名称
NetResource := TNetResource(NetworkTypeList.Items[J]^); //网络类型信息
//获取某个网络类型的文件资源的句柄,NetResource为网络类型信息,lphEnum为返回名柄
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource, lphEnum);
if Res <> NO_ERROR then break; //执行失败
while true do //列举一个网络类型的所有工作组的信息
begin
Count := $FFFFFFFF; //不限资源数目
BufSize := 8192; //缓冲区大小设置为8K
GetMem(Buf, BufSize); //申请内存,用于获取工作组信息
//获取一个网络类型的文件资源信息,
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if (Res = ERROR_NO_MORE_ITEMS) //资源列举完毕
or (Res <> NO_ERROR) //执行失败
then break;
P := TNetResourceArray(Buf);
for I := 0 to Count - 1 do //列举各个工作组的信息
begin
List.Add(StrPAS(P^.lpRemoteName)); //取得一个工作组的名称
Inc(P);
end;
end;
Res := WNetCloseEnum(lphEnum); //关闭一次列举
if Res <> NO_ERROR then break; //执行失败
end;
Result := True;
FreeMem(Buf);
NetworkTypeList.Destroy;
end;

function Tform1.GetUsers(GroupName: string; var List: TStringList): Boolean;
type
TNetResourceArray = ^TNetResource; //网络类型的数组
var
NetResource: TNetResource;
Buf: Pointer;
Count, BufSize, Res: DWord;
Ind: Integer;
lphEnum: THandle;
Temp: TNetResourceArray;
begin
Result := False;
List.Clear;
FillChar(NetResource, SizeOf(NetResource), 0); //初始化网络层次信息
NetResource.lpRemoteName := @GroupName[1]; //指定工作组名称
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER; //类型为服务器(工作组)
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK; //列举文件资源信息
//获取指定工作组的网络资源句柄
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource, lphEnum);
if Res <> NO_ERROR then Exit; //执行失败
while True do //列举指定工作组的网络资源
begin
Count := $FFFFFFFF; //不限资源数目
BufSize := 8192; //缓冲区大小设置为8K
GetMem(Buf, BufSize); //申请内存,用于获取工作组信息
//获取计算机名称
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if Res = ERROR_NO_MORE_ITEMS then break; //资源列举完毕
if (Res <> NO_ERROR) then Exit; //执行失败
Temp := TNetResourceArray(Buf);
for Ind := 0 to Count - 1 do //列举工作组的计算机名称
begin
//获取工作组的计算机名称,+2表示删除&quot;//&quot;,如//wangfajun=>wangfajun
List.Add(Temp^.lpRemoteName + 2);
Inc(Temp);
end;
FreeMem(Buf);
end;
Res := WNetCloseEnum(lphEnum); //关闭一次列举
if Res <> NO_ERROR then exit; //执行失败
Result := True;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
WSAData: TWSAData;
HostEnt: PHostEnt;
sIP: string;
i, j: Integer;
list1, list2: TStringList;
begin

Listbox1.Clear;
WSAStartup(2, WSAData);
list1 := TStringList.Create;
list2 := TStringList.Create;
if GetServerList(list1) then
begin
for i := 0 to list1.Count - 1 do
begin
if getusers(list1.Strings, list2) then
begin
for j := 0 to list2.Count - 1 do
begin
hostEnt:=nil;
sip:='';
HostEnt := gethostbyname(PChar(list2.Strings[j]));
if HostEnt <> nil then
begin
with HostEnt^ do
sIP := Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
end;
listbox1.Items.Add(list1.Strings+' : '+ list2.Strings[j]+' , '+sIP);
end;
end;
end;
end;
list1.Destroy;
list2.Destroy;
WSACleanup;

end;

end.
 
我有做好的..要就發給你
 
风卷残月,你的程序没有作用啊~~
 
to cqjxnc@21cn.com。谢谢。
 
通过机器名得到IP地址


function procedure TForm1.nametoip(name:string):string;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
begin
result:='';
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(name));
if HostEnt <> nil then
begin
with HostEnt^ do
result:= Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
end;
WSACleanup;
end;

这样你就可以根据,如“财务01”,得到IP地址了.
 
楼主的意思很模糊.你要获取的是IP地址,还是要保持机器名与动态IP的一致
内网中的主机名,大致可以认为是固定的.所以扫描一遍内网,看主机名就可以知道当前的IP了.
或者你自己建立一个 机器名--自定义名称 的映射,扫面一遍内网后,依据机器名显示你自定义的名称.
 
看看这个???
:淡淡的笑, 时间:2001-8-27 19:17:00, ID:607642
EdtHostName.Text 为本机的IP地址或远端IP地址(必须开放IPC$,默认为开)
procedure TForm1.Button1Click(Sender: TObject);

var
seaNetResource:NETRESOURCE;
seaResult:DWORD;
seaAccessBuffer:string[255];
seaAccessBufferLength:DWORD;
sidbuffer:string[255];
lpAccessBuffer:PChar;
seaSID:PSID;
seaSIDlength:DWORD;
seaReferencedDomainName:string[255];
seaReferencedDomainNameLength:DWORD;
lpReferencedDomainName:PChar;
seaSIDnameuse:SID_NAME_USE;
tempPchar:Pchar;
SidSubCount:PUCHAR;
i,j:integer;
StoreCount:byte;
tempPDWORD:PDWORD;
tempDWORD:DWORD;
storeSIDsub: array [0..8] of Integer;
seaSidIdentify:PSIDIdentifierAuthority;
newSID:PSID;
seaAccountBuffer:String[255];
pAccountName:LPTSTR;
AccountLength:DWORD;
BlResult:Boolean;
SeedLine:string;
const
AccountType:array [0..8] of string=('','User','Group','Domain','alias',
'WelknowGroup','Deleted','Invalid','Unknown');
begin
Button1.Enabled:=False;
lpAccessBuffer:=@seaAccessBuffer;
lpReferencedDomainName:=@seaReferencedDomainName;
seaSID:=@sidbuffer;
seaAccessBufferLength:=64;
seaSIDLength:=255;
seaReferencedDomainNameLength:=255;
seaNetResource.dwScope:=RESOURCE_GLOBALNET;
seaNetResource.dwType:=RESOURCETYPE_ANY;
seaNetResource.lpLocalName:=PChar('');
seaNetResource.lpRemoteName:=PChar('//'+EdtHostName.Text+'/IPC$');
seaNetResource.lpProvider:=PChar('');
seaResult:=WNetAddConnection2(seaNetResource,PChar(''),PChar(''),CONNECT_PROMPT);
if seaResult=NO_ERROR then
begin
if LookupAccountName(PChar('//'+EdtHostName.Text),Pchar(seedline),seaSID,seaSIDlength,
lpReferencedDomainName,seaReferencedDomainNamelength,seaSIDnameuse)
then
begin
cpDomain.Caption:=string(lpReferencedDomainName);
seaSidIdentify:=GetSidIdentifierAuthority(seaSID);
SidSubCount:=GetSidSubAuthorityCount(seaSID);
StoreCount:=SidSubCount^;

for i:=0 to Integer(StoreCount)-1 do
begin
tempPDWORD:=GetSidSubAuthority(seaSID,i);
storeSIDsub:=tempPDWORD^;
end;

//start to get username
pAccountName:=@seaAccountBuffer ;
seaReferencedDomainNameLength:=255;
AccountLength:=255;
storeSIDsub[StoreCount-1]:=500;
if AllocateAndInitializeSid(seaSidIdentify^,SidSubCount^,StoreSidSub[0],
StoreSidSub[1],StoreSidSub[2],StoreSidSub[3],StoreSidSub[4],
StoreSidSub[5],StoreSidSub[6],StoreSidSub[7],newSID) then
begin

if LookupAccountSid(PChar('//'+EdtHostName.Text),newsid,
pAccountName,AccountLength,lpReferencedDomainName,
seaReferencedDomainNameLength,seaSIDnameuse)
then
begin
lbAdmin.Caption:=String(pAccountName);
ListBox1.Items.Add('//'+lpReferencedDomainName+'/'+pAccountName+' Built-in Admin');
end
else
exit;
FreeSid(newSID);
//tempDWORD:=GetLastError;
j:=1;
i:=1000;
while j<=30 do
begin
seaReferencedDomainNamelength:=255;
AccountLength:=255;
StoreSidSub[StoreCount-1]:=i;
AllocateAndInitializeSid(seaSidIdentify^,SidSubCount^,StoreSidSub[0],
StoreSidSub[1],StoreSidSub[2],StoreSidSub[3],StoreSidSub[4],
StoreSidSub[5],StoreSidSub[6],StoreSidSub[7],newSID);
if LookupAccountSid(PChar('//'+EdtHostName.Text),newsid,
pAccountName,AccountLength,lpReferencedDomainName,
seaReferencedDomainNameLength,seaSIDnameuse)
then
begin
if seaSIDnameuse=sidTypeInvalid then j:=j+1
else if seaSIDnameuse<>sidTypeDeletedAccount then
begin
j:=0;
ListBox1.Items.Add('//'+lpReferencedDomainName+
'/'+pAccountName+' '+AccountType[seaSIDnameuse]);
StatusBar1.SimpleText:=pAccountName;
end;
end
else
j:=j+1;
Application.ProcessMessages;
i:=i+1;
FreeSID(newsid);
end;
end;

end
else ShowMessage('Cannot locate sid infomation!');
end
else ShowMessage('Connection Error!');
WNetCancelConnection2(PChar('//'+EdtHostName.Text+'/IPC$'),0,true);
Button1.Enabled:=True;
end;
 
来来来~~~质量三包,童叟无欺~~~给你做了一个DEMO,下载一用便知道~
下载地址:
到http://free.ys168.com/?crazycock 点文件夹“测试”,然后下载。

[red]包熟包甜包好吃,不好吃退货啊~[:D][:D][:D][/red]
 
关键是看你要做什么? 如果是写个程序的话,为什么不用广播呢?对一个端口广播一下,看谁应答谁就是了,很简单。
 
crazycock
好人一生平安!~~:)
 
后退
顶部