Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a very typical situation in which I want to connect an (android) phone to a linux notebook in the same (wifi) network using ssh. The IP's are assigned by DHCP so I know only the one of the client in advance. Knowing the IP of the client I could in principle loop a ssh command over all possible IP's in the same net to see if a server is listening there. However this seems highly inefficient. So, how can I figure out the IP of my host (knowing it's MAC address) so I can connect to it using ssh? I know about other software which achieves something like this so it must be possible. Related: can I dynamically assign a hostname to this IP on the client (assuming it is Linux) so that I can use a static entry in the ssh config file?

share|improve this question
    
One of the applications I have in mind is the new kdeconnect which establishes a connection (on different ports) between a phone and KDE. An obvious solution would be to missuse a dynamic DNS service to store the local IP's online. But how? –  highsciguy Oct 16 '13 at 9:20
    
Do you have control to the notebook and android phone? If you have, you can program a simple script to write the IP address of the notebook to a TXT file on publicly accessible server (preferably your own), and your android phone will get the IP from that TXT file –  Sharuzzaman Ahmat Raslan Oct 16 '13 at 9:52
    
Yes, I do and I could do it. It would be fairly easy if I would use for instance dropbox for it. But it looks like a hack for a problem which seems to be quite common. It also remains the problem to make ssh aware of the IP (if want to realize simple logins using the config file). –  highsciguy Oct 16 '13 at 10:33
    
There is a note about the kdeconnect backend here: unix.stackexchange.com/feeds/user/18047 . According to it kdeconnect uses 'broadcast messages' instead of avahi. But this seems to be a rather general term and I don't understand what it means. –  highsciguy Oct 16 '13 at 11:46
    
There is a related question here: android.stackexchange.com/questions/27522/… . The replies don't seem to be conclusive, however. –  highsciguy Oct 16 '13 at 11:54

3 Answers 3

You can do it in the following two steps:

Step1:

Scan your subnet to fill your ARP cache. There are methods but I suggest fping. Install it on Ubuntu by running command bellow in a terminal:

apt-get install fping

...then scan your network (for example, subnet 192.168.10.0/24):

fping -g 192.168.10.0/24

Now the ARP cache is filled with MAC address of devices in your subnet.

Step2

Apply an appropriate filter on your ARP cache to just see target device. Just use following command (where aa:bb:cc:dd:ee:ff is the device's MAC):

arp -n | grep -i aa:bb:cc:dd:ee:ff | cut -c-15

The output is the IP address of target device.

EDIT1:

Sample MAC address has been changed to lower case because Linux shows it in this manner (unlike Microsoft Windows using upper case)

EDIT2:

Following bash script add an entry to hosts file (/etc/hosts) with name cellphone so you can access your device with name cellphone. To refresh associated IP address just run it again. Change name,mac and subnet to desired values.

#!/bin/sh
name="cellphone"
mac=aa:bb:cc:dd:ee:ff
subnet=192.168.10.0/24
fping -g ${subnet}
ip="$(arp -n | grep -i ${mac} | cut -c-15)"
sed -i".bak" '/'${name}'/d' /etc/hosts
if [ -z "$ip" ]; then
    echo "Device not found!"
else
    echo "${ip}\t${name}" >> /etc/hosts
fi
share|improve this answer
1  
Very nice, works! MAC address needs to be lowercase, so a minor improvement is arp -n | grep -i AA:BB:CC:DD:EE:FF | cut -c 1-15. Since you mention arp: I was wondering if I can use it, or similar to assign a hostname to the IP I retrieved in this way. Such that I can have a static ssh config file. –  highsciguy Oct 18 '13 at 17:52
    
I would have started with nmap, but that's just because it's what I'm used to. Good answer. Perfect solution. –  bahamat Oct 18 '13 at 18:12

Zeroconf might be a solution here. It is a protocol to discover services on the network and establish a link between two hosts.

If you set up avahi mDNS (packages avahi-daemon and libnss-mdns) the hosts advertise their host names and you can resolve them with <hostname>.local.

share|improve this answer
    
Do I need to do it on both devices? Then this is probably something which would be difficult on the android? –  highsciguy Oct 16 '13 at 9:28
2  
Yes. Zeroconf works by means of advertising and discovery. I don't know if there are zeroconf implementations for Android. –  Marco Oct 16 '13 at 9:36
1  
A google search reveals that there are Zeroconf implementations for Android, lacking an Android device I can't provide further assistance. You might have more luck on Android SX. –  Marco Oct 16 '13 at 11:23

It sounds like your problem is that you're plugging something into the network and you want to be able to know what IP it will get.

On that basis it sounds like the simplest possible solution is to configure your DHCP server to give out a static IP for the appropriate MAC address.

Do you have control over the DHCP server? If so you could probably do one of two things:

  • Check the logs to see what IP the given MAC was allocated.
  • Setup a static map.
share|improve this answer
    
Yes, this is what I want but I actually don't have control over it (at least not all of them). –  highsciguy Oct 16 '13 at 9:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.