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:

#!/bin/bash
##Default to be home :S
whereIam=’scripts/work-home/work’
touch ~/$whereIam;

while true
do
if [ -f ~/$whereIam ]
then
sudo /home/idle-boy/scripts/work-home/net_conf.sh 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
else
sudo /home/idle-boy/scripts/work-home/net_conf.sh 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
fi
done

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/net_conf.sh , and i use sudo to run it as root,, the script is:

#!/bin/bash

if [ $1 -eq 1 ]
then
## work conf:
#stop network
/etc/init.d/NetworkManager stop
/etc/init.d/network stop
#configure eth0
/sbin/ifconfig eth0 192.100.80.196 netmask 255.255.254.0
#bring loopback interface up
/sbin/ifconfig lo up
#add gateway
/sbin/route add -net 0.0.0.0 gw 192.100.81.9
#insert resolv.conf configuration
#i always lock resolv.conf, so unlock it
chattr -i /etc/resolv.conf
>/etc/resolv.conf
echo ‘search maktoob.com’ >> /etc/resolv.conf
echo ‘nameserver 127.0.0.1′ >> /etc/resolv.conf
echo ‘nameserver 192.100.81.9′ >> /etc/resolv.conf
#lock it again
chattr +i /etc/resolv.conf
else
## 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 127.0.0.1′ >> /etc/resolv.conf
echo ‘nameserver 212.118.0.1′ >> /etc/resolv.conf
echo ‘nameserver 212.118.0.2′ >> /etc/resolv.conf
chattr +i /etc/resolv.conf
fi

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • 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=""> <strike> <strong>