Just to be clear it's not a one way toggle. It will be on/off mechanism, where on does the optimization by changing the mtu values, while off restores the default mtu value. All this without requiring any additional dependencies.
Just Store the default mtu value somewhere beforehand. It's just a 4 digit number. If network broke then it can be toggled off. So old 4 digit mtu value will be restored. Also please read my post carefully. I suggested to use ping command to make sure network works after changing to new mtu value otherwise it won't change. I think mtu value depends on hardware of the computer and not of the router. It's like how big a single packet can be sent at once to the router. I believe that network won't break for any an ideal scenario where user intects just with the toggle for mtu optimization.
So if toggled on,
Script fetches a list of network devices,
Suppose wlo2 and eno2 are the wifi and ethernet, the script find their mtu values by
ifconfig | grep mtu
It redirects the output and stores it to newly created mtu.oldconf file somewhere. It will contain 2 four digit number.
Now it fetches the lowest number in mtu.oldconf
Then gives it to a variable,
Now in a loop it keeps on increasing $mtubase by 4 and each time runs
ping -M do -s $mtubase <ip-address>
being the local ip of the interface you wish to check.
Note there is an additional 28 bytes as a header when using this method.
Just keep increasing the mtu size (in the ping command) until you get a error. On such an error stop the loop and subtract 4 from mtubase variable to get maximum MTU size, and add 28 to it. Now
ip link set <interface name> mtu $mtubase
Check using ping if network is reachable.
Otherwise restore orignal base value of mtubase in mtu.oldconf
That's it. Now the network should be far better.
If user toggled off, restore the value from mtu.oldconf
If it doesn't exist or got deleted, consider mtubase as 50 and run the program again.
I think pros of this outweigh the cons.
Also if a power failure occurs reboot restores orignal mtu values. Breaking of such a simple mechanism is highly unlikely and can anyways be restored using the mtu.oldconf backup presets. If that also doesn't exist then it can be easily recalibrated by considering mtubase value as very small value like 100. In a few minutes system will be restored. I think this function is like deploy and forget. Just the way you have update option in garuda assistant