Changing Internet configuration from command line in FreeBSD

Imagine a situation when Internet configuration must be changed on the fly which means without rebooting OS or editing configuration files.
The following commands can do this:
ifconfig em0 inet 192.168.1.4 netmask 255.255.255.0 route delete default route add default 192.168.1.1 echo "nameserver 8.8.8.8" > /etc/resolv.conf
Where:
- em0 - Ethernet interface with Internet access;
- 192.168.1.4 - IP address;
- 192.168.1.1 - default gateway;
- 8.8.8.8 - DNS server.
But, what if you need to switch between several Internet configurations? For example: you have installed FreeBSD on your laptop and you need to have access to Internet when you bring your laptop to work. It would be difficult to change configuration manually every time you are at work. For this purpose the following script is the best solution.
Create a file switch-internet-configuration with the content:
#!/bin/sh case $1 in "work" ) echo "Start to switch to '$1' internet configuration" ifconfig em0 inet 192.168.1.4 netmask 255.255.255.0 route delete default route add default 192.168.1.1 echo "nameserver 192.168.1.1" > /etc/resolv.conf echo "Switching to '$1' internet configuration is finished" ;; "home" ) echo "Start to switch to '$1' internet configuration" ifconfig em0 inet 192.168.0.4 netmask 255.255.255.0 route delete default route add default 192.168.0.200 echo "nameserver 192.168.0.200" > /etc/resolv.conf echo "Switching to '$1' internet configuration is finished" ;; * ) echo "The internet configuration does not exist" ;; esac
To switch Internet configuration to "home", run this command:
./switch-internet-configuration home
To switch Internet configuration to "work", run this command:
./switch-internet-configuration work
Before you use this script do not forget to change: Ethernet interface, IP address, default gateway and DNS server.