国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

獲取指定IP的終端的MAC地址

2024-07-21 02:16:03
字體:
供稿:網(wǎng)友



    因?yàn)闃I(yè)務(wù)需要,需要給公司部分終端進(jìn)行登記,以保證授權(quán)終端能夠登錄業(yè)務(wù)系統(tǒng),最好的方法就是記錄下每臺(tái)終端的mac地址來進(jìn)行驗(yàn)證是否有授權(quán)。

    下面是采用調(diào)用api的方式獲取指定ip的終端的mac地址:

  [dllimport("iphlpapi.dll")]
  public static extern int sendarp(int32 dest, int32 host, ref int64 mac, ref int32 length);
  //dest為目標(biāo)機(jī)器的ip;host為本機(jī)器的ip

  [dllimport("ws2_32.dll")]
  public static extern int32 inet_addr(string ip);

  public static string getnetcardaddress(string strip)
  {
   try
   {
    iphostentry host = dns.gethostbyname(system.environment.machinename);
    int32 local = inet_addr(host.addresslist[0].tostring());
    int32 remote = inet_addr(strip);

    int64 macinfo = new int64();
    int32 length = 6;
    sendarp(remote, local, ref macinfo, ref length);

    string temp = system.convert.tostring(macinfo, 16).padleft(12, '0').toupper();

    stringbuilder strreturn = new stringbuilder();
    int x = 12;
    for(int i=0;i<6;i++)
    {
     strreturn.append(temp.substring(x-2, 2));
     x -= 2;
    }

    return strreturn.tostring();
   }
   catch(exception error)
   {
    throw new exception(error.message);
   }
  }

    在上面的方式使用一段時(shí)間之后發(fā)現(xiàn)只能獲取到同一網(wǎng)段或沒有經(jīng)過任何路由的終端的mac地址,而對那些不同網(wǎng)段或經(jīng)過了路由的終端的mac地址則無法正常獲取到mac地址。下面的操作系統(tǒng)命令方式可以解決此問題:

  public static string getnetcardaddress2(string strip)
  {
   string mac = "";

   system.diagnostics.process process = new system.diagnostics.process();
   process.startinfo.filename = "nbtstat";
   process.startinfo.arguments = "-a "+strip;
   process.startinfo.useshellexecute = false;
   process.startinfo.createnowindow = true;
   process.startinfo.redirectstandardoutput = true;
 
   process.start();
 
   string output = process.standardoutput.readtoend();
   int length = output.indexof("mac address = ");

   if(length>0)
   {
    mac = output.substring(length+14, 17);
   }
 
   process.waitforexit();
 
   return mac.replace("-", "").trim();
  }
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 监利县| 涪陵区| 板桥市| 额尔古纳市| 安康市| 伊金霍洛旗| 临湘市| 得荣县| 邵阳县| 盐边县| 永吉县| 淅川县| 嘉黎县| 红安县| 乌兰察布市| 肥乡县| 衢州市| 砚山县| 渭南市| 黄石市| 鹤峰县| 乐亭县| 工布江达县| 山阳县| 远安县| 星座| 潞西市| 英超| 汉阴县| 共和县| 买车| 林甸县| 汉源县| 英山县| 司法| 扎鲁特旗| 兰溪市| 铁力市| 宜兴市| 南阳市| 凤台县|