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) + "\n");
				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!");
	}
}

  

posted @ 2019-08-07 19:40  Mosicol  Views(1051)  Comments(0Edit  收藏  举报