如何使用MAC地址识别计算机?

时间:2014-06-28 06:50:34

标签: java

我已使用此代码获取系统的MAC地址: -

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class App{

public static void main(String[] args){

InetAddress ip;
try {

    ip = InetAddress.getLocalHost();
    //System.out.println("Current IP address : " + ip.getHostAddress());

    NetworkInterface network = NetworkInterface.getByInetAddress(ip);

    byte[] mac = network.getHardwareAddress();

    System.out.print("Current MAC address : ");

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
    }
    System.out.println(sb.toString());

} catch (UnknownHostException e) {

    e.printStackTrace();

} catch (SocketException e){

    e.printStackTrace();

  }

  }

}

所以我的问题很少: -

  1. 如何将获取的MAC地址保存到文本文件中?
  2. 如何使用该文本文件(即MAC地址)来识别设备?

    提前致谢:)

2 个答案:

答案 0 :(得分:2)

  

如何将获取的MAC地址保存到文本文件中?

您已经有了在问题中执行此操作的代码。您需要做的就是写入文件而不是标准输出。 (如果你不知道该怎么做......请阅读Lesson: Basic I/O。)

  

如何使用该文本文件(即MAC地址)来识别设备?

将其读入一个String(参见上面的链接),并将String与您以同样方式创建的另一个String进行比较。


请注意,大多数系统都允许系统管理员更改NIC的MAC地址。因此,基于MAC地址检查的反盗版措施很容易被打败。

答案 1 :(得分:0)

public class MacAddress
{
   public static void Main(string[] argv)
   {
      int commlength, miblength, datastart, datalength;
      string nextmib, value;
      SNMP conn = new SNMP();
      string mib = "1.3.6.1.2.1.17.4.3.1.1";
      int orgmiblength = mib.Length;
      byte[] response = new byte[1024];

      nextmib = mib;

      while (true)
      {
         response = conn.get("getnext", argv[0], argv[1], nextmib);
         commlength = Convert.ToInt16(response[6]);
         miblength = Convert.ToInt16(response[23 + commlength]);
         datalength = Convert.ToInt16(response[25 + commlength + miblength]);
         datastart = 26 + commlength + miblength;
         value = BitConverter.ToString(response, datastart, datalength);
         nextmib = conn.getnextMIB(response);
         if (!(nextmib.Substring(0, orgmiblength) == mib))
            break;

         Console.WriteLine("{0} = {1}", nextmib, value);
      }
   }
}


class SNMP
{
   public SNMP()
   {

   }

   public byte[] get(string request, string host, string community, string mibstring)
   {
      byte[] packet = new byte[1024];
      byte[] mib = new byte[1024];
      int snmplen;
      int comlen = community.Length;
      string[] mibvals = mibstring.Split('.');
      int miblen = mibvals.Length;
      int cnt = 0, temp, i;
      int orgmiblen = miblen;
      int pos = 0;

      // Convert the string MIB into a byte array of integer values
      // Unfortunately, values over 128 require multiple bytes
      // which also increases the MIB length
      for (i = 0; i < orgmiblen; i++)
      {
         temp = Convert.ToInt16(mibvals[i]);
         if (temp > 127)
         {
            mib[cnt] = Convert.ToByte(128 + (temp / 128));
            mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
            cnt += 2;
            miblen++;
         } else
         {
            mib[cnt] = Convert.ToByte(temp);
            cnt++;
         }
      }
      snmplen = 29 + comlen + miblen - 1;  //Length of entire SNMP packet

      //The SNMP sequence start
      packet[pos++] = 0x30; //Sequence start
      packet[pos++] = Convert.ToByte(snmplen - 2);  //sequence size

      //SNMP version
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x01; //length
      packet[pos++] = 0x00; //SNMP version 1

      //Community name
      packet[pos++] = 0x04; // String type
      packet[pos++] = Convert.ToByte(comlen); //length
      //Convert community name to byte array
      byte[] data = Encoding.ASCII.GetBytes(community);
      for (i = 0; i < data.Length; i++)
      {
         packet[pos++] = data[i];
      }

      //Add GetRequest or GetNextRequest value
      if (request == "get")
         packet[pos++] = 0xA0;
      else
         packet[pos++] = 0xA1;

      packet[pos++] = Convert.ToByte(20 + miblen - 1); //Size of total MIB

      //Request ID
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x04; //length
      packet[pos++] = 0x00; //SNMP request ID
      packet[pos++] = 0x00;
      packet[pos++] = 0x00;
      packet[pos++] = 0x01;

      //Error status
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x01; //length
      packet[pos++] = 0x00; //SNMP error status

      //Error index
      packet[pos++] = 0x02; //Integer type
      packet[pos++] = 0x01; //length
      packet[pos++] = 0x00; //SNMP error index

      //Start of variable bindings
      packet[pos++] = 0x30; //Start of variable bindings sequence

      packet[pos++] = Convert.ToByte(6 + miblen - 1); // Size of variable binding

      packet[pos++] = 0x30; //Start of first variable bindings sequence
      packet[pos++] = Convert.ToByte(6 + miblen - 1 - 2); // size
      packet[pos++] = 0x06; //Object type
      packet[pos++] = Convert.ToByte(miblen - 1); //length

      //Start of MIB
      packet[pos++] = 0x2b;
      //Place MIB array in packet
      for(i = 2; i < miblen; i++)
         packet[pos++] = Convert.ToByte(mib[i]);
      packet[pos++] = 0x05; //Null object value
      packet[pos++] = 0x00; //Null

      //Send packet to destination
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                       ProtocolType.Udp);
      sock.SetSocketOption(SocketOptionLevel.Socket,
                      SocketOptionName.ReceiveTimeout, 5000);
      IPHostEntry ihe = Dns.Resolve(host);
      IPEndPoint iep = new IPEndPoint(ihe.AddressList[0], 161);
      EndPoint ep = (EndPoint)iep;
      sock.SendTo(packet, snmplen, SocketFlags.None, iep);

      //Receive response from packet
      try
      {
         int recv = sock.ReceiveFrom(packet, ref ep);
      } catch (SocketException)
      {
         packet[0] = 0xff;
      }
      return packet;
   }

   public string getnextMIB(byte[] mibin)
   {
      string output = "1.3";
      int commlength = mibin[6];
      int mibstart = 6 + commlength + 17; //find the start of the mib section
      //The MIB length is the length defined in the SNMP packet
     // minus 1 to remove the ending .0, which is not used
      int miblength = mibin[mibstart] - 1;
      mibstart += 2; //skip over the length and 0x2b values
      int mibvalue;

      for(int i = mibstart; i < mibstart + miblength; i++)
      {
         mibvalue = Convert.ToInt16(mibin[i]);
         if (mibvalue > 128)
         {
            mibvalue = (mibvalue/128)*128 + Convert.ToInt16(mibin[i+1]);
            i++;
         }
         output += "." + mibvalue;
      }
      return output;
   }
}