由于是教育网,时刻要注意不要走收费流量,所以路由表一定要及时更新。我们学校的免费IP在http://hitsun.hit.edu.cn/zxiplist.htm上提供,要把这些地址转换成类似“*.*.*.*/24 via *.*.*.*”的形式,其实就是一个字符串处理程序,注意合适的使用String类中的split方法。
java 代码
- package netManage;
- import java.io.*;
- public class FreeIpGenerator {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception{
- // TODO Auto-generated method stub
- FileReader fr = new FileReader("freeip.txt");
- File f = new File("routetable.txt");
- if(f.exists()){
- f.delete();
- }
- f.createNewFile();
- FileWriter fw = new FileWriter(f);
- BufferedReader br = new BufferedReader(fr);
- BufferedWriter bw = new BufferedWriter(fw);
- String line;
- while((line = br.readLine())!= null){
- if(line.length()!= 0){
- String[] words = line.split(" +");
- bw.write(words[0]+"/");
- bw.write(Integer.toString(getNumber(words[2])));
- bw.write(" via 202.118.242.30\n");
- }
- }
- bw.close();
- br.close();
- fw.close();
- fr.close();
- }
- //from the ip mask get the real number
- //e.g 255.255.255.0->24
- public static int getNumber(String ipmask){
- String[] ips = ipmask.split("\\.");
- int value = 0;
- for(String ip:ips){
- //System.out.print(Integer.getInteger(ip.substring(0,ip.length()-2)));
- value = value + Integer.bitCount(Integer.decode(ip));
- }
- return value;
- }
- }