|
@@ -5,14 +5,34 @@ import eu.bitwalker.useragentutils.Browser;
|
|
|
import eu.bitwalker.useragentutils.OperatingSystem;
|
|
|
import eu.bitwalker.useragentutils.UserAgent;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.LineNumberReader;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.NetworkInterface;
|
|
|
+import java.net.SocketException;
|
|
|
+import java.net.UnknownHostException;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
public class ApiWebMain
|
|
|
{
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
+ public static void main(String[] args)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ System.out.print(getMac("47.95.197.36"));
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void userAgent()
|
|
|
+ {
|
|
|
String userAgentStr = "ser-agent=Mozilla/5.0 (Linux; Android 5.0.2; sdk_google_atv_x86 Build/LSY64) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36";
|
|
|
UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
|
|
|
//获取浏览器对象
|
|
@@ -23,14 +43,14 @@ public class ApiWebMain
|
|
|
//操作系统
|
|
|
String osName = operatingSystem.getName();
|
|
|
//操作系统设备类型
|
|
|
- System.out.println("操作系统名:"+operatingSystem.getName());
|
|
|
- System.out.println("访问设备类型:"+operatingSystem.getDeviceType());
|
|
|
- System.out.println("操作系统家族:"+operatingSystem.getGroup());
|
|
|
- System.out.println("操作系统生产厂商:"+operatingSystem.getManufacturer());
|
|
|
+ System.out.println("操作系统名:" + operatingSystem.getName());
|
|
|
+ System.out.println("访问设备类型:" + operatingSystem.getDeviceType());
|
|
|
+ System.out.println("操作系统家族:" + operatingSystem.getGroup());
|
|
|
+ System.out.println("操作系统生产厂商:" + operatingSystem.getManufacturer());
|
|
|
|
|
|
- String eviceType = operatingSystem.getDeviceType().toString();
|
|
|
+ String eviceType = operatingSystem.getDeviceType().toString();
|
|
|
|
|
|
- if("MOBILE".equals(eviceType))
|
|
|
+ if ("MOBILE".equals(eviceType))
|
|
|
{
|
|
|
Pattern pattern = Pattern.compile(";\\s?(\\S*?\\s?\\S*?)\\s?(Build)?/");
|
|
|
Matcher matcher = pattern.matcher(userAgentStr);
|
|
@@ -38,15 +58,118 @@ public class ApiWebMain
|
|
|
if (matcher.find())
|
|
|
{
|
|
|
model = matcher.group(1).trim();
|
|
|
- System.out.println("操作系统生产厂商:"+model);
|
|
|
+ System.out.println("操作系统生产厂商:" + model);
|
|
|
}
|
|
|
}
|
|
|
- else if("WINDOWS".equals(eviceType))
|
|
|
+ else if ("WINDOWS".equals(eviceType))
|
|
|
{
|
|
|
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据IP地址获取mac地址
|
|
|
+ *
|
|
|
+ * @param ipAddress 127.0.0.1
|
|
|
+ * @return
|
|
|
+ * @throws SocketException
|
|
|
+ * @throws UnknownHostException
|
|
|
+ */
|
|
|
+ public static String getLocalMac(String ipAddress) throws SocketException,
|
|
|
+ UnknownHostException
|
|
|
+ {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ String str = "";
|
|
|
+ String macAddress = "";
|
|
|
+ final String LOOPBACK_ADDRESS = "127.0.0.1";
|
|
|
+ // 如果为127.0.0.1,则获取本地MAC地址。
|
|
|
+ if (LOOPBACK_ADDRESS.equals(ipAddress))
|
|
|
+ {
|
|
|
+ InetAddress inetAddress = InetAddress.getLocalHost();
|
|
|
+ // 貌似此方法需要JDK1.6。
|
|
|
+ byte[] mac = NetworkInterface.getByInetAddress(inetAddress)
|
|
|
+ .getHardwareAddress();
|
|
|
+ // 下面代码是把mac地址拼装成String
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < mac.length; i++)
|
|
|
+ {
|
|
|
+ if (i != 0)
|
|
|
+ {
|
|
|
+ sb.append("-");
|
|
|
+ }
|
|
|
+ // mac[i] & 0xFF 是为了把byte转化为正整数
|
|
|
+ String s = Integer.toHexString(mac[i] & 0xFF);
|
|
|
+ sb.append(s.length() == 1 ? 0 + s : s);
|
|
|
+ }
|
|
|
+ // 把字符串所有小写字母改为大写成为正规的mac地址并返回
|
|
|
+ macAddress = sb.toString().trim().toUpperCase();
|
|
|
+ return macAddress;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 获取非本地IP的MAC地址
|
|
|
+ try
|
|
|
+ {
|
|
|
+ System.out.println(ipAddress);
|
|
|
+ Process p = Runtime.getRuntime().exec("nbtstat -A " + ipAddress);
|
|
|
+ System.out.println("===process==" + p);
|
|
|
+ InputStreamReader ir = new InputStreamReader(p.getInputStream());
|
|
|
|
|
|
+ BufferedReader br = new BufferedReader(ir);
|
|
|
+
|
|
|
+ while ((str = br.readLine()) != null)
|
|
|
+ {
|
|
|
+ if (str.indexOf("MAC") > 1)
|
|
|
+ {
|
|
|
+ macAddress = str.substring(str.indexOf("MAC") + 9, str.length());
|
|
|
+ macAddress = macAddress.trim();
|
|
|
+ System.out.println("macAddress:" + macAddress);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ p.destroy();
|
|
|
+ br.close();
|
|
|
+ ir.close();
|
|
|
+ }
|
|
|
+ catch (IOException ex)
|
|
|
+ {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ return macAddress;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
-}
|
|
|
+ public static String getMac(String ip) throws IOException {
|
|
|
+ String mac = "not found!";
|
|
|
+ if (ip != null) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ Process process = Runtime.getRuntime().exec("arp "+ip);
|
|
|
+ InputStreamReader ir = new InputStreamReader(process.getInputStream());
|
|
|
+ LineNumberReader input = new LineNumberReader(ir);
|
|
|
+ String line;
|
|
|
+ StringBuffer s = new StringBuffer();
|
|
|
+ while ((line = input.readLine()) != null) {
|
|
|
+ s.append(line);
|
|
|
+
|
|
|
+ }
|
|
|
+ mac = s.toString();
|
|
|
+ if (mac != null) {
|
|
|
+
|
|
|
+ mac = mac.substring(mac.indexOf(":") - 2, mac.lastIndexOf(":") + 3);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ mac = "not found!";
|
|
|
+ }
|
|
|
+ return mac;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return mac;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|