public static boolean pingIp(String ip) throws UnknownHostException, IOException {
//能ping通放回true 反之 false 超时时间 3000毫秒
return InetAddress.getByName(ip).isReachable(5000);
}
public static boolean telnetport(String ip, Integer port) throws IOException {
Socket connect = new Socket();
boolean res = false;
try {
connect.connect(new InetSocketAddress(ip, port), 1000);//建立连接
//能telnet通返回true,否则返回false
res = connect.isConnected();//通过现有方法查看连通状态
} catch (IOException e) {
e.printStackTrace();
System.out.println("false");//当连不通时,直接抛异常,异常捕获即可
} finally {
try {
connect.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("false");
}
}
return res;
}