Updating DNS Servers in Linux file

Hello, I am trying to write a small fixlet that asks the user for two inputs for new DNS server ip addresses and uses that input to append two lines in two files. The lines start with “DNS1=” and “DNS2=” respectively. Both lines exist in both files.

The files are:
/etc/sysconfig/network-scripts/ifcfg-ens192
/etc/sysconfig/network-scripts/ifcfg-ens224

These files will populate resolv.conf nameserver entries. My thought was to find lines in these files that start with “DNS1” or starts with “DNS2” and append the line with user input after the “=”.

In other words, if line starts with “DNS1” then append line after the first “=” with the user input for primary DNS server. If line starts with “DNS2” then append line after the fist “=” with the user input for the secondary DNS server. Then do this for both files.

This turned out to be well over my head, I have the prompts working just fine, but I have nothing else. I’ve looked in the forums and found similar posts, but nothing close enough to give me the push in the right direction.

Any help would be greatly appreciated. Thank you.

Is there likely to be a DNS IP already in the files that will need to be overwritten or will the entries be blank?

Yes there is already an IP in all entries that will need to be overwritten. Apologies for not including that!

I think something like this would work but would need tested

action parameter query "PrimaryDNS" with description "Enter the primary DNS server IP address:"
action parameter query "SecondaryDNS" with description "Enter the secondary DNS server IP address:"

// Backup the original files
copy "/etc/sysconfig/network-scripts/ifcfg-ens192" "/etc/sysconfig/network-scripts/ifcfg-ens192.backup" 
copy "/etc/sysconfig/network-scripts/ifcfg-ens224" "/etc/sysconfig/network-scripts/ifcfg-ens224.backup"

// Replace the DNS server IP addresses in the original files
appendfile sed -i 's/^DNS1=.*/DNS1={parameter "PrimaryDNS"}/' "/etc/sysconfig/network-scripts/ifcfg-ens192"
appendfile sed -i 's/^DNS2=.*/DNS2={parameter "SecondaryDNS"}/' "/etc/sysconfig/network-scripts/ifcfg-ens192"
appendfile sed -i 's/^DNS1=.*/DNS1={parameter "PrimaryDNS"}/' "/etc/sysconfig/network-scripts/ifcfg-ens224"
appendfile sed -i 's/^DNS2=.*/DNS2={parameter "SecondaryDNS"}/' "/etc/sysconfig/network-scripts/ifcfg-ens224"

move __appendfile "/tmp/dns_update.sh"

wait chmod 775 "/tmp/dns_update.sh"
wait sh -c "/tmp/dns_update.sh"
1 Like

It fails on the move __appendfile “/tmp/dns_update.sh” line. I went to the __append file and did a chmod 777 and could move it manually. I also executed “dns_update.sh” and it worked perfectly.

I added “wait chmod 777 __appendfile” right above “move __appendfile “/tmp/dns_update.sh”” but it still failed on the move command.

Silly question but does /tmp/ exist and does it have space?

The ‘move’ command also fails if the target file already exists (it does not replace the existing file). Try deleting the target file first

delete "/tmp/dns_update.sh"
move __appendfile "/tmp/dns_update.sh"
1 Like

That was it! Thank you for the input!

So the fixlet ran without error but then did not make the updates to the files. I took the -c out of the sh command and that worked.

Thank you everyone!

1 Like