Call Home Script

A year or two ago when I first set up my old laptop as a linux server I created a script that logged into my router, parsed the external ip address, checked that against the ip address it got last time, and emailed me if they were different. That worked great for the intended purpose (telling me when the address of my server changed), but I was thinking recently that I could rewrite it to tell me whenever the external ip address of the computer changes, such as if someone stole one of my linux computers and booted it far enough for my cron job to be able to fire. That depends on the computer being connected to the internet when the script runs, but updating the script was interesting enough to be worth doing even if there’s very little chance of it being overly useful.

I’m building off of code I wrote a while ago, so the parts that work won’t have much commentary on what it took to write them.

Calling Home

The first (and most important part) is the script to notify you when it decides it needs to. I chose email because I expected it to be pretty easy (it was) and because always having an email in my inbox that contained the ip address of my server seemed useful.

A quick search on “python send email” turned up a docs.python.org page with the basics, so that’s might have been where I started. I wanted a general script that I could call from other places, but I didn’t want to include the login info for the server every time so I ended up with this script.

The username, password, and email address are obviously not actually all ‘*’s, but I didn’t particularly want to post my gmail info. The smtpuser that my version of the script uses is everything before the @ in my gmail address, and the smtppass has to be a valid password for your account, though interestingly it was willing to accept my last password (my password changed after I wrote the script and it still worked). Giving it an invalid password raised an exception at session.login(smtpuser, smtppass) with a link to a gmail support page.

The documentation example I linked above used another library for setting up the body of the message, but joining the header lines I have with line breaks works too. The "", # extra line break comment is a required blank line for it to parse correctly as an email.

Checking your IP

Once your script can notify you, it has to be able to tell when it needs to. My original script logged onto my router (a linksys), opened the status page, and parsed the external ip. That obviously depends on your computer being at home where it’s able to log into the router. A more general solution is to check a website for the ip address it sees you connecting from.

Since I have web hosting and wasn’t sure that sites like whatismyip would like being called regularly from a script I looked up the php to display the external ip of the requesting computer. I don’t really want my site to be hit be any random person looking for a machine-friendly ip address site, so it’s in a passworded section of my site, but here’s the html of my getip.php file:

<html><body>
<?php echo $_SERVER['REMOTE_ADDR']; ?>
</body></html>

Note: All block code formatting and syntax highlighting is done through code2html.

Since I’m fetching the ip address from an html page behind a password I had to find a way to have python log in. I ended up using urllib2:

uri = "http://192.168.1.1/"
username = "********"
password = "********"
page = "http://192.168.1.1/Status_Router.asp"

def get_ip() :
	pass_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
	pass_mgr.add_password(None, uri, username, password)

	auth_handler = urllib2.HTTPBasicAuthHandler(pass_mgr)
	opener = urllib2.build_opener(auth_handler)
	urllib2.install_opener(opener)
	
	handle = urllib2.urlopen(page)
	site_text = handle.read()

	match = re.search("var wan_ip = \"([\d\.]+)\";", site_text)
	#print match.group(1)

	my_ip = match.group(1)

	return (time.asctime(), my_ip)

The uri and regex in that snippet are for my linksys router. For the php code earlier in this post I’m matching on "([\d\.]+)". The return statement gives the time the ip address was taken and the ip string. I keep a log file with that data pair so I can see when the ip address changed, but that’s just python file io, so I need the time at some point and making a tuple here was easiest.

The entire script (including the error handling I trimmed out of the snippet) is posted here.

Scheduling

No matter how fancy your script is, it’s useless if it doesn’t actually get run. Since I’m running it on linux cron seemed like the easiest way to go. Once the script was there to tell me if my ip address changed I didn’t want to run it too often, but I did want it to run regularly. I settled for running it once an hour, and I think I’ll stick with that:

# to deactivate (all cron jobs): crontab -r
# to activate : crontab ./cronip
MAILTO=*username*@localhost
0 * * * * /home/*username*/scripts/ipcheck.py

The *username* should be the login of the user who the script is to be run as. If I remember correctly any errors or other output from the script will be mailed to the specified local user. I think there is a way to mail to an external user, but it required more setup than just typing an email address.

For the path to the script I wrote out the full path, but a relative path from your home directory might also work. The script will be run from the home directory, so any file input and output will be done from there unless an absolute path is specified.

Pyraminx

I took my pyraminx to work on Friday and let Tom borrow it for a while, so of course he beat my times within hours. In my defense that means he had it almost as long as I’d seriously worked at it, though he was at work and hopefully wasn’t concentrating too hard on it. Also, our times weren’t all that great (at 32 and 36 second averages) considering that the record average is 3.71 seconds, but neither of us looked up algorithms.

That meant that I had to work at it a bit more to reclaim my spot at the top of the scoreboard (the only one I’m at the top of now that I’m not the only one with the 4, 5, and 7 cubes).

I was doing a layer by layer solve (and I think Tom was too) where I’d solve one side, then use a couple algorithms I worked out to move the pieces in the second layer around without greatly disturbing the first layer. It’s reasonably quick, especially if you use the inspect time to figure out which side is which color (no stationary center pieces to make it obvious), but I suspect there’s a way to solve it all at once using the inspection to find all the pieces and plan moves.

I put about 45 minutes into the pyraminx last night, then some time today, and got times of single: 10.54 seconds, 3 of 5 average: 16.72. The fastest was last night, the average was an early set of solves from today.

I’m still doing layer by layer, but I came up with another algorithm for rotating all three pieces in the top layer and I generally got better at the puzzle. Speed on the 3-cube largely comes from finger tricks, so you’re moving the pieces quickly once you know what algorithm you’re going to use. Finger tricks are hard on the pyraminx, at least if you’re trying it like the 3-cube, because the pieces don’t always twist the way you want unless you’re holding the end of the point you’re rotating. I’m not great at it now (I still lock the puzzle or have it try to twist the wrong way sometimes), but I’m far faster at just moving the pieces around. I’ve also gotten quicker at my inspections and can spot secondary pieces to move before time starts, which lets me skip some of the search time after getting the four points oriented.

At this point my times are probably “good enough”, meaning I won’t really work on it until someone catches up again. On the other hand, it’s not a bad puzzle for low lighting (like at my desk at home) since it only has four colors, so I might generally play with it but not seriously work on times.

Finally Finished

I finally finished restickering the 7-cube. The picture is shortly before I finished, I just liked how that looked.

I then scrambled it (using the 100 move scramble generated by my timer) and timed a solve.

63:15.52 was a ridiculously long time for a single solve, but at least twenty minutes of that was spent attempting to finish the last pair of 5×5 center grids. I still haven’t managed to come up with algorithms for that for the 7. On the 5 I can get it within a few moves (also mainly guesses), but that only has a 3×3 grid so it’s much more limited on what pieces can be out of place.