// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExternIP {
    public static String getIP() throws Exception {
        URLConnection uc = (new URL("http://www.myip.dk/")).openConnection();
        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        StringBuilder sb = new StringBuilder("");
        String line;
        while((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        Pattern p = Pattern.compile("(?:<title>Your IP: )(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:</title>)");
        Matcher m = p.matcher(sb.toString());
        m.find();
        return m.group(1);
    }
    public static void main(String[] args) throws Exception {
        System.out.println(ExternIP.getIP());
    }
}
