Ping

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('/', '\\');
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值