Thursday, November 14, 2013

Installing OpenWRT on D-Link DIR-505L H/W Ver. A1

The D-Link DIR-505L is a great and cheap WiFi router that can run OpenWRT. I costs about $40 from Amazon (http://www.amazon.com/D-Link-Systems-SharePort-Companion-DIR-505L/dp/B009LENJ90) and it has a 400 MHz MIPS processor, 64 MB RAM, and 8 MB flash drive. It is probably the cheapest OpenWRT-capable router that you will find with 64 MB of RAM (I came to this conclusion after looking on the web for a few hours). We currently purchased two of these routers from running our code on OpenWRT in our lab (https://github.com/ipop-project). The first stop was the DIR-505 page on OpenWRT (http://wiki.openwrt.org/toh/d-link/dir-505). This page gives the details of the router, but it did not give a clear link to the firmware file for the router. Eventually I figured it out and here it is:

http://downloads.openwrt.org/snapshots/trunk/ar71xx/openwrt-ar71xx-generic-dir-505-a1-squashfs-factory.bin

The next step is to flash firmware with OpenWRT image through the router's web interface. Once you have successfully connect to the wireless router, point your web brower to http://192.168.0.1. The setup wizard will show up, just cancel that. Click on Maintenance (on the horizontal navigation bar), then Click on Firmware (on the vertical navigation bar on the left). You will see the input form where you can upload the firmware. The webpage will tell you not to update your firmware over WiFi. You can ignore that because i have done it multiple times and nothing bad happened. If you upload the file from above, you will get the following message: "The chosen file is not a firmware file". This took me a while to figure out and the main reason for this blog post.

First, you need to open the bin file in vim and set vim in hex mode

vim openwrt-ar71xx-generic-dir-505-a1-squashfs-factory.bin

then once you have vim open, run the hex mode command

:%!xxd

You will notice in the first few lines that it says

0000030: 5235 3035 4131 2d33 0000 0000 0000 4445  R505A1-3......DE

That value is what is causing the firmware update to fail, modify the fourth line of hex code directly to

0000030: 5235 3035 4c41 312d 3300 0000 0000 4445  R505LA1-3.....DE

You only have to update the hex number (not the letters on the right). Once done, revert back to regular mode

:%!xxd -r

then just save and exit

:wq

Now you should be able to update your firmware through the web interface with no trouble. If you have any issues, just leave a comment. Good luck, Internetland.

Thursday, October 31, 2013

Unbricking my Linksys wrtu54g-tm from bad OpenWRT update (only $5 to fix)

About a month ago, I decided to update my WRTUG54-TM router to the latest version of OpenWRT (attitude adjustment) and for the first time I decided to check the box on the Lua Web interface to save my current settings ... big mistake. For some reason, that messed up the update process (I have done this successfully at least 4 or 5 times in the past, so I know what I'm doing). After bricking my router, the first thing that I had to do is figure out how to unbrick it without any special hardware. The first solution is to try setting up a TFTP server on your machine and connect your laptop (or PC) to the router at the blue Internet port. Then set your laptop (or PC) ip address to 192.168.0.250, put the OpenWRT image as "vmlinuz-8668" and run reboot your router. Your router is supposed to automatically use a TFTP client to download the new image and update your router. This is explained here (http://wiki.openwrt.org/doc/howto/generic.flashing.tftp). Sadly, this automatic TFTP update with no user input is not possible on this router because it did not work. Also, this was confirmed by this blog post (http://wiki.scottn.us/wrtu54g-tm:uboot). The only way to trigger this update is to connect to the router via the serial port and manually trigger this using the "run update_kernel" command. Therefore, I had to figure out how to do this. It turns out to be much easier to fix, once you know what to do ... naturally. Basically, you need to buy a USB to TTL converter, here is the one that I found on Ebay (only costs about $5). You can find one by searching for "USB To RS232 TTL PL2303HX Cable Adapter" 

http://www.ebay.com/itm/321168200507?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649.

Another option mentioned is to use a Nokia C42 clone cable, but that requires soldering, plus it costs about the same price. So don't buy that one, it's not worth the struggle.

Now that you have your USB to RS232 TTL adapter, you need to open up your router. There are four screws located under each of the black rubber pegs of the router. They are glued on, just use your nails to pull them off and then you will see the screw drivers. Unscrew and open the router. Once you have the router open, carefully inspect it and you will find 4 pins with the word JP2 next to it. Once you find that, you need to figure out which pins to connect your adapter to. The pin with the triangle next to it is the VDC pin and that is going to match your red wire. The fourth pin is the ground pin, which connects to the black wire. The middle two pins for TXD and RXD, you can play around with that until you get the right configuration. It won't mess up your router. It took me many tries to get it right. But for me, I connected the white wire to the second pin (under the VDC pin) and the green was after that.

Now that you have your pins connected, you can connect to a USB port, also, you do not have to power your router to the wall plug, the USB adapter will power your router. When you plug into the USB, you will see many flagging green lights, then just one flashing green light, that means that your router is powered.

Now it's time to connect to your serial port from your computer. I use Linux and therefore I can accomplish this with the screen command line tool. But Windows users should be able to connect with Putty. Here is the screen command that I used to connect to the router via serial

sudo screen /dev/ttyUSB0 115200,cs8

The "115200" number is the baud rate which is very important to get right. I spent at least 30 minutes trying to figure this out. If you don't see anything when you do this, try unplugging your usb adapter which will cause screen to exit. Then plug it back in and run the screen command 1 or 2 seconds after you plug in the USB adapter. You should see a bunch of output and a command prompt. Now that you have the command prompt, you can do the TFTP update by running a TFTP server on your laptop or PC, put the updated image in the tftp root directory as "vmlinuz-8668" and use ifconfig to set the IP address to 192.168.0.250. Connect the ethernet cable from your laptop or PC to the blue Internet port in your router. Then type "run update_kernel" and wait. The command prompt will let you know when it's done. Then you can just restart by unplugging your USB adapter.

Well this is probably my longest post. But as always, I hope this helps someone else save some time. I would not have figured it out it is was not for this blog that I found about a year ago (http://wiki.scottn.us/wrtu54g-tm).

Friday, October 25, 2013

Using dig to do MDNS lookup

I love MDNS (or Bonjour as Apple calls it). Multicast DNS is a local area domain naming service that makes it easy to discover endpoints on your local area network. For example, I can always connect to my netbook with ssh pnetbook.local. If you have avahi-daemon installed on Linux, it will do a DNS query to 224.0.0.251:5353 and the avahi-daemon on the network that it responsible for that domain name will respond. In the past, I could only use this if I have avahi-daemon (or Apple Bonjour for Windows) installed on both machine. But it just truck me that I can use "dig" to emulate a DNS query to that same multicast IP and port without have avahi-daemon installed on both machines. As long as there are MDNS responders on the network, they will see the request and reply accordingly. Anyways, here is the command (so beautiful)

dig @224.0.0.251 -p 5353 pnetbook.local

Well I hope this helps some of you.

Friday, June 28, 2013

Running VMs on the Cloud (Amazon, Google, Rackspace, Microsoft, Digital Ocean, HP)

I have been using OpenStack a lot the past few months through the FutureGrid project and I've gotten quite fond of the idea of running VMs on the cloud. However, since I'm a networking guy, I was intrigued by the availability of public IP for these VMs. On FutureGrid, it is quite hard to get enough public IP addresses so many times you end up with VMs no public IP access which can be cumbersome to access. Anyways, I wanted to see how different cloud providers handled this problem. This is a quick list of things that I noticed:

Amazon EC2
- Gives an internal DNS and external DNS which points to internal and external IP addresses
- Local OS does not know the external public IP address
- Access only available through public key over ssh (for Linux)
- Firewall options available
- Allows you to define your own private network http://aws.amazon.com/vpc/
- $0.020 for cheapest instance 0.615GB RAM http://aws.amazon.com/ec2/pricing/

Google Compute Engine
- Internal DNS mapping is available, but no external DNS mapping
- You have to use gcutil to connect which discovers external IP and creates public key
- You also have to use gcutil to connect over ssh, which is pretty annoying
- Local OS does not know external public IP address
- Firewall options available
- Allows you to define your own private network https://developers.google.com/compute/docs/networking
- $0.013 for cheapest instance 0.6GB RAM https://cloud.google.com/pricing/compute-engine

Rackspace Cloud Servers
- It does not provide a DNS mapping by default
- Local OS gets two NICs one with private IP and another with public IP address
- Support username/password over ssh, you have to configure public-key on your own
- Firewall options available (not by default)
- Allows you to define your own private cloud (not by default) http://www.rackspace.com/knowledge_center/article/getting-started-with-cloud-networks
- $0.022 for cheapest instance 512RAM http://www.rackspace.com/cloud/pricing/

Windows Azure
- Provides DNS to external public IP
- Local OS does not know public IP address
- Supports both password or public key for ssh at VM creation time
- Firewall options available
- Allows you to define your own private network http://www.windowsazure.com/en-us/services/virtual-network/
- $0.020 for cheapest instance 768MB RAM http://www.windowsazure.com/en-us/pricing/

Digital Ocean
- No DNS mapping by default
- Local OS has one NIC with a public IP address
- Supports both username/password and public key
- No firewall options
- No support for defining your own private network
- $0.006 for cheapest instance 512 RAM https://www.digitalocean.com/pricing

HP Public Cloud
- No DNS mapping by default
- Local OS has one NIC with a private IP address
- Firewall options available
- Supports on public key for ssh access
- No support for defining your own private network
- $0.035 for cheapest instead 1GB RAM http://www.hpcloud.com/pricing

Clearly Digital Ocean won hands down because they are so cheap ... wow only $5 to run host a VM ... wow

Overall, I'm excited that there is so much competition in the IaaS space and I'm leaving out GoGrid, VMWare, HP and many others. Plus this is great for my research because with all of these VMs running at different providers, people will need to connect them in a secure fashion. Ok gotta go.

Friday, April 12, 2013

Newsbeuter - A Mutt-like RSS Reader (best kept secret replacement for Google Reader)

Ever since Google reader shut down, I have been frantically looking for a good replacement. I have finally settled on Newsbeuter because it's an active project, light-weight, and be added to cron for periodic updates from commandline (and most importantly of all, the interface is very similar to Mutt and I'm a big fan of Mutt).

Therefore I fully endorse newsbeuter http://www.newsbeuter.org/ as the ultimate rss reader to use, most importantly because you can add it to crontab and update newsfeeds every hour (which is actually more important than you might think). I tried


  • Feedly (too heavy and depends on browser), 
  • Liferea (nice but requires Gtk stack and no commandline update), 
  • Old Reader (web-based but too slow), 
  • Netvibes (silly interface and requires money for pro-account), 
  • Newsbleur (horrible interface) and Yahoo (also horrible interface)
  • Canto (python-based, commandline, too many dependencies, bad interface)
Hope this helps someone else in their search.

Friday, March 15, 2013

How to query select rows with logical indexing in Matlab/Octave

Once again, I am making this quick post because it took me a while to figure this out. Hopefully, this will save someone else some time (even if it's 30 seconds).

Let say you have the follow matrix

a = [1,2; 1,4; 2, 5; 2, 7]

or

a =

  1      2
  1      4
  2      5
  2      7

a > 4 will return a matrix with a 0 or 1 if the cell matches the condition. So in this case, it will return

  0      0
  0      0
  0      1
  0      1

The zeros represent the positions which are less than 4 and the ones are the positions that are greater than 4.

a(:,1) will return the first column of the matrix only

Basically, the line below will return only 2 and 4, since there are the only rows where the first position == 1

a1 = a(:,2)(a(:,1) ==1)

I know this is a sucky explanation but it's late and I'm tired.

Tuesday, March 5, 2013

I have gists (Gnutella crawler, parallel ssh, matplotlib)

Finally created some gists of code snippets that I've had for years

Python Gnutella Crawler
https://gist.github.com/ptony82/5092551

Parallel SSH (written in Bash)
https://gist.github.com/ptony82/5092639

Matplotlib Graph configurations
https://gist.github.com/ptony82/5092663