How I Shortened the ‘iwlist scan’ Output

August 4, 2008

Edit [03-04-2012]: I had a request (see comments) to include the channel and frequency. I added them, but unfortunately, I didn’t see a way toΒ  make the script output be 80 characters wide like the original. So, here are the two versions:

Additionally, now that I have looked at the code for the first time in a while, I must apologize for how poorly it is written :). After seeing what a mess I made the first time, I think I’d like to take a stab at cleaning it up and improving it a bit. So, hopefully, I’ll be able to write a more refined version in the coming months.

Edit [09-09-2012]: Updated link to script.

Edit [02-11-2009]: Updated the script to fix a small parsing bug.

In the past few years I have noticed that Linux has come a LONG way in support for wireless cards. I no longer read long stories about how 5 hours and 2 kernel patches later someone finally got their wireless card working.

In fact, I distinctly remember my first experience trying to get my wireless card to connect to a WPA network. It was my freshman year at college, and I spent almost an entire weekend digging through documentation, forum posts, config files, and google search results trying to get my laptop on the campus network through wireless. When all was said and done all I had was a script which was a simple bash script that ran all the commands I needed to connect. It had to be run as root (didn’t know about sudo) and it was a real hack. 3 and 1/2 years later and I simply boot up my laptop and it connects to any network I have predefined in my wpa_supplicant.conf file. I can walk around and it will automatically connect to new network, restores lost connections, etc.

One thing has always been a bit of a bother to me however. That is that the output of ‘iwlist scan‘ is quite lengthy. I can’t ever fit more than 2 wireless networks on a screen (unless I make my terminal super long)! Here is some sample output (MAC Address and actual network name hidden):

Cell 01 - Address: 00:00:00:00:00:00
   ESSID:"sample-network"
   Protocol:IEEE 802.11bg
   Mode:Master
   Frequency:2.462 GHz (Channel 11)
   Encryption key:on
   Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
             11 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
            48 Mb/s; 54 Mb/s
   Quality=33/100  Signal level=-80 dBm
   IE: WPA Version 1
        Group Cipher : CCMP
        Pairwise Ciphers (1) : CCMP
        Authentication Suites (1) : PSK
      Extra: Last beacon: 36ms ago

I am not trying to start a flame war with the writers of iwlist. I think they have a very great tool, but sometimes I just want to know what networks are available in a simple form of output. So, since I had a bit of free time I decided to write a simple little perl script to grab the output of ‘iwlist’ and contort it until I got following output:

+ --- + ---------------------- + ------- + -------- + --- + ------ + --------- +
| #   | Name                   | Quality | Sig Lvl  | Enc | Enc T. | Auth T.   |
+ --- + ---------------------- + ------- + -------- + --- + ------ + --------- +
| 1   | "network 1"            | 39/100  | -77 dBm  | off | WEP    | N/A       |
+ --- + ---------------------- + ------- + -------- + --- + ------ + --------- +
| 2   | "network 2"            | 100/100 + -30 dBm  | on  | WPA v1 | PSK       |
+ --- + ---------------------- + ------- + -------- + --- + ------ + --------- +
.
.
.
etc

So simply running my script will produce that nice, little, easy to read, fits-inside-an-80-character-terminal output. I am not entirely proud of all the perl code I used to write the script, but it works for what it does. I’m sure a true perl expert (which I am anything but) could have written it in under 10 lines, but hey, no one is perfect ;).

I often find myself torn when writing a short little utility script like this. I want to write good code, but I don’t want to spend all day writing a simple little script. I already took shortcuts in the form of using global variables, but I use function calls that only have one line of code in them. In retrospect I think it would have been smarter to just not worry about the functions, but it would take time to go convert it back to be functionless :-P. I will just have to live with the code for now, and be happy that it does what it does.

As another feature of the script it will give you the full output of a single network from the full ‘iwlist scan‘ output if you just specify the number of the network as a command line argument :).

Here is a link to the script, if you are interested: wiscan.pl I had to post it as a .html file because apparently my school does not allow you to have perl files accessible from their apache server. No worries though. Some day I will have my own little web setup which I will manage and then I won’t have to worry about things like this πŸ™‚

Anyway, hopefully this perl script will come in handy for one or two people or, at the very least, give the perl guru’s something to make fun of :). That’s all for now!

Sidenote: Since it’s so short I didn’t bother to put it under the GPL, but come on… it’s a small perl script for an open source utility. Obviously for this script to work you must have iwlist on your system, and perl installed πŸ™‚

Advertisements

Sometimes Java Makes Me Sad

August 1, 2008

On any given day I go back and forth as to whether I truly like Java as a language or not. On one hand the automatic memory management is really quite nice. However it has it’s downsides. Normally I find little quirks of the language that I really wish it had. First off, Java does not have unsigned integers. Now, this is not a true tragedy, and one can work around this by using long integers and bit operations. However that is normally too much work when one is just looking for a safer type.

Another downfall of Java is the pointer hiding. There are times when I really really want to be able to see exactly what is going on. (Yes I know every object is technically a pointer) There is one place in particular this has come to annoy me. I was trying to have a function change the value of an int. This would be quite easy in C or C++, but in Java it is rather tricky. Since you can’t pass the int as reference (Java only has pass by value) and the Integer class is Immutable. One hack I found was to declare an int array with a single element and pass that to the function:

public class TesterClass
{
    public static void main(String args[])
    {
     int[] fooArray = new int[1];

     fooArray[0] = 42;

     System.out.println("My val == " + fooArray[0]);
     myFunc(fooArray);
     System.out.println("My val == " + fooArray[0]);
    }

    public static void myFunc(int[] changeMe)
    {
     changeMe[0] = 12345;
    }

}

Which brings me to my next topic… why is the Integer class immutable!? Say I have the Integer class inside an ArrayList. Well I can’t simply change the value of that Integer. Why? Well because the creators of Java feel the Integer class should be immutable. To me, this makes no sense at all. Yes, I could simply make my own Integer class which allows for changing the underlying value, but why should I? Isn’t this a basic feature that it should have?! It could just have a .setValue() function. Simple! In my mind this makes using the primitive wrappers inside an ArrayList rather useless because you’d have to remove and insert them all the time if you have to change their values. Now, perhaps this is how the creators of Java wanted it. I suppose I will never know.

Overall I do like Java as a language, but sometimes I get discouraged by the potential issues of speed loss, and by some of the things I mentioned above. The control C++ gives the coder is hard to give up, and for now C++ will remain my favorite language πŸ™‚


Java and Memory — Yes, it Can Leak

July 29, 2008

Note: I had previously posted this on my SourceForge Diary on 6-18-08 , but after two posts to the diary I decided the blog was a better place :).

I have been keeping up work on Spheriosity lately as I have devoted my summer to it. Normally I spent around 7 or 8 hours a day writing code for it. Then on the weekend I try to give myself a bit of a break from staring at the computer screen πŸ˜‰

Before I get in to the main point of this entry I will just list a few things I have done with Spheriosity recently:

  • Added the ability to place a point on a line
  • Moved the move point button to the draw section
  • Wrote unit tests for the new code (putting a point on a line)
  • Added a little information section to the bottom of the window which displays area, radius, etc.Β  so the user has some idea what is going on

There are other small changes and if you really want you can investigate the CVS logs. When I release the next version (hopefully the end of the month) I will introduce a changelog file where I will try to sum up all the new features! As a simple FYI the next feature I plan to work on is parallel transport πŸ™‚Β  I can’t wait to get this implemented, but I do not think this will be any trivial task so please be patient with me as I work on this.

Anyway the reason I started this journal entry was because I just found a huge memory leak today in Spheriosity. A memory leak!? In Java!? YES! You CAN have memory leaks in Java. This realization hit me pretty hard today when I found out that I was wasting a lot of memory. Before today I had let the lore of Java convince me it was impossible to have memory leaks, but it’s true… you CAN have them.

How is this possible you ask? Well it has to do with how garbage collection works. Yes, in the traditional sense of C/C++ you can not have a memory leak where you personally forget to deallocate a chuck of memory. This still does not explain what I mean by memory leak though. I am talking about what happens when you hold on to a reference of an object long after it is needed. This happens very easily with private class variables and static variables. Lets talk a bit about garbage collection:

(This is simplified and I am sure the Java garbage collector is far from trivial, but I will simply explain what I know) In a nut shell the garbage collector knows it’s time to get rid of an object when there are no more references to it in code. So if you are storing an object in a private class variable and no longer need it you should set that variable to null. Especially if the class is going to be around for most of the life of the program. This is quite plausible with things such as user interfaces. It is also worth mentioning that you can think of this as a tree like structure. If you stop referencing a class at the top of the tree the entire branch will be collected as long as the only references to the sub objects occur within that branch.

In Spheriosity the problem I had was that after a user selected ‘New’ to clear the sphere I simply removed the highest node in the scene graph from the scene graph and did not clear all of the objects out of it. This caused everything in the frontend and backend to be kept around! All it took was a single line to clear out the scene graph. There were issues where I would get rid of some frontend objects but it was still an observer to something in the backend. As such the garbage collector would never collect it.

Now that I have learned all of this I will be extremely careful to avoid any future memory leaks. I am also going to be more careful when I explain memory management in java to people. I hear people say all the time that it is so amazing and they make it sound completely fail proof, but that is NOT the truth. You still need to be careful with how you manage memory. There is an entirely separate issue here known as heap smashing and in short it is caused by calling ‘new’ too much and forcing the garbage collector to work overtime! This is a conversation for another day, however.

I hope that I have enlightened at least one person about some of the pitfalls of Java. I still like the language and the library support is awesome. However I will never again say the memory management is easier than C++. The reality is that you have to be just as careful! Why do I care so much about memory usage? Well, I don’t like programs that are memory hogs so I try to create ones that don’t hog memory in their own right.

Sometimes I start to think Java itself is a memory hog by default because of the virtual machine and I do get concerned about that. However, without Java3D Spheriosity wouldn’t be anything that amazing so for now I will be sticking with Java πŸ™‚

PS: The cross platform support is nice too πŸ˜€


Intersection of Spherical Circles (Finally!)

July 23, 2008

For the past two weeks I have been pondering on and off how I would calculate the location of the intersection of two spherical circles. I really want Spheriosity to be as complete as possible and having the intersection tool only work on Spherical lines/line segments seems like a real lame idea. So I have finally devised an algorithm which can correctly determine the intersection points. I will give a brief overview of it here. I will leave out details about different parts of the algorithm which I feel are either trivial or would need their own blog post to explain ;).

So we start with two circles… What are the possibilities for their arrangements in terms of intersections:

  1. The most simple is if the circles are not anywhere near each other and don’t intersect.
  2. One circle could be “inside” the other. In this case the circles obviously do not intersect.

    One circle inside the other

    One circle inside the other

  3. The two circles could intersect only in one spot. This could happen in a number of ways:
    • Case 1:

      First Intersection Case

      First Intersection Case

    • Case 2:

      Intersection Case 2

      Intersection Case 2

  4. Then there is the case where the circles intersect twice. This is perhaps the most common case in terms of what users will see, but since the other cases are easy to construct we must also deal with them.

So that loosely describes my problem and the different scenarios that need to be considered. So… on to the solution! Much of this boils down to finding the intersection of two planes. Each circle on the sphere defines it’s own plane which basically slices the sphere, and the intersection of that plane and the sphere could also be used to form the circle. So the intersection of the planes formed by both circles forms a line which, if it goes through the sphere, can be used to find our points of intersection.

Defining that line of intersection turned out to be the largest problem. It was easy enough to get a vector going in the correct direction but I still needed a point to form the line and perform any sort of calculation on it. Here are the steps I take to find the intersection and make sure one actually exists:

  1. Consider two circles AB and CD (defined by center point and radius point).
  2. Find the normal vector to the planes formed by each circle. We will call these vectors a and c
  3. Take a x c if the magnitude squared of the resulting vector is 0 you can stop now because the planes are parallel, and the circles could not possibly intersect (or are coincident).
  4. Form an axis of rotation, r, between points A and C.
  5. For both circles form lines by rotating their respective center points forward and back by the angle formed between their center points, the center of the sphere, and their radius points. (call these lines l and m.
  6. Find the intersection of lines l and m and call this point I
  7. Find the intersection of the line formed by using point I and the cross product of a and c with the sphere.
  8. Consider the following cases:
    • Case 1: Line does not intersect the sphere: no intersection points
    • Case 2: Line intersects once with the sphere: one intersection point
    • Case 3: Line intersects twice with the sphere: two intersection points

There you have it! I know I left some details out, but writing out every single step would make this a really long post ;).


Python 2.5 and encryption — PyCrypto under Windows

July 20, 2008

[Edit: 8-13-08] – Upon request I have provided a link to an installer for PyCrypto-2.0.1 that is compiled for Python 2.5. You can download it here . I didn’t post it originally because I was not sure how long I could provide a link for. This one should be good for about a year … πŸ™‚ .

Note: The steps listed here will only work on Python 2.5 and above as that is when they added support for allowing MinGW to compile code

I mentioned in a previous post that I was looking for a way to get public/private key encryption in Python and I was having a bugger of a time until I found ezPyCrypto. I don’t know if I have mentioned this before, but I have been an Linux user for about 4 years now. Specifically Gentoo Linux ,and I would never run another OS now that I have been a Linux user. At any rate that’s not the point of this post. Since the script I was developing had to run on Windows I needed to install PyCrypto (it’s a dependency for ezPyCrypto). Well this would be trivial except that PyCrypto has C code that needs to be compiled. There are binary builds on the PyCrypto site for both Python 2.3 and 2.4, but not 2.5 which I am running. I wasn’t going to let this stop me however. The rest of this post will explain what you need to do in order to get PyCrypto working under Windows with Python 2.5. After completing this you will also be able to build your own installer that you could package with your scripts to let people who don’t want to install a C compiler run your code πŸ™‚

The first thing you are going to want to do is install MinGW . MinGW will give you a nice C and C++ compiler for Windows. Just follow the instructions on their site and you will be good to go. After you have MinGW installed you will probably want to add entries to the Windows PATH variable so that when you are in a terminal you will be able to directly access MinGW. [As a note I still run Windows XP so these instructions will be done with that in mind] Doing this is quite simple:

  1. Right Click My Computer –> Properties –> Advanced –> Environment Variables.
  2. You will see User and System variables. If you want all users to use MinGW edit the Path entry in there. Otherwise add a Path variable under User variables.
  3. Go to the Path variable from step 2. Add an entry to the Path that points to MinGW’s bin directory. For me this was “C:\MinGW\bin” but it all depends on where you installed MinGW. Note: Entires in Windows Environment variables are separated by ‘;’
  4. Click ok a bunch of times

While you are changing environment variables you will want to add the main directory for Python to your path. For me this was: “C:\Python25”. This made my final User Path entry: “C:\MinGW\bin;C:\Python25”

Now that you are done changing environment variables you will want to reboot your computer so that Windows gets your changes. Really… you will regret it if you don’t reboot!

Now download PyCrypto and unpackage it. This turned out to be problem enough because not many people keep programs around that open .tar.gz files. Since am a Linux user I just unpacked them in Linux and transferred them to Windows (I run Windows XP under VirtualBox ). However, if that is not an option you can download and install IZarc Archiver. IZarc has been my favorite [Windows] extraction tool for a long time and it supports just about every archive format imaginable.I would recommend extracting the contents of PyCrypto to your desktop. Now is when the first starts!

First, open up a terminal. We will only be using a few commands and you don’t have to be a terminal wiz kid to do this. I normally open a terminal by going: Start –> Run –> “cmd” –> Press Enter

Now that you have a terminal up you want get to the PyCrypto directory. To move between directories in the terminal we use the “cd” command.

cd PATH-GOES-HERE

So, for example here is the command I used to get to where I had PyCrypto:

cd C:\Documents and Settings\Jinto\Desktop\pycrypto-2.0.1

Of course that command will only work for you if your user name is Jinto and you extracted it to the desktop exactly like I did πŸ˜‰ So modify it to fit your needs. Once you are in the pycrypto directory do a quick:

dir /w

To make sure you see files. Specially make sure setup.py is there. We need to run that setup script and tell it to build the libraries. To do this issue the following command:

python setup.py build -c mingw32

If all goes well it should build without any troubles and all that is left is to install it by issuing the following command:

python setup.py install

You should be all set to use PyCrypto now, or to install ezPyCrypto which uses PyCrypto. As a bonus feature if you are looking to make an executable installer for PyCrypto you can run the following command:

python setup.py bdist_wininst

This will leave you with a .exe file located in the “dist” folder.

I hope this helps everyone get PyCrypto running under Windows. For those of you Linux users who think I am leaving you out check your distro specific respository system they probably already have a package for PyCrypto!


Reading all the Documentation…

July 19, 2008

So apparently when people the time to write something down they wrote it down for a reason. I happened to have missed this simple fact recently when I was working on some Python scripts. I was trying to separate the scripts into different folders based on what they did. Simple enough, but then I ran into the issue of certain scripts needing to go around folder boundries. Of course, Python supports this feature using it’s import system.

This is where my trouble was. I was reading the article about how import works from the standard Python docs. So I read as much as I thought I needed and skipped over, what I thought at the time, to be unimportant. As it turns out the ability to do relative imports using ‘..’ is a feature of only Python 2.5 *sigh*. Alright, so I could have just installed Python 2.5, but I didn’t want to limit this code to running on Python 2.5 for no good reason. So I went back and changed the code to using absolute imports which took around 30 minutes because there were a decent number of files to play with.

So how did I miss this in the documentation? Well I managed to skim over the part which mentions this is only for Python 2.5 (I blame myself for this, btw). I take this as a lesson to pay closer attention when reading documentation. It may have taken me an extra 5 or 10 minutes to read all the documentation about imports and Python… but I will would have saved half an hour of my time if I had taken the extra effort πŸ™‚


Java3D, the PickRay, and the PickCylinderRay

July 14, 2008

Today I was struggling to figure out why I could not highlight a line segment in Spheriosity. After much fiddling around I discovered that the problem only occurred when I was using a PickRay to select objects instead of my normal PickCanvas. For those that don’t know a PickCanvas is made to pick objects based on the x and y coordinates of the cursor and a PickRay has to be defined with a starting point and vector indicating its direction. I was a bit disappointed because I thought I had already solved this problem. The general problem here is: ‘How do I take the cursor location and select something in a Java3D BranchGroup?’

My final solution to this problem was to use a PickCylinderRay, which defines an infinite cylinder going in a defined direction from a defined starting point and with a defined radius. Switching to the PickCylinderRay actually solved more than one of my problems. Previously when I was using the PickCanvas I found it very difficult to select objects at times. Especially the line segments (LineStripArrays) . I know that you can set different tolerances to the PickCanvas but I was having a heck of a time getting that happy. The only problem I ran in to with the PickCylinderRay was that if the radius was too large it would pick objects that weren’t even close to the cursor. However, this makes sense πŸ™‚ Here is what the code looks like to define the vector from the camera to the cursor:

public void getCameraToMouseVec(Canvas3D myCanvas, Point clickPos,
                                Point3d cameraPoint, Vector3d dir)
{
     Point3d mousePos = new Point3d();

     //Getting current mouse and camera locations
     myCanvas.getPixelLocationInImagePlate(clickPos.x, clickPos.y, mousePos);
     myCanvas.getCenterEyeInImagePlate(cameraPoint);

     //This block of code converts our image plate coordinates out to virtual world coordinates because
     //that's ultimately what we care about.
     Transform3D motion = new Transform3D();
     myCanvas.getImagePlateToVworld(motion);

     //We do this convertion for both the camera and for the mouse position.
     motion.transform(mousePos);
     motion.transform(cameraPoint);

     //Get the three components of the vector going from the camera location to the eye
     dir.x = (mousePos.x - cameraPoint.x);
     dir.y = (mousePos.y - cameraPoint.y);
     dir.z = (mousePos.z - cameraPoint.z);
}

So after this function completes the variables cameraPoint and dir will be set to the correct values to call .setShapeCylinderRay() of the PickTool class . Here is a sample of the code which takes the output of the previously displayed function and uses it to make a selection:

public void mousePressed(MouseEvent arg0) //Called be Java through MouseListener interface
{
     //In Spheriosity I actually declare these once for the class and reuse them
     Point3d tmpPoint = new Point3d();
     Vector3d tmpVec  = new Vector3d();

     //I assume that 'myMainBranchGroup' is a member of the class that this function is in
     //and is the BranchGrounp we want to be picking from.
     PickTool myPicker = new PickTool(myMainBranchGroup);

     //I also assume that 'myCanvas' is a member of this class
     getCameraToMouseVec(this.myCanvas, arg0.getPoint(), tmpPoint, tmpVec);

     //Use the data gathered from getCameraToMouseVec to make the PickCylinderRay
     myPicker.setShapeCylinderRay(tmpPoint, tmpVec, .1f);

     PickResult result = myPicker.pickClosest();

     ... code to handle what was selected ...
}

There you have it! That is how I solved my little problem with PickRays not working. I’m glad I stumbled upon the original problem because I truly think this solution works much better given the rest of the code base in Spheriosity.

There are a few things to note here. One is that the .1f in the .setShapeCylinderRay() function should be changed depending on your needs (it determines the radius of the cylinder). Lower values mean the cursor has to be closer to the object higher values mean the cursor can be further away. Also the output of the getCameraToMouseVec() function could also be used to define a PickRay if that is truly what you want.

Hopefully this was able to help one or two people trying to select things in Java3D. If not… well at least it serves as a bit of rationale for some of what I have done with Spheriosity πŸ˜€