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

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

Jane
  • 1,085
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