Ubuntu: Script to Check Internet Connection and Repair

At school there are a set of laptops which I occasionally run experiments on for my research. I usually like to work remotely as I travel a bit and live in other cities. These laptops are all connected to the university network through a wireless AP in my office which passes the DHCP requests to somewhere in the department. This lets each one get an external IP which is extremely useful for SSH-ing into the machines one at a time remotely. However sometimes, for unknown reasons the IPs revert back to 10.x.x.x addresses and aren’t reachable. The problem can be solved by releasing the old address a couple of times:

sudo dhclient -r wlan1
sudo dhclient -r wlan1

and then asking for a new address:

sudo dhclient wlan1

However, this isn’t too helpful if I’m out of the lab. So to automate this I came up with this script which can be turned into a cronjob:

(more…)

Second Times a Charm?

After what seems like eternity, I have completed my second (and a half?) qualifying exam document at Guelph. For people who know me, or who have read a bit of my blog, you may know that I am on my third advisor for my PhD. The advisor I came to the University for suddenly passed away and the second advisor after that resigned right before my last QE was supposed to happen. So after starting with my third (and hopefully final) advisor, I decided to start my QE fresh so that it was something I was proud of and confident in before I presented it. For those of you who don’t know, a QE document is a sort of research proposal (<= 50 pages) of the work you intend to do for you PhD. It contains the literature review, motivation and sometimes initial results, as well as the planned methodology for the research. So anyway, I have completed the document and after several rounds of edits with my advisor it is ready and off to the committee. Here's hoping I'll make it to the exam stage this time and pass quickly so I can move on with the real work! Stay tuned for some posts that are more technical instead of these boring updates on my progress. Now that things are moving along I think I'll actually have something to post 🙂

Hostmonster auto update IP address of subdomain

With my Hostmonster account, I host this website with my www.jasonernst.com domain, but I also have many other machines that are referred to with subdomains. For example, dev.jasonernst.com is my home machine and lab.jasonernst.com is my office machine at school.

However, as you can imagine with the home machine in particular the IP is prone to change occasionally since it is given from the ISP using DHCP. So I searched around for some script to be able to change the zone file in Hostmonster since this controls the IP addresses of all my subdomains.

** Of course the obvious and easy way to do this is with some kind of dyndns account, but I’m picky and like to have everything working under my own domain 😛

I was looking for some type of SSH script way to do it, but it looks like there is no easy way to do this, but I found a ruby script here: http://deathwarrior.wordpress.com/ which allows me to do what I want. Unfortunately the script was written a little while ago and some of it needs to be changed a bit to work.

The first part is the required libraries that must be installed. This has changed because the ruby-mechanize library does not seem to be found in the Ubuntu 12.04 repos. This should do the trick though:

sudo apt-get install libwww-mechanize-ruby ruby-json ruby

The other problem is in the ruby script itself. It seems verify_mode does not exist anymore, so just comment that line out. Another problem is the user-agent which does not seem to be found in the version I was using with Ubuntu. So you can change that to “Windows IE 7” and it should fix it. Here is the code with the changes:


#!/usr/bin/env ruby

require 'mechanize'
require 'json'

USERNAME = 'MY_USERNAME'
PASSWORD = 'MY_PASSWD'
DOMAIN = 'positrones.net'
SUBDOMAIN = 'my_subdomain'
ADDRESS = '0.0.0.0' # If nil then try to get automagicaly

URLS = {
'login' => 'https://login.hostmonster.com/?',
'drecords' => 'https://my.hostmonster.com/cgi/dm/zoneedit/ajax'
}

USER_AGENT = 'Windows IE 7'

m = Mechanize.new do |a|
#a.verify_mode = OpenSSL::SSL::VERIFY_NONE
a.user_agent_alias = USER_AGENT
end

def get_ip
r = Net::HTTP.get('jasonernst.com', '/ip.php')
ip = r.match(/\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4}/)

ip[0].to_s
end

# Do the login stuff
print "Checking user and password... "
page = m.get( URLS['login'] );
form = page.form_with( :name => 'theform' )
form['login'] = USERNAME
form['password'] = PASSWORD
send_button = form.button_with(:value => 'Login')
form.click_button(send_button)
page = page.links[0].click
puts "done!"

# Edit the DNS Zone
page = page.link_with(:text => 'DNS Zone Editor').click
print "Getting old zone records... "
json = JSON.parse( m.post( URLS['drecords'], {'op' => 'getzonerecords', 'domain' => DOMAIN} ).body )
puts "done!"

print "Trying to get subdomain old info... "
json['data'].each do |r|
if r['name'] == SUBDOMAIN
print "done!\nSaving new address[#{r['address']} => #{ADDRESS||get_ip}]... "
json = m.post( URLS['drecords'], {'op' => 'editzonerecord',
'domain' => DOMAIN,
'name' => SUBDOMAIN,
'orig__name' => SUBDOMAIN,
'address' => ADDRESS||get_ip,
'orig__adress' => r['address'],
'ttl' => r['ttl'],
'orig__ttl' => r['ttl'],
'Line' => r['Line'],
'type' => r['type']}
)
json = JSON.parse(json.body)

if( json['result'] == 1 )
puts "done!\nAddress changed succesfully!"
Kernel::exit(0)
else
puts "\nAn error has ocurred trying to save new address :("
Kernel::exit(1)
end
end
end

puts "The subdomain #{SUBDOMAIN} cannot be found!"
Kernel::exit(1)

The last thing that should be done is a server page somewhere for this code to find out it’s own IP address. All the page should return is the IP address itself, not any HTML. You can leave the default in the script which will use my IP script, or you can make your own. It’s as simple as this:

And one more thing, if you are so inclined is to add the script as a chronjob. I have mine set to run every hour, but you can do it more or less often according to your preferences.

crontab -e

I named put my script in a /scripts/ folder I made and named it “hostmonster-auto-ip.sh”

So the crontab entry is:

@hourly /scripts/hostmonster-auto-ip.sh

Blog stats

This blog has been running for quite a few years now and I got thinking about the traffic patterns on it today in an earlier post, so I thought I’d put some of the info together into a post 🙂

Here are the visits in the last three years (74,000 hits)

And in just the last year (30,000 hits)

The average time on the site remained about the same when looking back one or three years and in both cases is about two minutes.

Here is a world map showing the countries where most of the traffic comes from. The top three were US, India and Canada. The darkest colour represents almost 6000 hits.

The most popular browser with visitors of my site is Firefox, followed by Chrome, IE and Safari. I was a bit surprised that Safari did more poorly than IE, but I guess it can be expected when you see the next figure with OS’s.

The most popular OS’s for my site: Windows, Linux, Mac. This makes me a little sad that Linux wasn’t the highest proportion, given the content of my site…but it was at least respectable.

Lastly, here is the posting frequency. There have been 83 posts total according to the wordpress admin page.

OpenGL Console Library for Blackberry Native SDK (Playbook)

Over the break, I decided to try my hand at some Blackberry native SDK programming since I recently got a playbook. I looked around the App World and noticed there weren’t any telnet/ssh tools available for free (that worked the way I wanted) so I thought this would be a good starter project.

However, for the look I wanted, I wanted to maximize the screen space for the console application to show as much text as possible and worked off the hello world example on the SDK webpage. Essentially, the simple “library” (note: library is used very loosely here) initializes the display, shows the keyboard and supports some simple output using putch and puts functions that I implemented. It can also show a blinking cursor and user input at a prompt. In case someone else can use the code I have made it available. Check back soon to see the source for the telnet portion of the app I made as well or look for it on the App World.

Simple Telnet - Example Application
Simple Telnet – Example Application using the conosole library

In order to use the library, you need to make use of the bbutil.c and bbutil.h files that are included in many of the examples provided by RIM. I have provided them here alongside my own code so that it is easier to follow along. I also provide two more files: glconsole.c and glconsole.h. You just need to #include “glconsole.h” in your source in order to use the library. All of the functions available to you are listed in the “glconsole.h” file as well. I will go over the important ones in this article.

First is gl_init(). This function initializes the library, the screen, font etc. This function should be called before using anything else in the library. Similarly, gl_cleanup() is used to free memory and should be called before your app is terminated.

After initialization, you may now use the puts and putch functions to display characters to the screen. gl_putch(char c) takes a single character and displays it onto the screen. It can handle newlines which are represented as ‘\n’ and tabs which are represented as ‘\t’. Backspaces by user input is handled as a ‘\b’ character. This is similar to how people on The osdev.rg website often handle character output in their custom operating systems. I’m not sure how it relates with normal c standards, but I imagine it is similar. Eventually I hope to implement a printf() type of function, but at the moment the putch() and puts() functions serve what I am doing well enough. Note: puts() is the same as putch() but instead takes a (char *) ie) a string. The string should also be null-terminated, or it will just continue until it hits a null character. The characters are automatically scrolled when the bottom of the screen is reached.

The only other really important function is gl_render(). This function is what should be called everytime the screen needs to be refreshed. This is usually in a for loop within your main() function in your app.

It is also possible to resize the screen using the gl_resize() function. By default the screen starts at 80X29 which is the maximum size with no virtualkeyboard showing, at the font size I selected. When the keyboard shows up, it fits 80×14. In some sample code I’ll also show how keyboard input can be handled, how to blink the cursor and how to detect when the keyboard is shown or hidden.

For the rest of this guide, I’ll assume you are using the QNX Momentix IDE available from RIM’s developer website. To start, create a new project by going to File->New Blackberry Tablet OS C/C++ Project. Name it whatever you like, click next. Then choose the C language, and empty Application Project. You can now drag the gl_console.c, glconsole.h, bbutil.c and bbutil.h files into the src folder within the IDE to include them. You will probably want to copy the files and not link them when it asks. Before compiling, there are several libaries which must be linked to the project. You can do this by right clicking your project, going to properties. Then go to C/C++ General->Paths and Symbols->Libraries. Here you can add libraries. Each time you add one, be sure to check “add to all Configurations” so that you don’t need to do this again if you change from a device-debug to device-release build. The following libraries should be included:

  • bps
  • EGL
  • GLESv1_CM
  • freetype
  • png

Now that all this is setup, it is possible to start you main() function and use the library. If you like you can right click on your src folder and click “new file” and create a main.c file or whatever you like to name it. While this isn’t the typical way you would include a library into a project, this way lets you edit the library source to your liking in case you want to further extend it.

The following example shows a skeleton of what is required to get the code to display some text:

#include /* EXIT_FAILURE */
#include /* NULL */
#include /* BPS_SUCCESS */
#include /* screen_event_get_event() */
#include /* NAVIGATOR_EXIT */
#include "glconsole.h"

void handleScreenEvent(bps_event_t *event);

int main(int argc, char * argv[])
{
int exit_application = 0;

if(EXIT_SUCCESS != gl_init())
{
fprintf(stderr, "error init\n");
return 0;
}

gl_puts("Hello gl console world!\n");

for(;;)
{
bps_event_t *event = NULL;
if (bps_get_event(&event, 0) != BPS_SUCCESS)
return EXIT_FAILURE;

if (event)
{
int domain = bps_event_get_domain(event);

if (domain == screen_get_domain())
handleScreenEvent(event);
else if ((domain == navigator_get_domain()) && (NAVIGATOR_EXIT == bps_event_get_code(event)))
exit_application = 1;

if (exit_application)
break;
}
gl_render();
}

gl_cleanup();
return 0;
}

void handleScreenEvent(bps_event_t *event)
{
/* do normal event handling here, see other bb examples */
}

The key parts are calling gl_init() which initializes the library. Then within the main loop, the gl_render() function should be called which will actually render the text. Finally gl_cleanup() should be called before termination to properly free memory.

In the near future I plan to add some support to handle ansi encoded text (specifically for my telnet application) so that it can display colours and move the cursor around the screen using this standard.

Here are the files:
glconsole.h
glconsole.c