JSP Get customer service MAC address __jsp

Source: Internet
Author: User
Tags stringbuffer
Package com.sun.servlet;
Import java.io.IOException;
Import Java.net.DatagramPacket;
Import Java.net.DatagramSocket;
Import java.net.InetAddress;
 /** * Host A sends a "UDP-NETBIOS-NS" query packet to Host B, that is, to host B's 137 port and sends a query packet to ask Host B's NetBIOS names information. * Second, Host B receives the "Udp-netbios-ns" query package, assuming Host B correctly installs the NetBIOS service ...
 And the 137 port is open, Host B sends a "UDP-NETBIOS-NS" reply packet to host A, that is, send answer package to host a. * and using UDP (NetBIOS Name Service) To quickly obtain a remote host MAC address * * @author windy/public class Udpgetclientmacaddr {private Strin
	G Sremoteaddr;
	private int iremoteport=137;
	Private byte[] buffer = new byte[1024];

	Private Datagramsocket Ds=null;
		Public udpgetclientmacaddr (String straddr) throws exception{sremoteaddr = straddr;
	ds = new Datagramsocket (); } Protected final Datagrampacket send (final byte[] bytes) throws IOException {Datagrampacket dp = new Datagrampacket (
		Bytes,bytes.length,inetaddress.getbyname (SREMOTEADDR), iremoteport);
		Ds.send (DP);
	return DP; } protected final Datagrampacket receive () throws Exception {DatagramPacket DP = new Datagrampacket (buffer,buffer.length);
		Ds.receive (DP);
	return DP;
		} protected byte[] Getquerycmd () throws Exception {byte[] T_ns = new BYTE[50];
		T_ns[0] = 0x00;
		T_NS[1] = 0x00;
		T_NS[2] = 0x00;
		T_NS[3] = 0x10;
		T_NS[4] = 0x00;
		T_NS[5] = 0x01;
		T_NS[6] = 0x00;
		T_NS[7] = 0x00;
		T_NS[8] = 0x00;
		T_NS[9] = 0x00;
		T_NS[10] = 0x00;
		T_NS[11] = 0x00;
		T_NS[12] = 0x20;
		T_NS[13] = 0x43;

		T_NS[14] = 0x4b;
		for (int i =; I < i++) {t_ns[i] = 0x41;
		} t_ns[45] = 0x00;
		T_NS[46] = 0x00;
		T_NS[47] = 0x21;
		T_NS[48] = 0x00;
		T_NS[49] = 0x01;
	return T_ns;
		} Protected Final String getmacaddr (byte[] brevdata) throws Exception {//Get computer name int i = brevdata[56] * 18 + 56;
		String saddr= "";
		StringBuffer sb = new StringBuffer (17); Read the number of Names (NetBIOS names, each of which is 18 bytes per NetBIOS Names)//And then calculate the position of the "Unit ID" field from the 56th byte position =56+number of NAMESX1
		8, and finally from this location to read a continuous 6 bytes, is the destination host's MAC address. for (int j = 1; J < 7;j++) {saddr = inteGer.tohexstring (0xFF & Brevdata[i+j]);
			if (Saddr.length () < 2) {sb.append (0);
			} sb.append (Saddr.touppercase ());
		if (J < 6) sb.append (': ');
	return sb.tostring ();
		Public final void Close () {try {ds.close ();
		catch (Exception ex) {ex.printstacktrace ();
		} Public final String getremotemacaddr () throws Exception {byte[] bqcmd = Getquerycmd ();
		Send (Bqcmd);
		Datagrampacket DP = receive ();
		String SMAC = getmacaddr (Dp.getdata ());

		Close ();
	return SMAC; }
}


Jsp:

<span style= "color: #000000;" ><%
				String smac = "";
				String sip = Request.getheader ("x-forwarded-for");
				if (sip = = NULL | | sip.length () = 0
						| | "Unknown". Equalsignorecase (SIP)) {
					sip = Request.getheader ("Proxy-client-ip");
				}
				if (sip = = NULL | | sip.length () = 0
						| | "Unknown". Equalsignorecase (SIP)) {
					sip = Request.getheader ("Wl-proxy-client-ip");
				}
				if (sip = = NULL | | sip.length () = 0
						| | "Unknown". Equalsignorecase (SIP)) {
					sip = request.getremoteaddr ();
				}
				Udpgetclientmacaddr Umac = new udpgetclientmacaddr (SIP);
				SMAC = Umac. Getremotemacaddr ();
				Session.setattribute ("Smac", SMAC);
			%></span>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.