[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: IP address depending on the MAC



Thank you, Chris, for the idea. The IP addresses would be randomly distributed, while I would like to keep them in a contiguous range.

On 03.10.2011 18:52, Chris Davies wrote:
Javier Barroso<javibarroso@gmail.com>  wrote:
If you know which macs you will have, you can make an init script
which generate /etc/network/interface and /etc/hostname and /etc/hosts
the first boot and then remove itself.

You don't even need to know in advance the MACs as long as enough of
the address is unique.

$ cat /etc/init.d/10setnetworkonlyfirstboot
#!/bin/bash
#>  mymac=$(ip add show dev eth0 | awk '/ether/ {print $2}')
#>  myhost=$(awk "/$mymac/ {print \$3}")
#>  myip=$(awk "/$mymac/ {print \$2}")

myhex=$(
     ip add show dev eth0 |                  # Get the interface detail
     awk '/ether/ {print toupper($2)}' |     # Extract MAC
     cut -d: -f6                             # Get last octet (as hex)
)
mydec=$( echo "16 i $myhex p" | dc )        # Convert hex to dec
myip="192.168.1.$mydec"                     # Must guarantee not 0 or 255


sed -i "s/IP/$myip" /etc/network/interfaces # and probably some
changes in /etc/hosts
sed -i "s/HOSTNAME/$myhost" /etc/hostname # and probably some changes
in /etc/hosts
rm -f /etc/init.d/10setnetworkonlyfirstboot

Rather than removing the configuration file, you could add, say,
"# Configured eth0" to /etc/network/interfaces during setup. Next time
just exit if that line exists in the file.

--
Ireneusz (Irek) Szczesniak
http://www.irkos.org


Reply to: