Archive for November, 2009
Using Quicksilver to edit hosts file
This is mostly just a follow up for my previous post on using a shell script to add and remove test domains in the hosts file. I have created two small applescripts for use in Quicksilver to execute that script. If you need to use these you place them in ~/Library/Application Support/Quicksilver/Actions and set your password. (Beware this could pose a threat, since you will have your password in cleartext on your harddrive).
The two scripts are almost identical. Actually the only difference is the action the shell script should call (add or remove). The rest of the scripts are completely the same.
I call this file addhost.
using terms from application "Quicksilver" on process text txt set the_password to "YOURPASSWORD" repeat with delimiter_position from 1 to (length of txt) if character delimiter_position of txt = " " then exit repeat end repeat if delimiter_position = (length of txt) then set hostname to txt as string set ipaddress to "" else set hostname to characters 1 thru (delimiter_position - 1) of txt as string set ipaddress to characters (delimiter_position + 1) thru (length of txt) of txt as string end if do shell script "sudo hosts.sh add " & hostname & " " & ipaddress password the_password with administrator privileges return nothing end process text end using terms from
I call this file removehost.
using terms from application "Quicksilver" on process text txt set the_password to "YOURPASSWORD" repeat with delimiter_position from 1 to (length of txt) if character delimiter_position of txt = " " then exit repeat end repeat if delimiter_position = (length of txt) then set hostname to txt as string set ipaddress to "" else set hostname to characters 1 thru (delimiter_position - 1) of txt as string set ipaddress to characters (delimiter_position + 1) thru (length of txt) of txt as string end if do shell script "sudo hosts.sh remove " & hostname & " " & ipaddress password the_password with administrator privileges return nothing end process text end using terms from
Now I am able to create a local domain by opening Quicksilver (double-control in my case) press period, write the domain name, press tab, and select the addhost action (ad is enough in my case). Removing is just as easy – the last step is just to select the removehost action (re is enough in my case).
Bash script for editing the hosts file
I just read a post about local development in apache by Jesper Rasmussen and thought one thing was missing. I usually test my sites and applications locally with the real domain – to ensure that functionality based on the url works as expected. However this means editing the hosts file several times a day. Now I have made a small shellscript that will add and remove lines from the hosts file.
#! /bin/bash
DEFAULT_IP=127.0.0.1
IP=${3:-$DEFAULT_IP}
case "$1" in
add)
echo "$IP $2" >> /etc/hosts
;;
remove)
sed -ie "\|^$IP $2\$|d" /etc/hosts
;;
*)
echo "Usage: "
echo "hosts.sh [add|remove] [hostname] [ip]"
echo
echo "Ip defaults to 127.0.0.1"
echo "Examples:"
echo "hosts.sh add testing.com"
echo "hosts.sh remove testing.com 192.168.1.1"
exit 1
;;
esac
exit 0
As you can see the script defaults the ip to 127.0.0.1 for easy creation of local domains. Next step is to create a quicksilver (and a gnome do) plugin for easy creation without ever touching the terminal. (Even though we all love the terminal, right?)