Detecting known computer on public network in BASH

2

Anybody know of a way through BASH (specifically OSX terminal, but I'd prefer a BASH only solution without OSX' in built functionality) to detect a known computer on a public network (that I am also located on)?

My thought was that since I know the MAC address of the computer I'm trying to find, I could simply resolve that to an IP, but I'm not sure exactly how I would go about resolving a MAC address to a local IP address on the public network using BASH commands.

My goal is to set up a scripted solution that will allow me to set up a desktop icon that when I double click, detects a known computer on the network and its corresponding local IP address, creates an SSH tunnel for VNC, and then opens the screen sharing app through the SSH tunnel. Essentially, double click to open a secure screen sharing connection on a public network with no third party software and only the tools available via BASH and OSX.

All of this happens within the same public wifi network. For example, imagine two people go to the same coffee shop. I want person A to scan for the IP of Person B given that they already know Person B's IP address.

Thought Space Designs

Posted 2014-09-06T23:21:44.787

Reputation: 149

Answers

1

They make something for this called a DNS name. If it's a public server then it more than likely has one. If it doesn't then you're going to want to make your own, if you already have a DNS name in your org then just add a new A record on your DNS server which specifies a new sub domain for that server. In general, public servers all use static IP addresses though so even this isn't really needed. The IP should never change so just use it. If the IP is changing then you're going to need to either install a dynamic dns client on the server which will keep the DNS entry up to date for you automatically or design your own system (possibly a script which keeps your host file(s) updated [I mean the host files on each of your clients; not the server]). Also, there is no need to worry about local IP vs public IP. This is something your router knows how to manage automatically. As long as your routing table is properly setup it'll find the best path to the server. If you have a home router then this will all be handled automatically behind the scenes.

Update

I actually just thought of a good solution which should work. Get a cloud drive like Google Drive. Set it up on both computers to sync correctly. Then have the other pc write a file to the sync folder which contain the local IP:

echo `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` > /home/user/share/pcname_current_ip

Now you just need to read that file to find the current IP for that computer (as long as they've both synced of course).

krowe

Posted 2014-09-06T23:21:44.787

Reputation: 5 031

Hmm, I don't think I was clear. I know how to set up remote access to my own local network already, but in my current scenario, two laptops both connect to the same public network somewhere while traveling. One is mine and the other is my SO's. I want to be able to push a button, detect her IP on the current local network based off of the known MAC address, and then tunnel through to her computer. – Thought Space Designs – 2014-09-07T00:09:17.753

In that case, I have no great answers. You could use arp to find the MAC but that relies on your PC communicating with the other PC first which isn't very reliable. You could use nmap but that relies on doing a ping scan which isn't very friendly and ICMP is usually heavily restricted on public networks. If this was meant for a specific public network you could try setting a static IP on the other PC and hoping no one has it yet. None of these is really very great but they could all do what you want in the right situation. – krowe – 2014-09-07T01:13:25.517

@ThoughtSpaceDesigns See my update for my best answer so far. If I think of something better I'll update again but this shouldn't be too bad (esp if you already use a web drive). – krowe – 2014-09-07T01:24:11.620

Your update does come closer to what I'm trying to achieve, but I'd prefer to not have to use a third computer (server in this case) to act as a liaison. I'd prefer to be able to just find the other computer's IP address based off of the MAC address which I already know. I do have a server connected to the internet that I could do this with, but I'd prefer to make it "direct connect". Any other ideas? – Thought Space Designs – 2014-09-08T14:03:15.193

If you want to connect from the office to a remote computer with a variable IP that changes each time it connects to a provider, the most simple way to do is to know that IP. With mac-address you know the ID of the network adapter and it's challenging to find that MAC address in a wide network. You should know the IP of that computer each time it changes. To do it it's better to install a dynamic DNS client on the remote computer. E.g. you take TSD.noip.org and from the laptop you update with the current IP. From the office with host TSD.noip.org it answers you the IP and you can use it. – Hastur – 2014-09-08T19:16:53.267

@Hastur He is asking for a way to find the local IP. Dynamic DNS solutions only provide a public IP. This won't work for him because he has no access to the router at his local coffee shop and therefore cannot forward the needed ports through. – krowe – 2014-09-08T19:21:23.863

I don't understand what can he does with the local IP from outside the coffee shop... I suppose he means when he is connected from home or from an hotel. Usually it works. Maybe it's better to connect to a VPN and to scan inside the VPN to search the Ip from the MAC address. (Really not needed at that point because inside the VPN you can chose a fixed local IP for the laptop...)
I think it's time to clean the question... To many comment and the question is migrating to a new one... :)
– Hastur – 2014-09-08T19:43:18.577

@Hastur Imagine that he goes to the coffee shop with someone and he and the person he is with both bring a laptop. They are both on the same local network. He just wants the local IP of his friends computer which was assigned by the DHCP server on the coffee shop network. – krowe – 2014-09-08T19:54:33.640

@krowe Gotcha. For this he can easily use nbtscan on the local subnet ( or arp ... as answered here) and filter the output with grep or awk. Maybe I should understand it from the question, but it could be easier if he writes informations that should change the meaning of the question in the question itself and not as a comment of an answer...

– Hastur – 2014-09-09T07:02:04.167

@Hastur Sorry for the ambiguity of my question. I've updated it to be more accurate. Can you please update your answer to match my question? I need something that would work on Ubuntu linux and OSX out of the box with no necessary package installs. Is this possible? – Thought Space Designs – 2014-09-09T22:43:18.777

@ThoughtSpaceDesigns Most Linux distributions don't include either of those packages by default. I doubt OSX does either. Doing this without any additional packages is probably going to be nearly impossible short of writing something from scratch. But they are very common and in most package repositories. – krowe – 2014-09-09T22:52:48.150

Is it possible to install packages via a shell script? I've never had to do that before... – Thought Space Designs – 2014-09-11T15:01:09.627

@ThoughtSpaceDesigns That is possible but why would you? This only needs to be done once on your machine. The reason I'm not suggesting to use those myself is only because I don't think that going onto an open network and doing arp scans is a nice thing to do. It can be detected and it might anger the admin. Aside from that, it is a great option. If you want to be nice, use mine. – krowe – 2014-09-11T15:39:27.137

1Also, start up voting some of these suggestions. Even if you aren't going to use them you should acknowledge that people are trying to help you here. – krowe – 2014-09-11T15:40:39.147

BTW I was talking about using arp above. Using my system you'd need to install a package on all machines you want to have IPs for. Strictly speaking, you could probably read IPs from anywhere without it but you'd need a client (or a tool which acted like one) to upload your IP. You'd also need a cloud storage which you didn't mind sharing with others. The advantage is that there is no scanning, it'll always work if you have access your cloud drive, you will be connecting through the infrastructure of your location (the adhoc approach will be limited to the infrastructure you bring along). – krowe – 2014-09-11T16:01:54.050

I just don't like the idea of having to use a third computer in the chain. I'd prefer to just scout out the one using the information I already know about it. – Thought Space Designs – 2014-09-11T17:16:59.583

1

Short answer: The most simple thing to do is probably to install on the laptops something like a dynamic DNS update client to keep track of their dynamic IP address when they are not in your Local Network.

Some words more: The problem is that with only the MAC address you are forced to scan a subnet to find an answering machine with that MAC address once you call it's IP address. Other side of the problem is that this IP change each time the remote machine connects to a provider (or the ADSL reset the connection and assign a different IP address).

It's most simple if that machine (the laptop) communicates somehow the current IP. A way to do it to use a dynamic DNS update client. It exist even a Linux daemon ddclient that help with [dynamic ip servers].(http://ubuntuguide.org/wiki/Dynamic_IP_servers)

After that you choose your name (e.g 'myname.AtFreeOrCommercialProvider.org') and enable on the laptop a program to update the IP, it will be enough to do write from the office the above line to know the current IP of the remote machine:

host myname.AtFreeOrCommercialProvider.org  

after you will be able to write it in your script and use as you want.


More words on the original way The core of your original question is

I know the MAC address of the computer I'm trying to find, I could simply resolve that to an IP, but I'm not sure exactly how I would go about resolving a MAC address to a local IP address on the public network using BASH commands.

To obtain MAC-address from the IP-address you can use,e.g., one of the following method that will give you IP and MAC-address of answering computer in the net:

  1. nbtscan, e.g.nbtscan 192.168.1.0-255 will scan the IP address group you specify answering with IP address, NetBIOS Name, Server, User, MAC address. (If 192.168.1.xxx is your local network...)
  2. arp with no mode specifier will print the current content of the IPv4 network neighbour table with IP and MAC address.
    From the same family of tools sudo arp-scan --interface=eth0 192.168.0.0/24 if you are using a cable connection, or sudo arp-scan --interface=wlan0 192.168.0.0/24 if you are using a wireless connection...
  3. nmap -sP xxx.xxx.xxx.xxx on all your local network addresses and filter them with the known MAC addresses.
  4. nmblookup if you know the NetBIOS names...

After that you have on the same line the IP and the MAC:address you can write your script. Some of this tools may need to be installed.

PS> You can know your current IP address from

# if you are using a cable connection on the network ineterface eth0
sbin/ifconfig eth0 | grep -E "inet:" | awk '{print $2}' | sed s/"inet:"//g
# or you are using a wireless connection on the network ineterface wlan0
sbin/ifconfig wlan0 | grep -E "inet:" | awk '{print $2}' | sed s/"inet:"//g

PPs> IMHO it's not so nice or efficient to scan all internet only to know if that specific computer is connected... It is a different case if you connect this computer to a LAN or a virtual private network (with less IP to be scanned it's possible)... :-)

Hastur

Posted 2014-09-06T23:21:44.787

Reputation: 15 043

0

What about setting up your own ad-hoc wireless network on one of the PCs and route the other PCs traffic through it. Secure and no need for third party ip tracking...

Ryan Griggs

Posted 2014-09-06T23:21:44.787

Reputation: 682

I'd like to script the solution, and this seems like it would just introduce more headache than simply scanning IPs on the local network for a known MAC. Or maybe I'm missing something.... – Thought Space Designs – 2014-09-11T14:58:42.980

Any possibility to just plug in a network cable between the two? – Ryan Griggs – 2014-09-11T15:10:01.807

This would be bit annoying to configure the first time but it really is probably the best 'work with what you have' answer. – krowe – 2014-09-11T15:53:58.880

"Anybody know of a way through BASH (specifically OSX terminal, but I'd prefer a BASH only solution without OSX' in built functionality) to detect a known computer on a public network (that I am also located on)?"

Your answer skirts the question unless there's a way to do this entirely through BASH that I'm missing. – Thought Space Designs – 2014-09-11T17:15:45.237

-1

The simplest way to accomplish this is with logmein or team viewer. Yes its third party but it would work very simply.

Ryan Griggs

Posted 2014-09-06T23:21:44.787

Reputation: 682

This is not at all what I asked. I don't want the simplest way, I want the free-est way. Plus, I'm using this as a learning tool. – Thought Space Designs – 2014-09-11T17:13:27.760