Ping.java
public class Ping {
public Ping() {
}
public static String exec(String ip, int count) throws Exception {
String result = "";
String pings = null;
if (count > 30) {
count = 30;
}
OSInfo osinfo = OSInfo.getOSInfo();
Proc proc = new Proc();
proc.setDebug(true);
proc.setChildText("ping");
if (osinfo.isWin()) {
pings = "ping " + ip + " -n " + count;
} else if (osinfo.isLinux()) {
if (ip.endsWith("255")) {
pings = "ping " + ip + " -b -c " + count;
} else {
pings = "ping " + ip + " -c " + count;
}
}
proc.exec(pings);
result = proc.getOutput();
Boolean status = result.contains("100%");
if (status) {
result = "ping失败!";
} else {
result = "ping成功!";
}
proc.stop();
return result;
}
public static void main(String[] arg) throws Exception {
System.out.println(exec("192.168.1.252", 3));
}
}
OSInfo.java
import java.io.File;
import java.io.Serializable;
import java.net.InetAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OSInfo implements Serializable {
static Logger logger = LoggerFactory.getLogger(OSInfo.class);
private static OSInfo mOSInfo;
private boolean mIsNT4;
private boolean mIsW2K;
private boolean mIsWin;
private boolean mIsSolaris;
private boolean mIsAIX;
private boolean mIsMacintosh;
private boolean mIsLinux;
private String mHostName;
private String osType;
private String mLineSeparator;
private String mPathSeparator;
private String mFileSeparator;
private char mPathSeparatorChar;
private char mFileSeparatorChar;
public static OSInfo getOSInfo() {
if (mOSInfo == null) {
mOSInfo = new OSInfo();
}
return mOSInfo;
}
private OSInfo() {
String osType = System.getProperty("os.name").toLowerCase();
this.mIsWin = -1 != osType.indexOf("windows");
this.mIsW2K = osType.equals("windows 2000");
this.mIsNT4 = osType.equals("windows nt");
this.mIsSolaris = osType.equals("solaris") || osType.equals("sunos");
this.mIsAIX = osType.equals("aix");
this.mIsMacintosh = osType.startsWith("mac");
this.mIsLinux = osType.startsWith("linux");
this.osType = osType;
this.mLineSeparator = System.getProperty("line.separator");
this.mPathSeparator = File.pathSeparator;
this.mFileSeparator = File.separator;
this.mFileSeparatorChar = File.separatorChar;
this.mPathSeparatorChar = File.pathSeparatorChar;
try {
InetAddress myaddr = InetAddress.getByName(InetAddress.getLocalHost().getHostAddress());
this.mHostName = myaddr.getHostName();
} catch (Exception var3) {
logger.error("error:" + var3);
}
}
public boolean isW2K() {
return this.mIsW2K;
}
public boolean isNT4() {
return this.mIsNT4;
}
public boolean isSolaris() {
return this.mIsSolaris;
}
public boolean isAIX() {
return this.mIsAIX;
}
public boolean isMacintosh() {
return this.mIsMacintosh;
}
public boolean isWin() {
return this.mIsWin;
}
public boolean isLinux() {
return this.mIsLinux;
}
public String getHostName() {
return this.mHostName;
}
public String getOsType() {
return this.osType;
}
public String getNonQualifiedHostName(String hostName) {
int idx = hostName.indexOf(".");
if (idx > 0) {
hostName = hostName.substring(0, idx);
}
return hostName;
}
public String getLineSeparator() {
return this.mLineSeparator;
}
public String getPathSeparator() {
return this.mPathSeparator;
}
public char getPathSeparatorChar() {
return this.mPathSeparator.charAt(0);
}
public String getFileSeparator() {
return this.mFileSeparator;
}
public char getFileSeparatorChar() {
return this.mFileSeparator.charAt(0);
}
public String getNormalizedPath(String path) {
return this.getFileSeparator().charAt(0) == '/' ? path.replace('\\', '/') : path.replace('/', '\\');
}