importorg.springframework.data.web.JsonPath;importjava.io.IOException;import java.net.*;public classPTUtil {/***
* ping操作
*@paramhostname
*@paramtimeout in milliseconds
*@return
*/
public staticJsonResult pingResult(String hostname,Integer timeout){
JsonResult jsonResult= newJsonResult();try{
InetAddress address=InetAddress.getByName(hostname);boolean flag =address.isReachable(timeout);if(flag){
jsonResult.setMessage("ping结果:the address is reachable.");
}else{
jsonResult.setCode(Constants.ResultCode.EXCEPTION);
jsonResult.setMessage("ping结果:the address is unreachable.");
}
}catch(UnknownHostException e) {
jsonResult.setCode(Constants.ResultCode.EXCEPTION);
jsonResult.setMessage("ping结果:UnknownHostException:"+e.getMessage());
}catch(IOException e) {
jsonResult.setCode(Constants.ResultCode.EXCEPTION);
jsonResult.setMessage("ping结果:IOException:"+e.getMessage());
}returnjsonResult;
}/***
* telnet 操作
*@paramhostname
*@paramtimeout in milliseconds
*@return
*/
public staticJsonResult telnetResult(String hostname,Integer port,Integer timeout){
JsonResult jsonResult= newJsonResult();try{
Socket server= newSocket();
InetSocketAddress address= newInetSocketAddress(hostname,port);
server.connect(address, timeout);
server.close();
jsonResult.setMessage("telnet结果:success!");
}catch(UnknownHostException e) {
jsonResult.setCode(Constants.ResultCode.EXCEPTION);
jsonResult.setMessage("telnet结果:UnknownHostException:"+e.getMessage());
}catch(IOException e) {
jsonResult.setCode(Constants.ResultCode.EXCEPTION);
jsonResult.setMessage("telnet结果:IOException:"+e.getMessage());
}returnjsonResult;
}
}