Skip to content

Instantly share code, notes, and snippets.

@blackraccoon000
Created April 10, 2013 09:01
Show Gist options
  • Save blackraccoon000/5353045 to your computer and use it in GitHub Desktop.
Save blackraccoon000/5353045 to your computer and use it in GitHub Desktop.
InterfaceGet!
package GetIntName;
/**
* Created with IntelliJ IDEA.
* User: Yutaka Fujii
* Date: 13/04/10
* Time: 10:53
* To change this template use File | Settings | File Templates.
*/
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Pattern;
public class GetInterfaceName {
void getIntNames() throws SocketException {
Enumeration enuIfs = NetworkInterface.getNetworkInterfaces();
while (enuIfs.hasMoreElements()){
NetworkInterface ni = (NetworkInterface)enuIfs.nextElement();
Enumeration enuAddrs = ni.getInetAddresses();
if(ni.getName().matches(Pattern.quote("eth3"))) {
while (enuAddrs.hasMoreElements()) {
InetAddress in4 = (InetAddress)enuAddrs.nextElement();
System.out.println("A: "+in4.getHostAddress()+"\n"+ni.getDisplayName());
}
}
if(ni.getName().matches(Pattern.quote("eth10"))) {
while (enuAddrs.hasMoreElements()) {
InetAddress in4 = (InetAddress)enuAddrs.nextElement();
System.out.println(ni.getDisplayName()+"\n"+"F: "+in4.getHostAddress());
}
}
}
}
}
package GetIntName;
import javax.swing.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.SocketException;
/**
* Created with IntelliJ IDEA.
* User: Yutaka Fujii
* Date: 13/04/10
* Time: 15:53
* To change this template use File | Settings | File Templates.
*/
public class RunAreaStream extends OutputStream {
private JTextArea _area;
private ByteArrayOutputStream _buf;
public RunAreaStream(JTextArea area){
_area = area;
_buf = new ByteArrayOutputStream();
}
@Override
public void write(int b)throws IOException {
_buf.write(b);
}
@Override
public void flush() throws IOException{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
_area.append(_buf.toString());
_buf.reset();
}
});
}
public static void main(String[] args) throws SocketException {
JTextArea area = new JTextArea();
area.setEditable(false);
RunAreaStream stream = new RunAreaStream(area);
System.setOut(new PrintStream(stream, true));
JFrame frame = new JFrame();
frame.getContentPane().add(area);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setLocation(0, 0);
frame.setSize(350,100);
frame.setVisible(true);
GetInterfaceName gn = new GetInterfaceName();
gn.getIntNames();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment