#!/usr/bin/perl ##### FUNCTION # To read ip settings from a machine generated file and # write those ip settings to the dhcpcd.conf file. # This script is designed to be started by a systemd service.path # The service monitors the mb_ip.cfg file for change. When the file is changed # the service calls this script. # Updated ip settings are applied after reboot. ##### INPUT # Takes user updates of IP settings saved in to the file /home/mimir/mb_ip.cfg # The user setting are range checked by the app before being saved to file. # The file mb_ip.cfg will only have ip settings that are valid (but not necessarily correct). ##### OUTPUT # Writes the ip settings to the /etc/dhcpcd.conf file. # # Reference: https://www.perlmonks.org/?node_id=11136353 # # Dazz # ver 1.3 # 8 Sept 2021 use strict; use warnings; use Path::Tiny; use Data::Dump; ###### Input file with ip settings my $ip_filename = '/home/mimir/mb_ip.cfg'; # The ip info entered by the user into the GUI ###### Output dhcp configuration file # my $dhcpcdfile = '/etc/dhcpcd.conf'; my $dhcpcdfile = 'd.conf'; # TEST ############################################################################################ use constant { FALSE => 0; TRUE => 1; }; # Load the input parameters ip_params from the input file my %ip_params; %ip_params = ( %ip_params, # add new data to defaults, split with "=" path($ip_filename)->slurp =~ /^(\w+)=(.*?)\s*$/gm ); # Print the contents of ip_params. #TEST use Data::Dump 'dd'; dd 'ip_params', \%ip_params; #TEST # Convert 2 lines DNS to 1 line DNS. $ip_params{domain_name_servers} = $ip_params{domain_name_server_1}." ".$ip_params{domain_name_server_2}; delete($ip_params{domain_name_server_1}); delete($ip_params{domain_name_server_2}); # Print to check the contents ip_params with combined DNS line. #TEST use Data::Dump 'dd'; dd 'ip_params', \%ip_params; #TEST { # block scope for locals local @ARGV = $dhcpcdfile; local $^I = '.bak'; # make backup, do inplace edit my $foundinterface = FALSE; while( <> ) { if( /^\s*profile\s+static_$ip_params{interface}\b.*\n/m ) # look for profile with matching interface name { # format matches 'static profile_eth0' $foundinterface = TRUE; warn "found section for $ip_params{interface} at line $.\n"; } elsif( $foundinterface and /static/ ) { warn "ip param key : $ip_params{$0}\n"; #TEST warn "ip param val : $ip_params{$1}\n"; #TEST ***ERROR: $1 not initiated. Lines 72 and 73*** s/^\s*static\s+(\w+)=\K.*/$ip_params{$1}/m or warn "failed to change $1"; # match string before \K Return string after \K # ?? What increments $ip_params through the hash ?? # ?? What stops over-writing of other later sections ?? eg. wlan0 warn "set $1 to $ip_params{$1}\n"; } elsif( $foundinterface ) { $foundinterface = FALSE; warn "ending changes at line $.\n"; } # Output line to dhcpcdfile print; } } #### #IP Configuration #Mon Sep 06 14:29:34 NZST 2021 routers=192.168.9.91 interface=eth0 domain_name_server_2=8.8.4.9 domain_name_server_1=8.8.8.9 ip_address=192.168.10.9/24 #### #static domain_name_servers=192.168.1.1 # fallback to static profile on eth0 #interface eth0 #fallback static_eth0 ### Static LAN IP profile for Mimirbox profile static_eth0 static ip_address=1.1.1.1/24 static routers=1.1.1.1 static domain_name_servers=1.1.1.1 2.2.2.2 # Access Point Static interface mimirbox wlan0 interface wlan0 static ip_address=192.130.2.20/24