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

Re: IP address depending on the MAC

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}")

    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.


Reply to: