2

We have a need for assigning IP addressees to raspberry pi's. I want to automate this with a script that can run at logon. The script would ping a range of IP addresses and the first IP address that fails, statically assign the device's interface that IP address.

Does anyone know what that would look like or if its possible?

jnova23
  • 21
  • 1
  • Do you have ONE raspberry pi and you are just trying to find an available address to use???? or do you have MULTIPLE raspberry Pis and you want to assign the first available addresses to the Pis that dont respond from a central ping, and assign them remotely????... Or do you have MULTIPLE raspberry Pis and you want them ALL individually to do this behavior – WU-TANG Oct 07 '20 at 20:45

2 Answers2

0

I can provide something useful, hopefully:

Code taken (and, modified) from:

ping multiple IP's using bash?

#!/bin/bash
# Program name: pingall.sh
date
#/cat /path/tolist.txt |  while read output
cat list.txt | while read output

do
    ping -c 1 "$output" > /dev/null
    if [ $? -eq 0 ]; then

    # a try to assign ip adress:
    sudo ifconfig eth0 192.168.0.1 netmask 255.255.255.0

    echo "node $output is up"
    else

    echo "node $output is down"
    fi
done

a try to assign ip adress:

sudo ifconfig eth0 192.168.0.1 netmask 255.255.255.0

Edit: The ifconfig (...) line is meant to be replaced by the found offline ip of choice.

The bold text Is the thing I modified, and while this will probably not work out of the box; It can hopefully provide some insight about how you would do; Since I am not a expert in bash, I am just hoping this will be useful. (Better than nothing I guess?)

If this is or if anyone finds anything that is , crazy. Totally wrong. Or downright wrong. Please, point that out

William Martens
  • 953
  • 4
  • 18
0

Instead of pinging for an IP address, you could use a part of your MAC address as a unique IP address.

#!/bin/bash

dev=eth0
read -r iface ifstatus mac _ < <( \
    ip -o -br link show $dev) || exit 1

mapfile -td : -s 3 <<< $mac     
printf -v ip '10.%d.%d.%d' ${MAPFILE[@]/#/0x}

echo $ip

#ip addr add $ip/8 broadcast + dev $dev
#ip link set $dev up
#ip route add default via 10.0.0.1 dev $dev