0

When no IP address is obtained via DHCP, I want to set a static IP, which I wrote in a text file before.

Content of my staticIP.txt:

10.10.0.254

So far, I get a static IP when DHCP ist not working. I edited the /etc/dhcp/dhclient.conf for this:

timeout 10;
lease {
interface "eth0";
fixed-address 10.0.0.10;
option subnet-mask 255.255.255.0;
renew 2 2022/1/1 00:00:01;
rebind 2 2022/1/1 00:00:01;
expire 2 2022/1/1 0:00:01;
}

(Source: Set static ip if not obtained from DHCP (script))

Now I want to overwrite "fixed-address 10.0.0.10;" with the static IP "10.10.0.254" in my staticIP.txt, when no DHCP is obtained.

All I was able to do so far is replacing the "fixed-address ;" block with sed and write it to the file:

sed -i 's/fixed-address [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}/<MANUAL ENTERED IP>/g' /etc/dhcp/dhclient.conf

Two things are missing right now:

  1. Extracting the IP from staticIP.txt and overwrite "fixed-address [IP];"

  2. Check if no IP was obtained via DHCP and run the sed command

I'd be glad if someone could help me.

Thanks a lot.

Community
  • 1
  • 1

1 Answers1

0

Why keep one IP-address in the file? Not it be better to add it directly to a regular expression?

sed -i 's#fixed-address 10.0.0.10;#fixed-address 10.10.0.254;#' /etc/dhcp/dhclient.conf
Den Vakxden
  • 53
  • 1
  • 7