PB + API取局域网计算机名、IP、MAC、工作组等信息

来源:互联网 发布:java和php 知乎 编辑:程序博客网 时间:2024/05/26 22:04

PB + API取局域网计算机名、IP、MAC、工作组等信息  

原理是用 NBTSTAT命令取得相关信息 如 (nbtstat -a 机器名 >存放文件名.txt ) ,然后再从 txt中出相关内容,此方法感觉 最好,之前有用网上的getmac.dll获取,但如果是无线的网卡可能取不出,mac信息.

  转载如下:

网上一些取局域网所有计算机信息的编程基本都用到一些DLL,有的例程只能取2000/XP的计算机名。这里我提供一种方法,纯粹使用PB + API来实现取局域网一个网段的所有计算机的信息(使用了网上下载的《PB扩充函数1.5》)。而且,按照此方法,我们可以取得任何DOS命令能取得的信息。

      使用的API

Function boolean IsWindow (Long hwnd ) Library “user32.dll”

FUNCTION ulong WinExec(ref string lpCmdLine,ulong nCmdShow) LIBRARY “kernel32.dll”

       使用到的《PB扩充函数1.5》中的函数

       uf_Network_Pinguf_file_isavailable。虽然使用《PB扩充函数1.5》时需要一个mhand.dll,但是我们用到的2个函数并没有使用到mhand.dll,所以也算是没有借助第三方DLL

       检索IP等信息使用2个自建的函数:

f_searchip():

string ls_ip,ls_temp

//ls_temp为需要检索的ip段,格式为xxx.xxx.xxx.如:1921680

ls_temp=192.168.0.

for i=1 to 255

       ls_ip=ls_temp + string(i)

       f_searchip1(ls_ip)

next

f_searchip1(string ls_ip):

//得到一个一个ip地址计算机信息并且插入数据窗口

u_kchs        lu_kchs  

string        ls_temp

long              ll_row,p

integer        li_filenum

ulong        ll_handle

string        ls_pcname,ls_mac,ls_group

sle_ts.text=’正在检索‘+as_ip

//如果能ping通为有效ip

if not(lu_kchs.uf_Network_Ping(as_ip)) then return

//使用NBTSTAT命令取得相关信息

ls_temp=”nbtstat -a “+as_ip+”>temp\”+as_ip

li_FileNum = FileOpen(“run.bat”,StreamMode!, Write!, LockWrite!, Replace!)

FileWrite(li_FileNum,ls_temp)

FileClose(li_filenum)

ls_temp=’run.bat’

ll_handle=WinExec(ls_temp,0)

//等待DOS窗口关闭

Do While isWindow(ll_handle)

       Yield()

Loop

//等待临时文件创建成功

do while not(lu_kchs.uf_file_isavailable(“temp\”+as_ip))

       Yield()

Loop

//取计算机mac,工作组等信息

li_FileNum=FileOpen(“temp\”+as_ip,StreamMode!,Read! )

if li_FileNum>0 then

       FileRead(li_FileNum,ls_temp)

       FileClose(li_filenum)

       FileDelete(“temp\”+as_ip)

              

       p=pos(ls_temp,’MAC Address = ‘)

       ls_mac=mid(ls_temp,p + 14,17)

              

       p=pos(ls_temp,’UNIQUE      Registered’)

       ls_pcname=trim(mid(ls_temp,p – 21,14))

              

       p=pos(ls_temp,’GROUP       Registered’)

       ls_group=trim(mid(ls_temp,p – 21,14))

      

       if ls_mac=’\NetBT_Tcpip_{942′ then ls_mac=’其他设备

       if ls_mac<>’其他设备‘ and trim(ls_mac) <> ” then

              //因为使用DHCP动态分配IP,所以根据MAC地址来标识唯一的计算机

              ll_row=dw_cx.find(“mac=’”+ls_mac+”‘”,1,dw_cx.rowcount())

              if ll_row>0 then

                     //如果原来有数据则修改

                     dw_cx.o b j e c t.mac[ll_row]=ls_mac

                     dw_cx.o b j e c t.pcname[ll_row]=ls_pcname

                     dw_cx.o b j e c t.workgroup[ll_row]=ls_group

                     dw_cx.o b j e c t.ip[ll_row]=as_ip

                     dw_cx.o b j e c t.online[ll_row]=1

              else

                     ll_row=dw_cx.insertrow(0)

                     dw_cx.o b j e c t.rowid[ll_row]=0

                     dw_cx.o b j e c t.mac[ll_row]=ls_mac

                     dw_cx.o b j e c t.pcname[ll_row]=ls_pcname

                     dw_cx.o b j e c t.workgroup[ll_row]=ls_group

                     dw_cx.o b j e c t.ip[ll_row]=as_ip

                     dw_cx.o b j e c t.online[ll_row]=1

              end if

       end if

end if

 

2 ===以下是我从上面精减成一个只取mac地址的函数(fx_getmac_dos)

定义api

Function boolean IsWindow (Long hwnd ) Library "user32.dll"
FUNCTION ulong WinExec(ref string lpCmdLine,ulong nCmdShow) LIBRARY "kernel32.dll"

string        ls_temp,as_ip

long              ll_row,p

integer        li_filenum

ulong        ll_handle

string        ls_pcname,ls_mac,ls_group

as_ip="jack1"  //此处是计算机名或ip都可

ls_temp="nbtstat -a "+as_ip+" >c:\"+as_ip+".txt"


li_FileNum = FileOpen("run.bat",StreamMode!, Write!, LockWrite!, Replace!)

FileWrite(li_FileNum,ls_temp)

FileClose(li_filenum)

ls_temp='run.bat'

ll_handle=WinExec(ls_temp,0)

//等待DOS窗口关闭

Do While isWindow(ll_handle)

       Yield()
Loop


li_FileNum=FileOpen("c:\"+as_ip+".txt",StreamMode!,Read! )

if li_FileNum>0 then
       FileRead(li_FileNum,ls_temp)
     
       p=pos(ls_temp,'MAC Address = ')

       ls_mac=mid(ls_temp,p + 14,17)
     
//       if  ls_mac='\NetBT_Tcpip_{942′ then  ls_mac='其他设备'

       if ls_mac<>'其他设备' and trim(ls_mac) <> '' then
   
   messagebox("",ls_mac)
  
  end if
  
   FileClose(li_filenum)
       FileDelete("c:\"+as_ip+".txt")
   FileDelete("run.bat")

end if

原创粉丝点击