说明:
Ping命令是个危险的命令,用它可以知道你的操作系统,IP等,为了安全禁Ping是个很好的方法,也是防DDOS攻击的。应该是有外部网络试图连接你的UDP的1434端口,不知道你打了补丁没有。
黑客入侵时,大多使用Ping命令来检测主机,如果Ping不通,水平差的“黑客”大多就会知难而退。事实上,完全可以造成一种假相,即使我们在线,但对方Ping时也不能相通,这样就能躲避很多攻击。
所以测试时,应确定以下两个方面:
1.确保Ping命令可用。
2.确认操作系统,不同系统,Ping命令的参数不一样。
未例代码如下:
Ping命令是个危险的命令,用它可以知道你的操作系统,IP等,为了安全禁Ping是个很好的方法,也是防DDOS攻击的。应该是有外部网络试图连接你的UDP的1434端口,不知道你打了补丁没有。
黑客入侵时,大多使用Ping命令来检测主机,如果Ping不通,水平差的“黑客”大多就会知难而退。事实上,完全可以造成一种假相,即使我们在线,但对方Ping时也不能相通,这样就能躲避很多攻击。
所以测试时,应确定以下两个方面:
1.确保Ping命令可用。
2.确认操作系统,不同系统,Ping命令的参数不一样。
未例代码如下:
/**
* 测试设备网络状态
*
* @return boolean
* @throws Exception
*/
private static boolean connectTest() throws Exception {
String OS = System.getProperty("os.name").toLowerCase();
int pingTimes = 4;
String pingCommand = "";
if (OS.indexOf("windows")>=0) {
pingCommand = "ping " + ip + " -n " + pingTimes;
} else {
pingCommand = "ping " + "-c " + pingTimes + " " + ip;
}
boolean status = isReachable(pingCommand, pingTimes);
if (!status) {
throw new Exception("设备不能连接(ping 不通!!!)");
}
return status;
}
/** * 测试是否能ping通
* * @param server
* * @param timeout
* * @return */
private static boolean isReachable(String pingCommand, int pingTimes) {
BufferedReader in = null;
Runtime r = Runtime.getRuntime();
try {
// 执行命令并获取输出
System.out.println(pingCommand);
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
// 逐行检查输出,计算类似出现=23ms TTL=62字样的次数
int connectedCount = 0;
String line = null;
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
}
// 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
return connectedCount == pingTimes;
} catch (Exception ex) {
ex.printStackTrace();
// 出现异常则返回假
return false;}
finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}