How to identify the local network you are connected to

1

I need a script to be able to identify if the computer is connected to my home network, to my work network, o to a different network, either by wifi or by cable.

My question is if there some network identifier available and how to get it from within a shell script.

Esteban Crespi

Posted 2013-10-18T21:17:10.953

Reputation: 153

parsing the output of hostname -A might work . . . – ernie – 2013-10-18T21:25:33.283

Thanks. I've tried that but it gives the host name repeated twice. I should say that I'm running cygwin under Windows7. – Esteban Crespi – 2013-10-18T21:35:56.873

I just get sethostname: Use the Network Control Panel Applet to set hostname. hostname -s is not supported. (which is odd since I specified -A not -s). ಠ_ఠ – Synetech – 2013-10-19T01:23:50.633

Answers

2

Your home, work, and so on are distinguished by different public IPs, so that you can use any CLI command that returns your public IP address and act upon the outcome of such commands.

Any of these commands will work on a normal Linux distro, I am certain wget exists in Cygwin, I simply do not know about curl and lynx:

wget -q -O - ipecho.net/plain
curl ipecho.net/plain
lynx --dump ipecho.net/plain

Acting on your public address has the added bonus that you can distinguish a different LAN using the same subnet as your work/home/whatnot from your work/home/..., should you ever bring your laptop to a new place.

MariusMatutiae

Posted 2013-10-18T21:17:10.953

Reputation: 41 321

Thanks, this works, though it is a little involved as my home public IP is not static. To know the current IP I am using NO-IP, and I'm having problems resolving the hostname to an IP when at work. – Esteban Crespi – 2013-10-21T13:53:51.953

When at home, the problem is easy. First you ask (dig) what the IP for your NO-IP address is, then you find your IP like I suggested, then you compare the two. As for work, why is it difficult? – MariusMatutiae – 2013-10-21T14:25:20.283

AT work there are lots of security settings: a firewall and a proxy that make almost impossible to do anything. for example 'dig +short www.google.com' is blocked and doesn't answer anything, in addition all the dynamic hosts are filtered out so I can't use ping or something similar. I have finally managed it using networks-tools.com as in curl -q http://network-tools.com/default.asp?host=myhostname | grep -oE '<br/>[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | cut -c6- but surely there has to be a simpler and more reliable way to do it!! – Esteban Crespi – 2013-10-21T15:16:47.687

Way to go. That's a problem whose solution you'll have to figure out all by yourself. Pls upvote my answer if you think I helped you. – MariusMatutiae – 2013-10-21T15:23:25.143

3

If there is a simple command to do this, I am not aware of it. What might work is the following:

wireless=<put name of wireless card here, e.g. wlp2s0>
wired=<put name of wired network adapter here, e.g. p1p1>
dev=$(ip route | grep default | awk '{print$5}') #finds the device that your Internet is coming through
if [ x$dev = x$wired ]; then
    echo "Wired connection."
elif [ x$dev = x$wireless ]; then
    echo -n "Wireless connection: "
    iwconfig $wireless 2>&1 | grep ESSID | sed 's@.*ESSID:\"\(.*\)".*@\1@' # prints ESSID of wireless connection only
fi

That should work, as long as you don't forget to fill out the wired/wireless variables.

EDIT You definitely SHOULD say that you are running cygwin under Windows. This script most likely won't work under cygwin. I have never used cygwin so I don't know the level at which it imitates Linux. If this script doesn't work, you are out of luck (I think).

BenjiWiebe

Posted 2013-10-18T21:17:10.953

Reputation: 7 672

Thanks for your answer, it seems that neither the ip nor the iwconfigare avaliable for cygwin. I've found however that the command: netsh wlan show interfaces | grep [^B]SSID gives me the SSID of the wlan (not the ESSID though). I can also check if I'm wired or not parsing the output of netsh interface show interface | Conectado. What I can't figure out is how to know if I'm wired at home or at work. – Esteban Crespi – 2013-10-18T23:05:25.167

The computer itself might not know whether you are at home or work wired connection. Does your computer have a different internal IP between home and work? You could try testing that. Also, what LAN IP's can be pinged at work/home? – BenjiWiebe – 2013-10-18T23:30:29.507

-1

Are the subnet masks different based upon which network you connect to? Your home will be 255.255.255.0 while your work will VLSM out at 255.255.255.254 or less.

Check via command line in Windows with ipconfig and check the subnet mask with ifconfig for Linux.

Another way to tell would be to DNS resolve from your computer and see what DNS picks it up, is it your ISP that you use at home or the DNS servers under your offices domain? That would also tell you, i mean a look at your routing tables also.

Check this out and see if you first routes are from inside your companies domain, run this from within command prompt. Find command prompt and open it and type:

netstat -f

 TCP    10.0.0.145:56433       stackoverflow.com:http  ESTABLISHED  
 TCP    10.0.0.145:56462       stackoverflow.com:http  ESTABLISHED   
 TCP    10.0.0.145:56592       stackoverflow.com:http  ESTABLISHED
 TCP    10.0.0.145:56748       stackoverflow.com:http  ESTABLISHED
 TCP    10.0.0.145:56782       stackoverflow.com:http  ESTABLISHED
 TCP    10.0.0.145:56902       stackoverflow.com:http  ESTABLISHED
 TCP    10.0.0.145:56949       stackoverflow.com:http  ESTABLISHED
 TCP    10.0.0.145:57056       stackoverflow.com:http  ESTABLISHED

Example above shows where we are now, the IP's are resolved because of our -f to this site it should be. Someone said you are running Cygwin in the Windows environment. Why don't you just add the "cygwin\bin" directory to your windows path? You'll have full access to most GNU tools, and can use them in pretty much the same way you do on Linux.

LinuxLuther

Posted 2013-10-18T21:17:10.953

Reputation: 1