Tweeting My Machine’s IP
So I wanted to connect to my ubuntu server, unfortunately I don’t have a static ip and I’m not planning to get one, so i had to find a work around.
My first thought was to develop an app that reads my IP from http://www.whatsmyip.net and then publishing it somehow whenever it changes, but later on i decided to tweet it instead because given my connection state my inbox would be buried in one lined emails, not to mention its too much of a hassle to actually log in and read it, as a result I decided to tweet it instead, also i decided to use Java instead of shell scripting because i have another windows machine that i want to deploy this on it as well.
First of all i located an easy to use twitter java library which turned out to be twitter4j, starting from there it wasn’t that challenging. It reads whatsmyip.net html page, parsing the <title> tag which includes the ip address, then checks ip against the last posted ip if they are equal nothing happens if they are different a new ip is posted, this happens every 4 minutes (which is think is too frequent but well you can change it if you want to).
I included the source code, as well as the twitter4j library…ENJOY
How to :
- modify file tweeterer to include your twitter user name and password (dont forget to keep that twitter account private using twitter security features
- Recompile using javac
- use java -jar IPTweeter.jar
- to make sure that this runs whenever the machine starts by placing a simple script in your /etc/init.d/ folder.
- or you can just add it to the crontab