NetBIOS是网络基本输入输出系统,通过netbios协议,可以得到局域网内的主机信息
在windows下可以使用nbtstat命令查看netbios信息
nbtstat -A 192.168.0.103
向指内网指定地址发送netbios协议数据包,可以得到局域网内机器的计算机名,工作组,MAC地址
代码如下:
#include <stdio.h>#include <afx.h>#include <WinSock2.h>#pragma commentlib,”ws2_32.lib”)//数据包BYTE bs[50]={0x0,0x00,0x0,0x10,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x20,0x43,0x4b,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x0,0x0,0x21,0x0,0x1};WSADATA wsadata;SOCKET m_Socket;SOCKADDR_IN serveraddr;int port=137; //netbios协议端口}void main){//初始化winsock版本WORD ver=MAKEWORD2,2);WSAStartupver,&wsadata);//初始socket地址信息char strIP[20] = {“192.168.153.1″};m_Socket = socketAF_INET, SOCK_DGRAM,IPPROTO_UDP);serveraddr.sin_family = AF_INET;serveraddr.sin_port = htonsport);serveraddr.sin_addr.S_un.S_addr = inet_addrstrIP);//发送UDP数据包DWORD len = sendtom_Socket,char*)bs,sizeofbs),0,sockaddr*)&serveraddr,sizeofserveraddr));if len < 0){printf”send udp error”);}//接受返回的数据包BYTE Buf[512] = {0};recvfromm_Socket,char*)Buf,sizeofBuf),0,NULL,NULL);printf”IP is %s\n”,strIP);//关闭socket连接closesocketm_Socket);WSACleanup);//处理数据包里的信息CString str,strHost,strHex,strMac,Host,Group,User;int i;strHost=””; //机器名字strHex=””;//MAC地址User=”?”;//Host=”\\”;int tem=0,num=0;bool bAdd=true;//根据数据报规则取出相应的信息fori=57;i<500;i++) //57-72{//ifBuf[i]==0xcc)//break; if num>3)break; ifBuf[i]==0x20)bAdd=false;ifbAdd){str.Format”%c”,Buf[i]);ifBuf[i]>=’ ‘)strHost+=str;str.Format”%02x.”,Buf[i]);strHex+=str;}if++tem)%18==0){ bAdd=true; strHost.TrimRightchar)NULL);ifstrHost==””){strMac.Delete17,strMac.GetLength)-17);printf”MAC is %s\n”,strMac);num++;break;}ifnum==0&&strHost!=””){printf”Host is %s\n”,strHost);Host=strHost;num++;}else{ifHost!=strHost && num==1&& strHost!=””){printf”Host is %s\n”,strHost);Group=strHost;num++;}else {ifstrHost!=Host&&strHost!=Group&&num==2&&strHost!=””){User=strHost;ifUser!=”__MSBROWSE__”){printf”user is %s\n”,User);num++;}}}}strMac=strHex;strHost=””;strHex=””;}}}