Pessoal,
eu to fazendo um sistema de monitoramento tipo nagios, zabbix e etc
p/ meu tcc, mas todo em java
e por causa disso, eu preciso fazer o ping(isreachable,…) e o dns(dnsjava)
mas eu não estou conseguindo fazer o ping funcionar, embora o dnsjava funcione
o interessante é que eu já tentei rodar com sudo e como root direto, além de
colocar o SUID e todos os arquivos do jdk via chmod, mas isso também não resolveu
o problema, eu também já desabilitei o iptables p/ ele não filtrar os pacotes, mas nada
netto@black-dev:~$ uname -a
Linux black-dev 2.6.31-21-generic-pae #59-Ubuntu SMP Wed Mar 24 08:47:55 UTC 2010 i686 GNU/Linux
netto@black-dev:~$ sudo iptables -L
[sudo] password for netto:
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
netto@black-dev:~$
netto@black-dev:~$ java -version
java version “1.6.0_20”
Java™ SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot™ Server VM (build 16.3-b01, mixed mode)
ide: netbeans 6.8
a minha classe netutils:
package net.sf.exdev.jmon2;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.IOException;
import org.xbill.DNS.*;
public class NetUtils {
private static void Lookup(String s) {
InetAddress thisComputer;
byte[] address;
try {
thisComputer = InetAddress.getByName(s);
address = thisComputer.getAddress();
System.out.println(thisComputer.toString());
} catch (UnknownHostException ue) {
System.out.println("Cannot find host " + s);
return;
}
if (isHostname(s)) {
for (int i = 0; i < address.length; i++) {
int unsignedByte = address[i] < 0 ? address[i] + 256 : address[i];
System.out.print(unsignedByte + ".");
}
System.out.println();
System.out.println(" --- ");
} else {
try {
System.out.println(InetAddress.getByName(s));
} catch (UnknownHostException e) {
System.out.println("Could not lookup the address " + s);
}
}
}
public static String reverseLookup(String hostIp) throws IOException {
Record opt = null;
Resolver res = new ExtendedResolver();
Name name = ReverseMap.fromAddress(hostIp);
int type = Type.PTR;
int dclass = DClass.IN;
Record rec = Record.newRecord(name, type, dclass);
Message query = Message.newQuery(rec);
Message response = res.send(query);
Record[] answers = response.getSectionArray(Section.ANSWER);
if (answers.length == 0) {
return hostIp;
} else {
return answers[0].rdataToString();
}
}
private static boolean isHostname(String s) {
char[] ca = s.toCharArray();
for (int i = 0; i < ca.length; i++) {
if (!Character.isDigit(ca[i])) {
if (ca[i] != '.') {
return true;
}
}
}
return false;
}
private static void ping(String host) {
try {
// 5s - 5000
if (InetAddress.getByName(host).isReachable(500)) {
System.out.println("Ping OK: " + host);
} else {
System.out.println("Ping FALHOU: " + host);
}
} catch (Exception e) {
System.err.println("Ping FALHOU: " + host + " - " + e);
}
}
public static void main(String[] args) throws Exception {
Lookup("google.com");
Lookup("72.14.204.147");
System.out.println("-------");
System.out.println(reverseLookup("72.14.204.147"));
System.out.println(reverseLookup("200.147.35.142"));
System.out.println(reverseLookup("64.233.163.104"));
ping("uol.com.br");
ping("72.14.204.147");
}
}
btw, também aceito sugestões de como otimizar o código
Inté,
Geraldo