// Are you after a reverse lookup for a name?
String x = "";
if (!returnIP && IP4_ADDRESS.matcher(site).matches()) {
x = "-x ";
}
Process p = new Process("dig +short " + x + site);
p.run();
p.waitFor(5000); // this should be fast -- 5 seconds allows aeons of
// time
String out = p.getOutput();
// look for an IPv4 address?
if (returnIP) {
Matcher m = IP4_ADDRESS.matcher(out);
if (m.find())
return m.group();
throw new FailureException("Couldn't find IP address for " + site
+ " in " + out);
}
// look for a name
String[] bits = StrUtils.splitLines(out);
String ip = null;
for (String string : bits) {
if (string.isEmpty()) {
continue;
}
if (IP4_ADDRESS.matcher(string).matches()) {
ip = string;
continue;
}
if (string.endsWith(".")) {
string = string.substring(0, string.length() - 1);
}
return string;
}
// try a reverse lookup
if (ip == null)
throw new FailureException("Couldn't find server name or ip for "
+ site + " in [" + out + "] " + p.getError());
return dig(ip, false);
}