LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 06-21-2010, 08:57 AM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
How to identify a network?


Hello

Is there a "best practice" way to identify a network?

A portable computer may be connected to one of several networks (home, office, public hotspot ...). Ideally it would identify the network and configure itself accordingly (mount specific networked file systems, start sshd or not ...).

Thinking so far ...
  1. The network's IP address is next to useless with so many networks being 192.168.1.0/24 or similar.
  2. Nodes at specific addresses with specific services is too detailed, cumbersome to maintain and anyway such nodes may not be up.
  3. Perhaps the best way to identify a network is by the MAC address(es) of its gateway(s) -- and their IP addresses are conveniently given by the DHCP server.
But how to get the MAC addresses? nmap will do it but takes ~14 seconds; too long. rarp is obsolete, according to its man page: "This program is obsolete. From version 2.3, the Linux kernel no longer contains RARP support. For a replacement RARP daemon, see ftp://ftp.dementia.org/pub/net-tools" (address did not load).

Failing a gateway's MAC solution I did try setting the home LAN to the deliberately obscure 10.35.136.0/28 but was unable to ping the WAN default gateway or DNS server from 10.35.136.7 via LAN gateway 10.35.136.1. Could ping the gateway. Maybe too obscure for the ADSL modem/router? Or netadmin error?

In case it matters, I'm not looking for a very secure solution, more of a practicable convenience.
 
Old 06-21-2010, 09:11 AM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 3,406

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
I like using wicd to address this problem as it allows you to setup profiles for each connection that you make. You can use the post connect scripting options to do things like mount network shares.
 
Old 06-21-2010, 10:40 AM   #3
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

The 'arp' command should work. Maybe something like:
Code:
#!/bin/bash
ROUTER=`route | grep '^default' | tail -n 1 | awk '{print $2}'`
/sbin/arp | grep '^'$ROUTER | awk '{print $3}'
Dave
 
Old 06-21-2010, 12:45 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by allend View Post
I like using wicd to address this problem as it allows you to setup profiles for each connection that you make. You can use the post connect scripting options to do things like mount network shares.
Thanks allend

Have just started using wicd and saw the profiles. Presumably it's a manual thing rather than automatic.
 
Old 06-21-2010, 12:54 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by ilikejam View Post
Hi.

The 'arp' command should work. Maybe something like:
Code:
#!/bin/bash
ROUTER=`route | grep '^default' | tail -n 1 | awk '{print $2}'`
/sbin/arp | grep '^'$ROUTER | awk '{print $3}'
Dave
Thanks for the idea and the code Dave

What I forgot to put in the OP is that I'd tried it and the ARP cache was not populated (except for the local machine) during boot but you set me to thinking -- perhaps pinging the default gateway would be enough to put it in the ARP cache. I'll try it and post back.
 
Old 06-21-2010, 01:25 PM   #6
fruttenboel
Member
 
Registered: Jul 2008
Posts: 270

Rep: Reputation: 48
Quote:
Originally Posted by catkin View Post
Hello

Is there a "best practice" way to identify a network?
I set up my laptops to be DHCP clients. In my router I force a dedicated IP address to a specific MAC address. So at home, I will always have the same IP address. At work you could do something similar. But at StarBucks you will have to run with a random IP address.
 
Old 06-22-2010, 03:26 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Thanks fruttenboel
Quote:
Originally Posted by fruttenboel View Post
I set up my laptops to be DHCP clients.
AFAIK that is the only sane choice. In case they may not be secure, it would be nice to ignore them and use reputable public DNS servers. In case of running a smart caching nameserver like dnsmasq and in case the DNS servers supplied by the DHCP server are not the fastest it would be nice to incorporate them in the list used by dnsmasq and let it choose the best.
Quote:
Originally Posted by fruttenboel View Post
In my router I force a dedicated IP address to a specific MAC address. So at home, I will always have the same IP address. At work you could do something similar.
Nice if you can do it but SOHO routers increasingly lack that facility and sys/netadmins at work may not be willing.
Quote:
Originally Posted by fruttenboel View Post
But at StarBucks you will have to run with a random IP address.
No problem
 
Old 06-22-2010, 03:27 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by catkin View Post
... perhaps pinging the default gateway would be enough to put it in the ARP cache. I'll try it and post back.
Works perfectly

EDIT: here's bash script to do it
Code:
found=
while read destination gateway _
do
    [[ $destination = default ]] && found=yes && break
done <<< "$( route )"

if [[ $found ]]; then
    found=
    ping -c 1 -q $gateway >/dev/null # populate ARP cache
    while read address _ hw_address _
    do
        [[ $address = $gateway ]]  && found=yes && break
    done <<< "$( arp )"
fi

[[ $found ]] && echo "Gateway HW address is $hw_address" || echo 'Gateway HW address not found' >&2
EDIT2:

More elegant code, thanks to konsolebox in this LQ thread
Code:
    # Get the default gateway hardware address
    found=
    while read -u 3 destination gateway _
    do
        if [[ $destination = default ]]; then
            ping -c 1 -q $gateway >/dev/null # populate ARP cache
            while read -u 4 address _ hw_address _
                do
                    [[ $address = $gateway ]] && found=yes && break
                done 4< <( arp )
            fi
    done 3< <( route )

Last edited by catkin; 06-22-2010 at 04:46 AM.
 
  


Reply

Tags
gateway, identify, lan, mac address, network


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify network ID which is creating spam! Md.Abul Quashem Linux - Desktop 2 02-23-2010 11:23 AM
Identify route for some network clients without netmask ? hodhod Linux - Networking 1 06-10-2009 09:27 PM
How do I identify ESSID of a wireless network? rickh Linux - Wireless Networking 3 05-15-2006 05:33 PM
identify a network packet alaios Linux - Networking 4 04-19-2006 07:54 PM
identify application causing network traffic maenho Linux - Software 2 03-03-2005 09:24 AM


All times are GMT -5. The time now is 12:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration