How can I get MAC Address without using the IP in Java? - java

How can I found the MAC address of a network card without knowing and using the IP address in Java?

you can use NetworkInterface.getHardwareAddress. You can get the list of all network cards with NetworkInterface.getNetworkInterfaces().
you can use this code for knowing MAC Address.
InetAddress address = InetAddress.getLocalHost();
NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
byte mac[] = nwi.getHardwareAddress();
System.out.println(mac);

You can use system command like below(if you on nix systems, if your os is windows use ipconfig in a similar way):
Runtime r = Runtime.getRuntime();
Process p = r.exec("ifconfig -u |grep ether");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null) {
System.out.println(line);
}
b.close();
On my system it gives:
ether 28:37:37:15:3b:31
ether b2:00:10:65:56:41
...
Notice that there are maybe many MAC addresses, the Bluetooth, the Ethernet, the WIFI and so on.

Related

JSch - How to capture stream from remote server using PortForwarding L?

I tried to use InputStream and Buffer[], also BufferedReader, also PipedInputStream. For all cases i got null:
sessionB = jSch.getSession(username, "localhost", forwardedPort);
sessionB.connect();
if(sessionB.isConnected()) {
System.out.println("Connected host B!");
channel = (ChannelExec) sessionB.openChannel("exec");
br = new BufferedReader(new
InputStreamReader(channel.getInputStream()));
((ChannelExec)channel).setCommand("command");
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
if(channel.isConnected()) {
System.out.println("Channel is connected!");
}
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
And console output:
Connected host A! Connected host B! Channel is connected!
Problem: I got nothing printed (System.out.println(line);)
There is a way to get that stream from exec channel over portforwarding ? Thanks for your help
get/setInputStream and get/setOutputStream only concern the standard input/output of the command, nothing to do with port forwarding.
The port forwarding is done through two functions setPortForwardingL and setPortForwardingR.
You should have something like that in your code.
int assinged_port=session.setPortForwardingL(lport, rhost, rport);
As always, there is very few documentation on JSCH but a lot of detailed examples for L forwarding and R forwarding
The port forwarding is done on the session so I'm not sure you also need an active connection. But if you do, you should consider opening a 'shell' connection instead of an 'exec'. Then you wouldn't have to run a useless command just to maintain the connection.
PS: Some code is missing so it's difficult to say something precise on the example given in the question.
Thanks for your help. I solved it using Pipes, i share what i did:
PipedInputStream en = new PipedInputStream();
pin = new PipedOutputStream((PipedInputStream) en);
BufferedReader br = new BufferedReader(new InputStreamReader((PipedInputStream)
channel.getInputStream()));
channel.connect(5*1000);
String received=null;
while((received=br.readLine())!=null) {
System.out.println(received);
}

Checking Internet Connection without URL test connection using java

How can I check whether my computer is connected to the Internet. I don't want to use URL check method. Is it possible to call an operating system's function using java? For example, in JNA library, is there any function to perform this ?
You can use ping command like this:
class PingHost
{
public static void main(String[] args)
{
String ip = "www.google.com";
String pingResult = "";
String pingCmd = "ping " + ip;
try
{
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);
BufferedReader in = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
pingResult += inputLine;
}
in.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
You should receive a response like:
64 bytes from 173.194.37.147: icmp_seq=0 ttl=52 time=299.032 ms
64 bytes from 173.194.37.147: icmp_seq=1 ttl=52 time=508.100 ms
You could check to see if a common hostname (such as google) is resolvable through dns to an ip address.
Once that address has been resolved, you can check to see if it is reachable.

How to get MAC Address without using IP Address in java socket programming

I am New in socket programming in Java.Can someone tell me , how to get MAC Address without using IP Address in socket programming.
This is the code by which i can get MAC address in socket--
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "- " : ""));
}
System.out.println(sb.toString());
but this is using IP Address ultimately.Is there any way to get the MAC Address without using IP Address?
Any PC's MAC ADDRESS by using socket
In a nutshell, there is no reliable method for finding out the MAC address of a host outside your subnet.
If you are on the same subnet as the host in question, take a look at ARP and RARP.
MAC addresses are only used on local networks. It is the way a switch/router knows where a packet has to be sent. IP is used to transport packets from network to network.
All TCP/UDP packets include senders IP and MAC. This way the receiving device can include the MAC in the return package so the switch/router know where to deliver it. MAC addresses should be unique but there is no guarantee, and it is not possible to use as a device address on the internet.
When you use internet you use IP protocol (A global address system)
When your on a local network the devices usually use MAC addresses.
try{
InetAddress ip = InetAddress.getLocalHost();
System.out.println("ip : " + ip);NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("Current MAC address : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println(sb.toString());
String s=sb.toString();System.out.println(s);

How to get the IP address of a system using Android phone?

How to get the IP address of the PC using Android phone? (In particular how to fetch the IP address of a system with a specific MAC address, connected on the same network as the Android phone)?
wired wired
modem--------router---------PC(mac:EE:00:B3:2F:56:12)
|
|
|
wireless
|
|
|
-------------android phone(A)
private String getIP(String mac) {
String ip = null;
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line = "";
while((line = br.readLine()) != null) {
String[] tokens = line.split("\\s+");
// The ARP table has the form:
// IP address HW type Flags HW address Mask Device
// 192.168.178.21 0x1 0x2 00:1a:2b:3c:4d:5e * tiwlan0
if(tokens.length >= 4 && tokens[3].equalsIgnoreCase(mac)) {
ip = tokens[0];
break;
}
}
br.close();
}
catch(Exception e) { Log.e(TAG, e.toString()); }
return ip;
}
But beware: Unless you have already established contact with your PC (and you'll need either its IP address or its name), the ARP table will be empty.
I suppose you'd like to do it the other way round, establish the connection with the PC knowing only its MAC address. Then it's not that simple. You might try to ping everyone on the local network (Runtime.getRuntime().exec("ping -b 192.168.178.255");) just to fill the ARP table.
Or, maybe, you can get the list of all clients with their IP addresses from your router?

Finding mac Address using IP address in java

public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
// InetAddress address = InetAddress.getByName("192.168.46.53");
/*
* Get NetworkInterface for the current host and then read the
* hardware address.
*/
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
/*
* Extract each array of mac address and convert it to hexa with the
* following format 08-00-27-DC-4A-9E.
*/
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
} else {
System.out.println("Address doesn't exist or is not accessible.");
}
} else {
System.out.println("Network Interface for the specified address is not found.");
}
I'm having a problem finding the MAC address of a remote host, but I am able to find the MAC address of my local host. If I have the IP address of the other system can I retrieve the MAC address of that system?
InetAddress address = InetAddress.getByName("192.168.46.53");
if i specify the ip address of a system in my workgroup... ni values gets null.... and not able to fetch it.... but if give my ip address of my system...it fetches???
Thanks,
Sunny
You will only be able to fetch the MAC address of remote hosts on your local LAN, that is, hosts that are in the same subnet as your computer. MAC addresses of hosts more than one hop away (IP hop, not Ethernet hop) cannot be determined.
And note that fetching the corresponding MAC address for hosts on your local LAN requires the permissions necessary to fetch either the ARP table, or those necessary to send and receive raw packets. Most OSes allow reading of the ARP table without special permissions, but the mechanism you use to do so will change depending on the OS. If you need a technique for a particular OS, you will have to update your question to include that info.

Categories