When changes are made to `/etc/network/interfaces` on a server system without physical or kvm switch access, it comes in handy to have a script that resets your network configuration to the previous state and restarts the networking in case of network connectivity loss.
Here is a little bash script that checks if it can reach a certain server via icmp ping after a few seconds of applying the new configuration and resets `/etc/network/interfaces` to your backup file, followed by a `service networking restart` command, if it cant reach the outside world any more. Make sure to execute this command in a `screen` session, otherwise it might be interrupted by your ssh session being closed.
#!/bin/bash ## Simple /etc/network/interfaces test script ## VARIABLES # Path to new /etc/network/interfaces configuration file: new_interfaces="/etc/network/interfaces.new" # Original /etc/network/interfaces configuration file: current_interfaces="/etc/network/interfaces" # How long to wait in seconds before restoring original config test_delay=5 # Ping check host ping_host="188.8.131.52" ## MAIN # Backup original /etc/network/interfaces cp -v $current_interfaces $current_interfaces.bak # Apply new configuration cat $test_interfaces > current_interfaces service networking restart # Wait a little sleep $test_delay # Test network connectivity via icmp ping. If it fails, restore $current_interfaces.bak file if [ ! "$(ping -q -c1 $ping_host)" ]; then cat $current_interfaces.bak > $current_interfaces service networking restart fi # You can also just wait and then restore the old configuration if you do not want to trust the ping check. You might want to set a higher $test_delay for this case to be able to test your networking # sleep $test_delay # cat $current_interfaces.bak > $current_interfaces # service networking restart exit 0