#!/usr/bin/perl use strict; use warnings; use Tie::File; use feature "switch"; no warnings 'experimental'; # Declare constants use constant false => 0; use constant true => 1; # Declare variables my $ip_filename = '/home/mimir/mb_ip.cfg'; # The ip info entered by the user into the GUI my $dhcpcd_filename = '/home/mimir/d.conf'; # The dhcp configuration file to be written to #my $dhcpcd_filename = '/etc/dhcpcd.conf'; # A hash of config parameters to be updated in order # of how they should be saved. # Initiate with reasonable start values. my %ip_params = ( interface => "usb0", ip_address => "1.1.1.0", routers => "127.0.0.0", domain_servers => "1.1.1.1"); my $ip_line; # whole parameter line my @ip_fields; # parameter fields in a line # The list of saved user param variables tie my @ip_array, 'Tie::File', $ip_filename or die "Cannot tie file '$ip_filename': $!"; ########### Get param values from the GUI input file mb_ip.cfg ### Read the values from mb_ip.cfg and save to a hash for $ip_line (@ip_array) { @ip_fields = split /=/, $ip_line; # get the parameter name and value given ($ip_fields[0]){ when ( /interface/ ) { $ip_params{'interface'} = $ip_fields[1]; } when ( /ip_address/ ) { $ip_params{'ip_address'} = $ip_fields[1]; } when ( /routers/ ) { $ip_params{'routers'} = $ip_fields[1]; } when ( /domain_name_servers/ ) { $ip_params{'domain_servers'} = $ip_fields[1]; } default { print "no match to params\n\n" } } } print "Network parameters loaded into the array \n"; for(keys %ip_params){ print(" $_ is $ip_params{$_}\n"); } untie @ip_array; ########### Find the lines in the config file to be replaced. # Find the required interface section # Initiate the boolean variables that only change one parameter # in the config file section. # When all are found and changed, jump out. my $isFoundInterface = false; my $isFoundIP = false; my $isFoundRouters = false; my $isFoundDNS = false; # The config file to search and replace variables tie my @dhcpcd_array, 'Tie::File', $dhcpcd_filename or die "Cannot tie file '$dhcpcd_filename': $!"; print " \n\n\n"; # Find and replace the config lines for $ip_line (@dhcpcd_array){ print "$ip_line\n"; # look for 'profile static_eth0' that marks start of the section if ( $ip_line =~ /^.*profile\s*static_$ip_params{'interface'}\s*/ ) { $isFoundInterface = true; # Found the first line of the section. print "############## Found profile line\n" ; next; # Jump to the next line in the file. } # When the profile interface section is found, look for the parameters to change if ( $isFoundInterface == true ) { given ($ip_line){ when ( /^.*static\s*ip_address=/ ) { $ip_line = " static ip_address=$ip_params{'ip_address'}"; $isFoundIP = true; } when ( /^.*static\s*routers=/ ) { $ip_line = " static routers=$ip_params{'routers'}"; $isFoundRouters = true; } when ( /^.*static\s*domain_name_servers=/ ) { $ip_line = " static domain_name_servers=$ip_params{'domain_servers'}"; $isFoundDNS = true; } default { if ($isFoundInterface and $isFoundIP and $isFoundRouters and $isFoundDNS ) { last; } #stop searching the config file when changes all done. } } } print "isIP : $isFoundIP\n"; print "isRouters : $isFoundRouters\n"; print "isDNS : $isFoundDNS\n\n"; print "looking for the ip config params\n"; } untie @dhcpcd_array;