1

I want to lookup in my system (using java) for all network cards and network interfaces. So, more exactly, I want to get the whole output from ifconfig command (in Linux) but in Java. I know if I use NetworkInterface.getNetworkInterfaces() will return only configured network interfaces.

A rough approach I found at this link. It is OK, but I'm interested if there are other possibilities on this.

artaxerxe
  • 6,281
  • 21
  • 68
  • 106
  • you can execute system commands in java, http://stackoverflow.com/questions/792024/how-to-execute-system-commands-linux-bsd-using-java – fsonmezay Dec 23 '11 at 07:35
  • @fsonmezay Yes, I know, as the link suggests, but I want to know if there is a trick that will give me this. Something like NetworkInterface.getNetworkInterfaces() – artaxerxe Dec 23 '11 at 07:39
  • getNetworkInterfaces() should return also interface with DOWN state. And also interfaces without IP addresses. – SKi Dec 23 '11 at 08:31
  • Strange, I just try it with windows. Maybe it works different way in Linux. There is isUp() function in the class. So it would even more stange if it does not ever return DOWN interfaces. – SKi Dec 23 '11 at 09:49
  • @User1 Yes, you're right, but if you have a network card that isn't configured yet, you won't see it in the Enumeration returned by getNetworkInterfaces() method. That's what I was talking about. – artaxerxe Dec 23 '11 at 10:34

1 Answers1

2

You can use Runtime.getRuntime().exec("ipconfig") for Windows and Runtime.getRuntime().exec("ifconfig") for linux to get ifconfig result in java

There is no way to get to the gory details here in 100% Java. You can either execute commands or write JNI.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • See the comment written for fsonmezay – artaxerxe Dec 23 '11 at 07:41
  • 1
    @bmargulies Why would you edit someone's answer with an opinionated statement like that? Just leave a comment. – shmosel Dec 28 '17 at 02:13
  • I added a fact to make the answer more complete after closing another question as a duplicate of this one. You could support your claim that my statement is mere opinion with another answer that offered contrary facts. If you did that, I'd upvote your answer and roll back my edit. – bmargulies Dec 28 '17 at 02:16