script

Location Based DNS Switching For Internet vs. Intranet

I realized over the weekend that since I run my own mail server out of my home, I can configure my computer to download my mail over the intranet whenever I am on my home network. By doing this, I can drastically reduce my mail download times because it cuts the Internet out of the equation. Rather than using DNS + the Internet to get my mail, I can download it directly from internal IP address of the server.

To understand how to set this up, you have to understand that whenever you use a domain name (like michaeljaylissner.com), your computer does an IP lookup. First, it looks in /etc/hosts to see if it knows the IP of the domain locally. If it does, it will use the IP listed there. If it does not, it will ask your Internet provider what IP to use, and will use that. Thus, what we want to do is set up the computer so that when we are at home, /etc/hosts provides the internal IP of our server, and so when we are not at home, it does not.

When I am at home, I am always on a wireless network called, "pizzapuppysantaclaus." Thus, by checking what wireless network I am connected to, I can check if I am at home, and make whatever changes are necessary. Conveniently, whenever you change network connections, you run all of the scripts located in /etc/network/if-up.d/. Thus, we will put a small script in there that checks what wireless network we are on, and then changes our /etc/hosts file if necessary.

To set up this configuration, I made three files. The first is the script mentioned above, which needs to be owned by root, and placed in /etc/network/if-up.d. You can name it whatever you want, and by changing "pizzapuppysantaclaus" to the name of your network, you can fit it to your needs. Here's the contents of the script:

#First, we check if we are connected to pizzapuppysantaclaus
#If grep has a hit, we're connected, and $? will equal 0, if not, $? will equal 1
iwconfig 2> /dev/null | grep pizzapuppysantaclaus > /dev/null
 
if [ $? = 0 ]
then
  #Switch the /etc/hosts file with the other one
  cp -f /etc/hostsIntranet /etc/hosts
 
  else
  #Switch the /etc/hosts file with the other one
  cp -f /etc/hostsInternet /etc/hosts
 
fi
 
exit 0

This script simply performs a check of our wireless ID. If it's pizzapuppysantaclaus, it switches /etc/hostsIntranet for /etc/hosts. If not, it switches /etc/hostsInternet for /etc/hosts.

The contents of /etc/hostsIntranet are:

192.168.1.132	michaeljaylissner.com
192.168.1.132	charityhikers.org
127.0.0.1	localhost
127.0.1.1	opal
 
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

And /etc/hostsInternet is just a copy of /etc/hosts.

So, to make this whole thing run, put the script in /etc/network/if-up.d, and set its owner to root with execute permission. Create a file called /etc/hostsIntranet, that contains your intranet configuration, as shown above. Make a copy of your normal /etc/hosts file called /etc/hostsInternet.

Once all that's done, you should be all set. Any questions, please feel free to comment!

Delete Original Images from F-Spot and Rotate Using Exif Information

Tagged:  

Over the past couple weeks, my girlfriend and I spent a lot of time working on the photos from our Peru trip. Since we used F-Spot to do our photo editing, when we were done we had a couple of problems when it came to transferring the photos back to her computer which is running Vista.

The first problem was that when we rotated images with F-Spot, it simply changed the exif information for the photo, and didn't change the pixels of the photo themselves. This was fine when viewed in F-Spot because it is aware of exif information, and displays the photos correctly. However, when we transferred the files to her computer, we discovered that Vista does not take exif information into account on the OS level, nor does Picassa. As a result, we needed to somehow rotate the images that had exif information indicating a non-normal rotation.

The second problem we encountered is that in F-Spot when you edit a photo, it creates a second file with the edited photo, and leaves the original unchanged. So, if you edit file dsc00343.jpg, you get a second photo called dsc00343 (Modified).jpg. This is OK when in F-Spot, however, when we went to her computer, it was very hard for her to have ONLY the modified version of those photos, and to delete the originals (since the edited versions are better than the originals).

To solve the first problem, I used a couple of tricks. The first thing I did was to make a copy of the photos in case all went south.

mkdir backupPhotos
cp *.jpg *.jpeg backupPhotos
cd backupPhotos

Once that is done, we can begin rotating images. For this, we will need the jhead program.

sudo aptitude install jhead

Once that's installed, we rotate:
jhead -autorot  *.jpg *.jpeg

This will rotate all of the files that have unusual information in the exif orientation field.

Problem number one solved.

For problem two, we will need to isolate all of the photos that have been modified, and delete the originals. To do this, we will capitalize on the fact that the renamed images use the original pictures name in their name.

To begin with, we create a list of the photos that have been modified:

ls *.jpg *.jpeg | grep -i modified > modifiedImages.txt

Using awk, we can separate out the original name of the photos. The following commands will convert the ')' to '(' and will use the two '(' as a delimiters, returning the name of the file as the first field, and the .jpg or .jpeg as the third field. After that, it will remove any spaces from the file name, and will create a new file with a list of the modified pictures. It sounds complicated, but the final result should work:

tr ')' '(' < modifiedImages.txt > modifiedImages2.txt
cat modifiedImages2.txt | awk -F'(' '{print $1,$3;}' | sed 's/  //g' > modifiedImages3.txt

You should now have a file called modifiedImages3.txt that contains the name of all of the original photos. To delete the pictures in this list from the collection - permanently - run:

rm `cat modifiedImages3.txt`

You should now be able to transfer this entire directory of photos to another computer without rotation issues or duplicated photos.

Syndicate content