Thursday, November 29, 2007

Changing the default gateway using Network Manager

I have an oddball network configuration at home. I have a wireless D-Link router that connects to a Huawei router that in turn connects to the internet. So far so good.

The problem is that I want to use the D-Link router DHCP Server, but I want to use the Huawei router as default gateway... and there is no way to specify that in the D-Link router.

The solution I had so far implied setting the connection manually in Ubuntu, and that works fine... Apart from the fact that I have no fast way of setting up a VPN! So I decided to give it one more go with Network Manager, and turns out it is pretty easy to change the default gateway for a particular interface.

To do that, just create a file in /etc/network/if-up.d/ and call it gwconfig for instance.

sudo gvim /etc/network/if-up.d/gwconfig

In that file, check for the interface that you want (e.g. eth1), delete the default gateway that is messing up your system (e.g. 192.168.0.10) and add the the one you really want (e.g. 192.168.1.1).

#!/bin/sh

if [ "$IFACE" = "eth1" ]; then
route delete default gw 192.168.1.10
route add default gw 192.168.1.1
fi


Make sure you make the file executable and you're done.

sudo chmod a+x /etc/network/if-up.d/gwconfig

Set the network to Roaming Mode, and all should work fine. If it doesn't, try to disable and re-enable the network on Network Manager.

Btw, I still wasn't able to make the VPN work with this configuration... more on that later.

Thursday, November 22, 2007

Greasemonkey tips

Greasemonkey is a great Firefox add-on that allows you to add custom JavaScript to any web page. This simple concept allows you to do extraordinary changes to a web page and you can find a lot of pre-made scripts around. But if you want to do your own scripts, there's a few things that you should know before you start...

Registering events
Let's say you want to catch a button click. Here's how to do it:

var button = document.getElementById(buttonId);
button.addEventListener('click', myFunction, false);

Don't do stuff like button.onclick = "myFunction()" or even button.onclick = myFunction. It won't work.

Accessing element properties
Suppose you want to add some information to a link that you created to use during the onclick event. You must do it with:

lnk.setAttribute("attrName", value);

and fetch it with:

lnk.getAttribute("attrName");

Doing lnk.attrName or lnk["attrName"] will not work, and will return null when you access it.

Parameters in Event Handlers
Let's say you registered an event like mentioned above. The event always receives a MouseEvent argument, so you should declare the function like myFunction(evt). This is important because if you by accident register as an event handler a function that takes a boolean paramenter, like myFunction(flag), flag will always be "true" when called from the event.

Stopping the bubble
Assuming you defined your function like mentioned earlier, myFunction(evt), you can stop the event from bubbling by calling

evt.preventDefault();

Testing functions
Because Firebug is such a great tool to debug your scripts, you may want to check for Grasemonkey functions before you use them. Do it with something like:

if (typeof(GM_getValue) != "undefined")

And you should be able to run the script outside Greasemonkey for easier debugging.

More tips
Well, so far these were the problems I faced! :-) I'll update this as I keep discovering Greasemonkey's idiosyncrasies.

Thursday, November 1, 2007

Upgrading to Gutsy Gibbon

Ubuntu has launched a new release last October, Gutsy Gibbon. I was really eager to try it out, so I clicked the magic button to do the upgrade, just like I did last April.

Turns out that this time things weren't so easy... For some reason my Update Manager had been giving me trouble for a couple of weeks. And the Gutsy upgrade was the worst time for things to go really bad.

To start with, the upgrade seemed to stop at some point for no apparent reason. Because the upgrade script is made in Python I decided to try to figure out what was going on. For some inexplicable reason, the confirmation dialog was freezing the installation... so I removed the call to the dialog and things seemed to move forward.

Obviously my happiness didn't last long, and the process stopped at the middle of the installation. No chance but to boot the computer and be prepared for the worst. Turns out the computer booted OK, Ubuntu loaded, but the system was so mixed up with new and old stuff, that X didn't start and apt-get was unable to recover the situation... oh well...

Still, I had text mode so I was able to backup all the things that I should have backed up earlier. I booted Windows and downloaded the Gutsy CD. And then I reinstalled everything.