[英]Why does this not return the IP address?
I can not get this to return anything but null
for ip. 我无法得到这个,但是为了ip而返回null
。 I must be missing something in the way that I format the operations inside the String array, please help! 我必须在格式化String数组中的操作的方式中遗漏一些东西,请帮忙! Also, is there a better sdk for command line work in Java? 另外,Java中的命令行工作是否有更好的sdk? Update For future reference, this is an EC2 Instance, and doing an InetAddress.getLocalHost() returns null, so I've reverted to the command line (the AWS SDK is kind of a pain just to drill down for a localhost IP). 更新为了将来参考,这是一个EC2实例,并且执行InetAddress.getLocalHost()会返回null,因此我已经恢复到命令行(AWS SDK只是为了深入了解本地主机IP而感到痛苦)。
// Command to be run: /sbin/ifconfig | awk 'NR==2{print$2}' | sed 's/addr://g'
//要运行的命令: /sbin/ifconfig | awk 'NR==2{print$2}' | sed 's/addr://g'
/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;
}
Runtime.exec()
when there's a perfectly easy way to enumerate network interfaces in Java ? 为什么在使用Java枚举网络接口的简单方法时,您尝试使用Runtime.exec()
? Runtime.exec()
won't . 读取Runtime.exec()
时不会 。 It summarizes all major pitfalls you can fall into when using Runtime.exec()
(including the one mentioned in #2) and tells you how to avoid/solve them. 它总结了使用Runtime.exec()
(包括#2中提到的那个Runtime.exec()
时可能遇到的所有主要缺陷,并告诉您如何避免/解决它们。 StringBuilder result = new StringBuilder()
try {
while ((ls_str = ls_in.readLine()) != null) {
result.append(ls_str);
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
If you pass an array to exec(), it acts as if all the elements after the first are arguments to the first one. 如果将数组传递给exec(),它就好像第一个元素之后的所有元素都是第一个元素的参数。 "awk" is not a valid argument to ifconfig
. “awk”不是ifconfig
的有效参数。
The form of Runtime.exec()
which takes a String[]
does not execute multiple commands in a pipeline. 采用String[]
的Runtime.exec()
形式不会在管道中执行多个命令。 Rather it executes a single command with additional arguments. 而是它使用其他参数执行单个命令。 I think the easiest way to do what you want is to exec
a shell to do the pipeline: 我认为执行所需操作的最简单方法是exec
shell来执行管道:
Runtime.getRuntime().exec(new String[] {
"bash", "-c", "/sbin/ifconfig | awk 'NR==2{print$2}' | sed 's/addr://g'"
});
You can use java.net.NetworkInterface
. 您可以使用java.net.NetworkInterface
。 Like this: 像这样:
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;
}
Joachim Sauer already posted the link to the documentation Joachim Sauer已经发布了文档链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.