为什么不返回 IP 地址?
除了 ip 的 null
之外,我无法让它返回任何内容。我在格式化字符串数组内的操作时一定遗漏了一些东西,请帮忙!另外,是否有更好的用于 Java 命令行工作的 sdk? 更新供将来参考,这是一个 EC2 实例,并且执行 InetAddress.getLocalHost() 返回 null,因此我已恢复到命令行(AWS SDK 有点痛苦,只是为了钻取关闭本地主机 IP)。
// 要运行的命令:/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;
}
I can not get this to return anything but null
for ip. I must be missing something in the way that I format the operations inside the String array, please help! Also, is there a better sdk for command line work in Java? 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).
// Command to be run: /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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Runtime.exec()
获胜时't。它总结了使用 Runtime.exec() 时可能陷入的所有主要陷阱(包括 #2 中提到的陷阱),并告诉您如何避免/解决它们。Runtime.exec()
when there's a perfectly easy way to enumerate network interfaces in Java?Runtime.exec()
won't. It summarizes all major pitfalls you can fall into when usingRuntime.exec()
(including the one mentioned in #2) and tells you how to avoid/solve them.如果将数组传递给 exec(),则它的作用就好像第一个之后的所有元素都是第一个元素的参数。 “awk”不是
ifconfig
的有效参数。If you pass an array to exec(), it acts as if all the elements after the first are arguments to the first one. "awk" is not a valid argument to
ifconfig
.采用
String[]
的Runtime.exec()
形式不会在管道中执行多个命令。相反,它执行带有附加参数的单个命令。我认为完成您想要的操作的最简单方法是exec
shell 来执行管道:The form of
Runtime.exec()
which takes aString[]
does not execute multiple commands in a pipeline. Rather it executes a single command with additional arguments. I think the easiest way to do what you want is toexec
a shell to do the pipeline:您可以使用
java.net.NetworkInterface
。像这样:Joachim Sauer 已经发布了 文档链接
You can use
java.net.NetworkInterface
. Like this:Joachim Sauer already posted the link to the documentation