WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Eth0:1 fallback static IP on first boot?  (Read 1622 times)

Offline ovacikar

  • Jr. Member
  • **
  • Posts: 65
Eth0:1 fallback static IP on first boot?
« on: February 25, 2024, 10:27:46 AM »
Hello,

Yesterday I needed to boot a new headless picore 14.1 install, and the MacOS bootpd server i used previously to assign an IP was not working this time.

Would it be possible to assign a static 192.168.0.x IP to eth0:1 on boot , if dhcp fails? the X value can be randomized using the last byte of the MAC address or something?

Code: [Select]
sudo ifconfig eth0:1 $(echo "192.168.0.$((0x$(ifconfig eth0 | grep HWaddr | awk {'print $5'}| awk -F: {'print $6'})))") netmask 255.255.255.0 up



Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11694
Re: Eth0:1 fallback static IP on first boot?
« Reply #1 on: February 25, 2024, 12:57:09 PM »
Hi ovacikar
I suppose you could do something like this:
Code: [Select]
# Wait for network to come up.
SEC=60
while [ $SEC -gt 0 ]
do
        SEC=$(($SEC - 1))
        # If the device is up, break out of the loop.
        ifconfig eth0:1 | grep -q "Bcast:" && break
        sleep 1
done

if [ $SEC -eq 0 ]
        # DHCP failed, so kill the DHCP client.
        pkill udhcpc
        # Assign static address.
        sudo ifconfig eth0:1 192.168.1.200 netmask 255.255.255.0 broadcast 192.168.1.255 up
fi

... if dhcp fails? the X value can be randomized using the last byte of the MAC address or something? ...
Don't do that. You run the risk of colliding with another device
if it has the same address. Have your DHCP server set aside an
address range not to assign and use those for static assignments.

Offline CentralWare

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 776
Re: Eth0:1 fallback static IP on first boot?
« Reply #2 on: March 06, 2024, 01:47:38 PM »
I cannot speak for anyone else's methods, as my way of thinking may not be suited for everyone, but here's how we do things at the shop:

For a potentially headless device (RasPi pico-w included) we first check to see if there's been a successful wired or wireless connection previously; if so we attempt to reconnect using those specs.

IF NOT or if the above attempt fails, we shut down dhcp-client if it's running, clean out all IP related settings and temporarily turn the device INTO an access point.  A remote computer can then connect to the RasPi, configure it as desired and upon disconnecting from it, have it reboot to make the new settings go into effect.

We started doing things in this fashion when we had to come up with a close-to-fool-proof method to build devices which were Alexa/Google compatible where we could configure a device remotely without the fear of duplicate IP addressing, we were able to see what device we were connecting to based on its SSID, etc.  Each device could have the exact same network specs (such as 192.168.0.1/24) and not interfere with any others as they cannot "see" one another.

To accomplish this with a normal RasPi, you'll need dnsmasq to serve as the device's DHCP server (it's tiny and is only needed in "AP MODE.")  Setting it up is very simple since you really only need to tell it what IP ADDRESS RANGE to use, such as the above noted 192.168.0.1/24.  Secondly, you'll need hostapd.  This allows you to create the hot-spot ("Access Point") itself.  Finally, you'd launch dropbear (or OpenSSH if that's your preference) and from there it simply waits for an SSH connection from your remote computer.

I just did a GxxGLE search for the concept; here's a page which might help if you're interested: https://raspberrypi-guide.github.io/networking/create-wireless-access-point

Offline ovacikar

  • Jr. Member
  • **
  • Posts: 65
Re: Eth0:1 fallback static IP on first boot?
« Reply #3 on: March 06, 2024, 02:09:44 PM »
I was looking for something similar to raspbian cmdline.txt, to pass the static Ip on boot by mounting the vfat filesystem only.

https://raspberrypi.stackexchange.com/questions/13464/how-can-i-edit-a-raspbian-sd-card-image-to-use-a-static-ip-address-within-window

Open your SD card installed with Raspbian with Windows Explorer through card reader. You'd find cmdline.txt. Open cmdline.txtand add ip = x.x.x.x after rootwait (x.x.x.x being the intended IP address).

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh ip=192.168.1.145

« Last Edit: March 06, 2024, 02:11:49 PM by ovacikar »

Online Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1267
Re: Eth0:1 fallback static IP on first boot?
« Reply #4 on: March 06, 2024, 05:32:55 PM »
command line can be processed by any program.  it live at /proc/cmdline after boot.

Write a script to do what you want.

Offline patrikg

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 722
Re: Eth0:1 fallback static IP on first boot?
« Reply #5 on: March 07, 2024, 02:47:27 AM »
Yes and you need to change the initrd. Write/change if I am wrong here.
If you pass some the kernel command line options that not correspond to the kernels parameters, the kernel will pass these to environmental variables.
So if you pass ip=213.123.123.123 you can get this via $ip.

You can use that in the bootlocal.sh
Code: [Select]
echo $ip

Offline ovacikar

  • Jr. Member
  • **
  • Posts: 65
Re: Eth0:1 fallback static IP on first boot?
« Reply #6 on: March 07, 2024, 09:40:04 AM »
Got it, however I only need this after downloading a new image. So it needs to be implemented on the original picore image, not the one i have modified.

Maybe I submit it as an enhancement request then.

Offline ovacikar

  • Jr. Member
  • **
  • Posts: 65
Re: Eth0:1 fallback static IP on first boot?
« Reply #7 on: March 07, 2024, 09:53:21 AM »
@Paul_123 sorry for off topic, but did you receive my PM for openvpn? Need to recompile it with current openssl. currently it is downloading old openssl-1.1.1.tcz

Online Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1267
Re: Eth0:1 fallback static IP on first boot?
« Reply #8 on: March 07, 2024, 12:47:35 PM »
I will look at it.  All architectures need updated.