Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
parsing the output of hostname -A might work . . . –  ernie Oct 18 '13 at 21:25
    
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 Oct 18 '13 at 21:35
    
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 Oct 19 '13 at 1:23

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
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 Oct 21 '13 at 13:53
    
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 Oct 21 '13 at 14:25
    
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 Oct 21 '13 at 15:16
    
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 Oct 21 '13 at 15:23

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).

share|improve this answer
    
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 Oct 18 '13 at 23:05
    
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 Oct 18 '13 at 23:30

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.

share|improve this answer

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.