zoukankan      html  css  js  c++  java
  • 使用java 执行ping命令

     

    借助 Runtime.getRuntime().exec() 可以运行一个windows的exe程序
    如图,使用java运行 ping 192.168.2.106,返回这样的字符串
    使用java 执行ping命令
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package socket;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class TestSocket {
     
        public static void main(String[] args) throws IOException {
     
            Process p = Runtime.getRuntime().exec("ping " "192.168.2.106");
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                if (line.length() != 0)
                    sb.append(line + " ");
            }
            System.out.println("本次指令返回的消息是:");
            System.out.println(sb.toString());
        }
     
    }
  • 相关阅读:
    python——(os, shutil)
    python-(subprocess, commands)
    PHP设计模式二:单例模式
    PHP设计模式一:工厂方法设计模式
    PHP垃圾回收机制
    PHP异常处理机制
    超文本传送协议HTTP
    IP地址
    Linux系统网络基本配置
    Linux系统LVM基本使用
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10194470.html
Copyright © 2011-2022 走看看