zoukankan      html  css  js  c++  java
  • java生成连续MAC地址

    有时候性能测试的时候需要用到设备的MAC地址,为避免MAC地址重复,那么就生成连续的MAC地址就不会重复了

    直接看代码吧

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.math.BigInteger;
    import java.util.regex.Pattern;
    
    public class ConsequentMAC {
    	public static String pattern = "[0-9a-fA-F]{12}";
    	public static String patternSn = "[0-9A-Z]{18}";
    	public static  void genPatchMAC (String initialMAC,int count,String filePath) {
    		String startMAC = initialMAC.replace(":", "");
    		boolean isMatch = Pattern.matches(pattern,startMAC);
    		if(isMatch == false) {
    			System.out.println(startMAC + "初始MAC地址非法");
    			System.exit(0);
    		}
    		try {
    			File file = new File(filePath);
    			FileWriter writer = new FileWriter(file, true);
    			BigInteger remoteMAC = new BigInteger(startMAC,16);
    			BigInteger increase = new BigInteger("1");
    			String resultMAC = "";
    			for(int i=0;i<count;i++) {
    				resultMAC = remoteMAC.toString(16).toUpperCase();
    				writer.write(formatMAC(resultMAC) + "
    ");
    				remoteMAC = remoteMAC.add(increase);
    			}
    			writer.close();
    		} catch (IOException e) {			
    			e.printStackTrace();
    		}
    	}	
    	
    	private static String formatMAC(String str) {
            StringBuilder temMAC = new StringBuilder("");
            for (int i = 1; i <= 12; i++) {
            	temMAC.append(str.charAt(i - 1));
                if (i % 2 == 0) {
                	temMAC.append(":");
                }
            }
            String MAC = temMAC.substring(0, 17);
            return MAC;
        }
    
    	public static void main(String[] args) {
    		String filePath = "wifi600w.dat";
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
    		genPatchMAC("70:00:00:00:00:00",6000000,filePath);
    		System.out.println("finished!");
    	}
    }
    

      

  • 相关阅读:
    抽象工厂模式
    观察者模式
    建造者模式
    外观模式
    drf 之路由
    drf之视图
    drf--请求和响应
    def--序列化
    drf之restful规范
    Tepora使用
  • 原文地址:https://www.cnblogs.com/mosicol/p/11317327.html
Copyright © 2011-2022 走看看