Work/Home switcher script

Date January 25, 2009

Recently, i decided to bring my laptop to the work, so i start using it and leaving my work PC as testing machine,, also connecting to online servers through it.

In the work we used a static IP with a static gateway for each department, also with a specific resolv.conf configuration,, at home, i use another configuration for network and internet connection,, so every time i go home, i need to reconfigure the internet,, when i go to the work, i also need to reconfigure the network.. so i decided to write a script so it help me to automate this.

Also as i said, i will use my work PC to connect to online servers, yes i can connect through my laptop, but when i go home i must connect to the work PC so i can connect to the online servers.

mmmmm,, so my idea was to automate the network connection and also if am at the work then each terminal i open it must login directly to my work PC,,, also my mind didn’t work from a long time maybe 3 months and i started to 4get many things so i need to refresh it somehow 😉

the following is the script i wrote:

##Default to be home :S
touch ~/$whereIam;

while true
if [ -f ~/$whereIam ]
sudo /home/idle-boy/scripts/work-home/ 1 > /dev/null
zenity –notification –window-icon=/usr/share/icons/Fedora/48×48/apps/redhat-main-menu.png –text ‘I am in the work!';
rm -rf ~/scripts/work-home/work
sudo /home/idle-boy/scripts/work-home/ 0 /dev/null
zenity –notification –window-icon=/usr/share/icons/HighContrast-SVG/scalable/emotes/face-wink.svg –text ‘OoOpPps am home!?';
touch ~/scripts/work-home/work

The script simply touch a file under ~/scripts/work-home/ named ‘work’,, if the file exist then am in the work,, if the file not exist then am at home,, also i used zenity so it display a notification so when i want to change mode i just click on it.

for internet configuration i wrote the script /home/idle-boy/scripts/work-home/ , and i use sudo to run it as root,, the script is:


if [ $1 -eq 1 ]
## work conf:
#stop network
/etc/init.d/NetworkManager stop
/etc/init.d/network stop
#configure eth0
/sbin/ifconfig eth0 netmask
#bring loopback interface up
/sbin/ifconfig lo up
#add gateway
/sbin/route add -net gw
#insert resolv.conf configuration
#i always lock resolv.conf, so unlock it
chattr -i /etc/resolv.conf
echo ‘search’ >> /etc/resolv.conf
echo ‘nameserver′ >> /etc/resolv.conf
echo ‘nameserver′ >> /etc/resolv.conf
#lock it again
chattr +i /etc/resolv.conf
## home conf:
#bring up the network and NetworkManager
#i use NetworkManager to connect to wlan, so it take everything from the router via DHCP
/etc/init.d/network restart
/etc/init.d/NetworkManager restart
#i don’t like getting resolv.conf configuration via NetworkManager, am home!,, so:
chattr -i /etc/resolv.conf
> /etc/resolv.conf
echo ‘nameserver′ >> /etc/resolv.conf
echo ‘nameserver′ >> /etc/resolv.conf
echo ‘nameserver′ >> /etc/resolv.conf
chattr +i /etc/resolv.conf

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • StumbleUpon
  • Technorati
  • Reddit
  • Webnews
  • MisterWong
  • Y!GG

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>