I'm building a java application that gets the mac addresses of a user and compare it with the correspondent value in the database(security feature). but the problem happens on mac os when i discovered that the list of mac addresses has common values(ex: on my mac the list of mac addresses are: 001C42000009,001C42000008,E0F8474267B6(wifi),70CD60F1A5C1(ethernet))
Is there a way to know all these common values that will result when getting the Mac address on Mac os.
Thank you.
At http://standards.ieee.org/develop/regauth/oui/public.html you can lookup a vendor using first 3 bytes of the MAC address, 00-1C-42 points to "Parallels, Inc." (http://www.parallels.com). Are you using some of their virtualization software? Try what java.net.NetworkInterface.isVirtual() returns for this address, if that is not useful then some ugly filter may require (e.g. based on address pattern)
import java.net.NetworkInterface;
import java.util.Enumeration;
public class NetworkInterfaceTest {
public static void main(String args[]) {
try {
Enumeration<NetworkInterface> ie = NetworkInterface.getNetworkInterfaces();
while (ie.hasMoreElements()) {
NetworkInterface i = ie.nextElement();
System.out.println(i.getDisplayName() + " [" + i.getName() + "]: " + formatAddress(i.getHardwareAddress()) + "; isVirtual=" + i.isVirtual());
}
} catch (Exception e){
e.printStackTrace();
}
}
private static String formatAddress(byte[] address) {
if (address == null) {
return null;
}
StringBuilder ret = new StringBuilder(address.length * 2);
for (byte b : address) {
if (ret.length() > 0) {
ret.append('-');
}
String bs = Integer.toHexString(b & 0x000000FF).toUpperCase();
if (bs.length() < 2) {
ret.append('0');
}
ret.append(bs);
}
return ret.toString();
}
}
i believe something like this will do the work for you
try {
InetAddress []addresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
/*
* Get NetworkInterfaces for current host and read hardware addresses.
*/
for(int j=0; j< addresses.length; i++) {
System.out.format("%02X%s", mac[i], (i < addresses.length – 1) ? "-" : "");
}
System.out.println();
}
}
Related
The following java code lists all the bare metal servers in softlayer for a specific SL account and filters for servers which are powered on (e.g. powerState='on'.
public void listServers(Account.Service service, ApiClient client) throws Exception {
service.withMask().hardware().fullyQualifiedDomainName().primaryIpAddress();
service.withMask().hardware().hardwareStatus();
service.withMask().hardware().id();
Account account = service.getObject();
//
// list of softlayer servers for the client account
//
for (Hardware hardware : account.getHardware()) {
String hostname = hardware.getFullyQualifiedDomainName();
String hardwareStatus = (hardware.getHardwareStatus() == null) ? null : hardware.getHardwareStatus().getStatus();
Long serverId = hardware.getId();
String powerState = null;
if (serverId != null) {
Hardware.Service hardwareService = Hardware.service(client, serverId);
hardwareService.setMask("mask[serverPowerState");
try {
powerState = hardwareService.getServerPowerState();
} catch (Exception ex) {
System.out.println("Error, cannot get powerState, hostname=" + hostname + ", msg=" + ex.getMessage());
}
}
System.out.println("Hostname=" + hostname + ", hwStatus=" + hardwareStatus + ", powerState=" + powerState);
}
}
Code seems to work, but for at least one of the servers, it fails on the call to hardwareService.getServerPowerState()
"Unable to establish IPMI v2 / RMCP+ session".
Any ideas why this is failing ?
I am using JNDI in Java to perform DNS lookups in my application to resolve A records - running under Java 8 on Windows 7. However, I am having trouble resolving records unless I specify the complete host entry including domain name.
Java appears to be ignoring the DNS search list which is configured on the PC. I don't have a problem including the domain name, if that is what Java requires, but I can't find a public method to obtain the domains in the search list.
The following SSCCE uses the private method sun.net.dns.ResolverConfiguration to obtain the DNS search list, but I shouldn't use it as it is an internal proprietary API and may be removed in a future release.
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
public class SSCCE {
public static void main(String[] args) {
String[] hostsToLookup = new String[] { "testhost", "testhost.mydomain.com" };
try {
System.out.println("DNS Search List:");
for (Object o: sun.net.dns.ResolverConfiguration.open().searchlist()) {
System.out.println(" " + o);
}
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
InitialDirContext idc = new InitialDirContext(p);
for (String h : hostsToLookup) {
System.out.println("Host: " + h);
try {
Attributes attrs = idc.getAttributes(h, new String[] { "A" });
Attribute attr = attrs.get("A");
if (attr != null) {
for (int i = 0; i < attr.size(); i++) {
System.out.println(" " + attr.get(i));
}
}
}
catch (NameNotFoundException e) {
System.out.println(" undefined");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
When I run this using just the host part it doesn't resolve, but when I manually add the domain from the search list then it does:
DNS Search List:
mydomain.com
Host: testhost
undefined
Host: testhost.mydomain.com
192.0.2.1
Is it possible to either make Java honour the DNS search list using JNDI or is there a public method to obtain the DNS search list?
I can't find the ip address of a device connected to my network. I tried using network interface, but only gives me the loopback address and my pc address; the code that i used is:
try
{
Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
for (; n.hasMoreElements();)
{
NetworkInterface e = n.nextElement();
System.out.println("Interface: " + e.getName());
Enumeration<InetAddress> a = e.getInetAddresses();
for (; a.hasMoreElements();)
{
InetAddress addr = a.nextElement();
System.out.println(" " + addr.getHostAddress());
}
}
}catch (Exception e)
{
System.out.println(e.toString());
}
Also, i used PrintServiceLookup,but the methods of that class doesn't give the ip address (the device is a card printer); the code that i used is:
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
System.out.println("Printer Services found:");
printService(services);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
if (service!=null) {
System.out.println("Default Printer Service found:");
System.out.println(service);
}
private static void printService(PrintService[] services) {
if (services!=null && services.length>0) {
for (int i = 0; i < services.length; i++) {
System.out.println(services[i]);
}
}
}
Anyone has a different point of view or perspective to solve the issue?
NetworkInterface.getInetAddresses() is used to return the local IP address of the network adaptor, hence the reason you are seeing loopback and your primary IP.
It's not possible to enumerate devices on the network in this way. You may need to look into something like SNMP.
So, since dynamic variables aren't a thing in Java, and if statements will be horribly unwieldy, was looking for help converting this code block into a more concise one.
I looked into hashmaps, and they just didn't seem quite right, it's highly likely I was misunderstanding them though.
public String m1 = "Name1";
public String m1ip = "192.1.1.1";
public String m2 = "Name2";
public String m2ip = "192.1.1.1";
public String req;
public String reqip;
... snip some code...
if (requestedMachine == 1)
{ req = m1; reqip = m1ip;}
else if (requestedMachine == 2)
{ req = m2; reqip = m2ip;}
else if (requestedMachine == 3)
{ req = m3; reqip = m3ip;}
else if (requestedMachine == 4)
{ req = m4; reqip = m4ip;}
else if (requestedMachine == 5)
{ req = m5; reqip = m5ip;}
requestedMachine is going to be an integer, that defines which values should be assigned to req & reqip.
Thanks in advance.
Define a Machine class, containing a name and an ip field. Create an array of Machine. Access the machine located at the index requestedMachine (or requestedMachine - 1 if the number starts at 1):
Machine[] machines = new Machine[] {
new Machine("Name1", "192.1.1.1"),
new Machine("Name2", "192.1.1.1"),
...
}
...
Machine machine = machines[requestedMachine - 1];
First, create a Machine class:
class Machine {
String name;
String ip;
//Constructor, getters, setters etc omitted
}
Initialize an array of Machines:
Machine[] machines = ... //initialize them with values
Get the machine corresponding to requestedMachine:
Machine myMachine = machines[requestedMachine];
This is a great candidate for an enum:
/**
<P>{#code java EnumDeltaXmpl}</P>
**/
public class EnumDeltaXmpl {
public static final void main(String[] ingo_red) {
test(MachineAction.ONE);
test(MachineAction.TWO);
test(MachineAction.THREE);
test(MachineAction.FOUR);
}
private static final void test(MachineAction m_a) {
System.out.println("MachineAction." + m_a + ": name=" + m_a.sName + ", ip=" + m_a.sIP + "");
}
}
enum MachineAction {
ONE("Name1", "192.1.1.1"),
TWO("Name2", "292.2.2.2"),
THREE("Name3", "392.3.3.3"),
FOUR("Name4", "492.4.4.4"),
FIVE("Name5", "592.5.5.5");
public final String sName;
public final String sIP;
private MachineAction(String s_name, String s_ip) {
sName = s_name;
sIP = s_ip;
}
}
Output:
[C:\java_code\]java EnumDeltaXmpl
MachineAction.ONE: name=Name1, ip=192.1.1.1
MachineAction.TWO: name=Name2, ip=292.2.2.2
MachineAction.THREE: name=Name3, ip=392.3.3.3
MachineAction.FOUR: name=Name4, ip=492.4.4.4
Thee best choice you have is to build an array of machines with IP, Name etc..then you only need to find the machine required into the array.
public class Machine(){
private String name, ip;
public Machine(String name, String ip){
this.name=name;
// You can check a valid ip
this.ip=ip;
}}
public class Machines(){
private Machine[] machines;
private int number_of_machines;
public Machines(){
//define number_of_machines for your array and length of itself
}}
main()
Machine[] Machines = new Machine[number_of_machines];
Machine m1 = new Machine(String name, String ip);
.
.
.
Machine mn = new Machine(String name, String ip);
int number=5;
for(int i=0; i<number_of_machines; i++){
if (machines[number]<number_of_machines){
System.out.println("There is no machine with that number");
}else if (machines[number]==number_of_machines-1){
System.out.println("That is the choosen machine");
}
}
}
If your id values are not necessarily integers or if they are not a continuous sequence from 0 forward, you could also use a HashMap. Something like
HashMap<Integer, Machine> machines = new HashMap<>();
machines.put(1, machine1);
machines.put(7, machine7);
...
to get the desired value
Machine machine7 = machines.get(7);
You can replace the key with a String or whatever you like if needed. Your id values also do not need to go 0,1,2,3,4,5, ... as they need to if you are going with an array.
As the title says... I'm trying to be able to get the IP of the wifi iface when it is configured as hotspot. Ideally, I would like to find something that works for all the phones.
Of course, the WifiManager is useless when it comes to get info from the AP.
Luckily, I've been able to get the IPs of all the interfaces by doing this:
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
Log.d("IPs", inetAddress.getHostAddress() );
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
This chunk of code will print all the IP of all the interfaces (Wifi hotspot included). The main problem is that I don't find a way to identify the WiFi interface. This is an issue since some phones have multiple interfaces (WiMax, etc). This is what I've tried so far:
Filtering by the wifi iface display name: it's not a good approach because the display name changes from one device to another (wlan0, eth0, wl0.1, etc).
Filtering by its mac address: almost work, but on some devices the hotspot iface does not have a MAC address ( iface.getHardwareAddress() returns null)...so not a valid solution.
Any suggestions?
Here's what I did to get the wifi hotspot ip:
public String getWifiApIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
if (intf.getName().contains("wlan")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()
&& (inetAddress.getAddress().length == 4)) {
Log.d(TAG, inetAddress.getHostAddress());
return inetAddress.getHostAddress();
}
}
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString());
}
return null;
}
This will give you the IP address of any wifi device, which means it's not just for the hotspot. If you're connected to another wifi network (meaning you're not in hotspot mode), it'll return an IP.
You should check if you are in AP mode first or not. You can use this class for that: http://www.whitebyte.info/android/android-wifi-hotspot-manager-class
You can use this.
((WifiManager) mContext.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo().serverAddress
Full Code
private String getHotspotIPAddress() {
int ipAddress = mContext.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo().serverAddress;
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
ipAddress = Integer.reverseBytes(ipAddress);
}
byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
String ipAddressString;
try {
ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
} catch (UnknownHostException ex) {
ipAddressString = "";
}
return ipAddressString;
}
Here is a possible solution that utilizes WiFiManager ConnectionInfo to find corresponding NetworkInterface.
If you just need the IP then you can use:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
When the Wifi is not setup as a hotspot, it has a name android-xx7632x324x32423 home
when hotspot is turned on, that name is gone. Also the ip address changes.
So if you are able to get the Wifi config before enabling the hotspot, first of all you can use intf.getName() to get a reference to it.
Second, the ip changed, so if you know which interface the wifi is in CONNECTED mode, you can use that info to identify it later on after enabling the hotspot.
Below is some code I used for debugging. I just spit out everything I can find, make a huge mess then clean it up when I figured my problem out. GL
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import android.net.ConnectivityManager;
textStatus = (TextView) findViewById(R.id.textStatus);
try {
for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) {
for (InetAddress addr : Collections.list(intf.getInetAddresses())) {
if (!addr.isLoopbackAddress()){
textStatus.append("\n\n IP Address: " + addr.getHostAddress() );
textStatus.append("\n" + addr.getHostName() );
textStatus.append("\n" + addr.getCanonicalHostName() );
textStatus.append("\n\n" + intf.toString() );
textStatus.append("\n\n" + intf.getName() );
textStatus.append("\n\n" + intf.isUp() );
}
}
}
} catch (Exception ex) {
textStatus.append("\n\n Error getting IP address: " + ex.getLocalizedMessage() );
}
connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
allInfo = connectivity.getAllNetworkInfo();
mobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
textStatus.append("\n\n TypeName: " + mobileInfo.getTypeName());
textStatus.append("\n State: " + mobileInfo.getState());
textStatus.append("\n Subtype: " + mobileInfo.getSubtype());
textStatus.append("\n SubtypeName: " + mobileInfo.getSubtypeName());
textStatus.append("\n Type: " + mobileInfo.getType());
textStatus.append("\n ConnectedOrConnecting: " + mobileInfo.isConnectedOrConnecting());
textStatus.append("\n DetailedState: " + mobileInfo.getDetailedState());
textStatus.append("\n ExtraInfo: " + mobileInfo.getExtraInfo());
textStatus.append("\n Reason: " + mobileInfo.getReason());
textStatus.append("\n Failover: " + mobileInfo.isFailover());
textStatus.append("\n Roaming: " + mobileInfo.isRoaming());
textStatus.append("\n\n 0: " + allInfo[0].toString());
textStatus.append("\n\n 1: " + allInfo[1].toString());
textStatus.append("\n\n 2: " + allInfo[2].toString());
private static byte[] convert2Bytes(int hostAddress) {
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
return addressBytes;
}
public static String getApIpAddr(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
byte[] ipAddress = convert2Bytes(dhcpInfo.serverAddress);
try {
String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress();
return apIpAddr;
} catch (UnknownHostException e) {
e.printStackTrace();
}
return null;
}
I use the solution of ajma, changing intf.getName().contains("wlan") to intf.getName().contains("wl") || intf.getName().contains("ap"). And it works for many mobile phones.
But it returns null when you just connected to a WiFi.
With the number of new phones coming out every year from new manufacturers simply identifying the name of the wireless interface is bound to fail in the nearest future. The following method can get the remote server ip from the integer ip returned by getDhcpInfo().serverAddress.
public String getIPv4Address(int ipAddress) {
// convert integer ip to a byte array
byte[] tempAddress = BigInteger.valueOf(ipAddress).toByteArray();
int size = tempAddress.length;
// reverse the content of the byte array
for(int i = 0; i < size/2; i++) {
byte temp = tempAddress[size-1-i];
tempAddress[size-1-i] = tempAddress[i];
tempAddress[i] = temp;
}
try {
// get the IPv4 formatted ip from the reversed byte array
InetAddress inetIP = InetAddress.getByAddress(tempAddress);
return inetIP.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "";
}
Then you can use it like this from the activity where you use the WiFi service
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String serverIp = getIPv4Address(wifi.getDhcpInfo().serverAddress);
Log.v("Server Ip", serverIp);
This should show you the IP of the connected server.
NOTE: ensure you have already created a successful connection to an access point (e.g. hotspot) via WiFi before querying for the server ip. You only need the SSID and preSharedkey (if it's secure) to create a successful connection and not the server ip.