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

本文介绍了一种使用PB+API获取局域网内计算机的MAC地址、IP地址等信息的方法。通过调用NBTSTAT命令并解析输出结果,能够有效地收集局域网内的计算机信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

PROJECT = "wifidemo" VERSION = "1.0.0" --测试支持硬件:ESP32C3 --测试固件版本:LuatOS-SoC_V0003_ESP32C3[_USB].soc local sys = require "sys" require("sysplus") -- 尝试加载http模块 local http local success, err = pcall(require, "http") if success then http = http else -- 如果http模块加载失败,再尝试加载http2模块(假设存在http2模块) success, err = pcall(require, "http2") if success then http = http2 else log.error("main", "Failed to load http or http2 module: ", err) return end end local rtos_bsp = rtos.bsp() -- spi_id,pin_reset,pin_dc,pin_cs,pin_busy,mode function eink_pin() if rtos_bsp == "AIR101" then return 0, pin.PB03, pin.PB01, pin.PB04, pin.PB00 elseif rtos_bsp == "AIR103" then return 0, pin.PB03, pin.PB01, pin.PB04, pin.PB00 elseif rtos_bsp == "AIR105" then return 5, pin.PC12, pin.PE08, pin.PC14, pin.PE09 elseif rtos_bsp == "ESP32C3" then --(spi_id, pin_busy, pin_reset, pin_dc, pin_cs) return 2, 11, 10, 6, 7 elseif rtos_bsp == "ESP32S3" then return 2, 16, 15, 14, 13 elseif rtos_bsp == "EC618" then return 0, 1, 10, 8, 18 else log.info("main", "bsp not support") return end end --需要自行填写的东西 --wifi信息 local wifiName,wifiPassword = "**你的WIFI地址*****", "******你的WIFI密码******" --地区id,请前往https://api.luatos.org/luatos-calendar/v1/check-city/ 查询自己所在位置的id local location = "101010100" --北京 --天气接口信息,需要自己申请,具体参数请参考https://api.luatos.org/ 页面上的描述 --采用天气API:https://tianqiapi.com/ local appid,appsecret = "27*******","3wd******" local function connectWifi() log.info("wlan", "wlan_init:", wlan.init()) wlan.setMode(wlan.STATION) wlan.connect(wifiName,wifiPassword,1) -- 等待连上路由,此时还没获ip result, _ = sys.waitUntil("WLAN_STA_CONNECTED") log.info("wlan", "WLAN_STA_CONNECTED", result) -- 等到成功获ip就代表连上局域网了 result, data = sys.waitUntil("IP_READY") log.info("wlan", "IP_READY", result, data) end local function requestHttp() local code, headers, body = http.request("GET","http://apicn.luatos.org:23328/luatos-calendar/v1?mac=111&battery=10&location="..location.."&appid="..appid.."&appsecret="..appsecret).wait() if code == 200 then return body else log.info("http get failed",code, headers, body) sys.wait(500) return "" end end function refresh() log.info("refresh","start!") local data for i=1,5 do--重试最多五次 collectgarbage("collect") data = requestHttp() collectgarbage("collect") if #data > 100 then break end log.info("load fail","retry!") end if #data < 100 then log.info("load fail","exit!") return end collectgarbage("collect") local spi_id, pin_busy, pin_reset, pin_dc, pin_cs = eink_pin() local mode =0 if spi_id then eink.model(eink.MODEL_1in54) spi.setup(spi_id,nil,0,0,8,20*1000*1000) eink.setup(mode, spi_id,pin_busy,pin_reset,pin_dc,pin_cs) eink.setWin(200, 200,2) --稍微等一会,免得墨水屏没初始化完成 sys.wait(100) eink.clear(1) log.info("eink", "end setup") eink.drawXbm(0, 0, 200, 200, data) -- 刷屏幕 eink.show() eink.sleep() log.info("refresh","done") end end sys.taskInit(function() --先连wifi connectWifi() while true do refresh() sys.wait(3600*1000)--一小时刷新一次吧 end end) -- 用户代码已结束--------------------------------------------- -- 结尾总是这一句 sys.run() -- sys.run()之后后面不要加任何语句!!!!! 这个脚本
最新发布
03-31
在用pb开发信息管理系统时,为了安全期间,我们希望记录每个登录用户的信息,并生成日志,其中最重要的是知道是哪个机器运行程序的?这样,只要获运行程序的机器ip问题就解决了,然而,pb中没有象delphi 中有现成的函数可以很方便的获的机器的ip,我们只好利用外部函数来实现了。 首先声明外部函数 function int WSAStartup( uint UIVersionRequested, ref s_WSAData lpWSAData ) library "wsock32.dll" function int WSACleanup() library "wsock32.dll" function int WSAGetLastError ( ) library "wsock32.dll" function int gethostname ( ref string name, int namelen ) library "wsock32.dll" function string GetHost(string lpszhost, ref blob lpszaddress ) library "PBWS32.DLL" 然后在form的open事件中执行如下代码就可以了 s_wsadata l_WSAData int li_version = 257 blob{4} lb_hostaddress IF wsastartup ( li_version, l_WSAData ) = 0 THEN IF gethostname ( ls_HostName, len(ls_HostName) ) < 0 THEN messagebox("GetHostName",WSAGetLastError()) ELSE GetHost(ls_HostName, lb_HostAddress) //获得主机IP地址 ls_IpAddress = string(asc(string(blobmid(lb_HostAddress,1,1))),"000") + "." ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,2,1))),"000") + "." ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,3,1))),"000") + "." ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,4,1))),"000") //将IP地址的ASC码形式转化为***.***.***.***字符串格式 END IF WSACleanup() ELSE messagebox("GetHostName",WSAGetLastError()) END IF
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值