LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   How to identify a network? (https://www.linuxquestions.org/questions/linux-networking-3/how-to-identify-a-network-815423/)

catkin 06-21-2010 08:57 AM

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.

allend 06-21-2010 09:11 AM

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.

ilikejam 06-21-2010 10:40 AM

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

catkin 06-21-2010 12:45 PM

Quote:

Originally Posted by allend (Post 4010236)
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.

catkin 06-21-2010 12:54 PM

Quote:

Originally Posted by ilikejam (Post 4010346)
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.

fruttenboel 06-21-2010 01:25 PM

Quote:

Originally Posted by catkin (Post 4010223)
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.

catkin 06-22-2010 03:26 AM

Thanks fruttenboel :)
Quote:

Originally Posted by fruttenboel (Post 4010560)
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 (Post 4010560)
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 (Post 4010560)
But at StarBucks you will have to run with a random IP address.

No problem

catkin 06-22-2010 03:27 AM

Quote:

Originally Posted by catkin (Post 4010514)
... 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 )



All times are GMT -5. The time now is 01:26 AM.