Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@juanvgarcia
Created November 13, 2013 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanvgarcia/7443442 to your computer and use it in GitHub Desktop.
Save juanvgarcia/7443442 to your computer and use it in GitHub Desktop.
Script I use to change the static network configuration for a VM (I have different network configs for home/work). This script will expect comments of the form #work config start or #home config start to mark each configuration section. I complement it with two alias that include sudo on ~/.bash_profile.
#!/bin/sh
if [ "$1" != "work" ] && [ "$1" != "home" ];
then
echo "Wrong arguments Usage: $0 home|work"
exit $E_BADARGS
fi
interface_file='/etc/network/interfaces'
work_config_start=`grep -n "#work config start" $interface_file | awk -F':' '{print $1}'`
work_config_end=`grep -n "#work config end" $interface_file | awk -F':' '{print $1}'`
home_config_start=`grep -n "#home config start" $interface_file | awk -F':' '{print $1}'`
home_config_end=`grep -n "#home config end" $interface_file | awk -F':' '{print $1}'`
if [ "$1" = "work" ];
then
start_to_comment=`expr $home_config_start + 1`
end_to_comment=`expr $home_config_end - 1`
start_to_uncomment=`expr $work_config_start + 1`
end_to_uncomment=`expr $work_config_end - 1`
else
start_to_comment=`expr $work_config_start + 1`
end_to_comment=`expr $work_config_end - 1`
start_to_uncomment=`expr $home_config_start + 1`
end_to_uncomment=`expr $home_config_end - 1`
fi
sed -i.bak "$start_to_comment,$end_to_comment s/^/#/g" $interface_file
sed -i.bak "$start_to_uncomment,$end_to_uncomment s/^#//g" $interface_file
service networking restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment