实现的功能:
以某个MAC地址为起始基准,生成N个连续的MAC地址,保存到某个TXT文件当中(换行为分隔符)
genPatchMAC(String,int,String)
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 void genPactchMAC (String initialMAC,int count,String filePath) {
String startMAC = initialMAC.replace(":", "");
boolean isMatch = Pattern.matches(pattern,startMAC);
if(isMatch == false) {
System.out.println(startMAC + " 该MAC地址非法!请输入合法格式的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 = "mac.txt";
File file = new File(filePath);
if (file.exists()) {
file.delete();
}
genPactchMAC("cf:00:00:00:00:00",500,"mac500.txt");
System.out.println("finished!");
}
}