Java 为什么不返回IP地址?

Java 为什么不返回IP地址?,java,runtime,Java,Runtime,对于ip,我无法让它返回除null之外的任何内容。我一定是在格式化字符串数组中的操作时遗漏了什么,请帮助!另外,是否有更好的sdk用于Java中的命令行工作Update作为将来的参考,这是一个EC2实例,执行InetAddress.getLocalHost()操作时返回null,因此我返回到命令行(只需深入了解localhost IP,AWS SDK就有点麻烦) //要运行的命令:/sbin/ifconfig | awk'NR==2{print$2}'| sed's/addr://g' Stri

对于ip,我无法让它返回除
null
之外的任何内容。我一定是在格式化字符串数组中的操作时遗漏了什么,请帮助!另外,是否有更好的sdk用于Java中的命令行工作Update作为将来的参考,这是一个EC2实例,执行InetAddress.getLocalHost()操作时返回null,因此我返回到命令行(只需深入了解localhost IP,AWS SDK就有点麻烦)

//要运行的命令:
/sbin/ifconfig | awk'NR==2{print$2}'| sed's/addr://g'

String[] command = new String[] {"/sbin/ifconfig", "awk 'NR==2{print$2}'", "sed 's/addr://g'" };
String ip = runCommand(command);

public static String runCommand(String[] command) {
        String ls_str;
        Process ls_proc = null;
        try {
            ls_proc = Runtime.getRuntime().exec(command);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        DataInputStream ls_in = new DataInputStream(ls_proc.getInputStream());

        try {
            while ((ls_str = ls_in.readLine()) != null) {
                    return ls_str;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

如果将数组传递给exec(),它的作用就好像第一个数组后面的所有元素都是第一个数组的参数一样。“awk”不是
ifconfig
的有效参数

  • 当出现错误时,为什么要尝试使用
    Runtime.exec()
  • 您尝试执行多个命令,将一个命令输出到另一个命令上。这项工作通常由壳牌公司完成。通过直接执行它,您无法获得该特性。您可以通过调用shell并将整个管道作为要执行的参数传递给它来解决这个问题
  • 阅读。它总结了使用
    Runtime.exec()
    (包括#2中提到的)时可能遇到的所有主要陷阱,并告诉您如何避免/解决它们

  • Runtime.exec()
    的形式接受一个
    字符串[]
    ,它不会在管道中执行多个命令。相反,它执行带有附加参数的单个命令。我认为做你想做的事情最简单的方法是
    exec
    一个shell来执行管道:

    Runtime.getRuntime().exec(new String[] { 
        "bash", "-c", "/sbin/ifconfig | awk 'NR==2{print$2}' | sed 's/addr://g'"
    });
    

    您可以使用
    java.net.NetworkInterface
    。像这样:

    public static List<String> getIPAdresses() {
        List<String> ips = new ArrayList<String>();
        try {
            Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    
            while (e.hasMoreElements()) {
                NetworkInterface ni = e.nextElement();
    
                Enumeration<InetAddress> e2 = ni.getInetAddresses();
    
                while (e2.hasMoreElements()) {
                    InetAddress ip = e2.nextElement();
                    if (!ip.isLoopbackAddress())
                        ips.add(ip.getHostAddress());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ips;
    }
    
    公共静态列表getipAddresses(){
    List ips=new ArrayList();
    试一试{
    枚举e=NetworkInterface.getNetworkInterfaces();
    而(e.hasMoreElements()){
    NetworkInterface ni=e.nextElement();
    枚举e2=ni.getInetAddresses();
    while(e2.hasMoreElements()){
    InetAddress ip=e2.nextElement();
    如果(!ip.isLoopbackAddress())
    add(ip.getHostAddress());
    }
    }
    }捕获(例外e){
    e、 printStackTrace();
    }
    返回IP;
    }
    

    Joachim Sauer已经发布了

    您真的需要调用系统命令来获取接口的IP地址,还是可以使用Java库来实现这一点;网络接口似乎不起作用。@user375566-如果我在EC2实例上使用枚举系统中IP地址的代码,我会得到输出。InetAddress addr=null;请尝试{addr=InetAddress.getLocalHost();}catch(UnknownHostException e){e.printStackTrace();}字符串hostname=addr.getHostName();返回主机名;为我返回null^他们需要一种更好的方式来格式化注释中的代码,这并不重要,因为他只对第一行感兴趣。1)框是EC2实例;网络接口似乎返回null。#2和#3仍然非常有效:)同意。我很少用Java做命令行工作,很高兴知道:)我知道如何做;查看更新以了解我无法使用此功能的原因。
    public static List<String> getIPAdresses() {
        List<String> ips = new ArrayList<String>();
        try {
            Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    
            while (e.hasMoreElements()) {
                NetworkInterface ni = e.nextElement();
    
                Enumeration<InetAddress> e2 = ni.getInetAddresses();
    
                while (e2.hasMoreElements()) {
                    InetAddress ip = e2.nextElement();
                    if (!ip.isLoopbackAddress())
                        ips.add(ip.getHostAddress());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ips;
    }