How I Shortened the ‘iwlist scan’ Output

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

20 Responses to How I Shortened the ‘iwlist scan’ Output

  1. Wes says:

    I still have (and occasionally use) that first script you wrote. πŸ™‚ And that is a pretty nifty little utility you have written there. Maybe we can incorporate that as a plug-in to iwlist… :-p

  2. jintoreedwine says:

    Haha, glad you like it. You know how I have a secret passion for ASCII based GUIs πŸ˜‰

  3. jan says:

    i’ve tried your script, however the Sig Lvl did not display the value. the column on the Sig Lvl just contain “dBm” (without double quotes). what might seems to be the problem?

  4. jintoreedwine says:

    @jan: Hmmm… I’m not entirely sure without seeing your output from iwlist scan. If you want me to have a look then run this command in a terminal and e-mail me the file that it creates (iwlist_scan_out.txt):

    iwlist scan > iwlist_scan_output.txt

    Please make sure you do it in an area where there are access points πŸ™‚

    My e-mail address is: AbinNumber1 (at) gmail (dot) com

  5. jan says:

    I’ve sent you the email. Thank you very much πŸ™‚

  6. xchg says:

    Thanks I use it πŸ™‚

  7. davygravy says:

    Jinto!

    Awesome… I’m using your script on my tiny/embedded/wireless weather site … http://davysweather.dyndns.org

    for the following page, in particular:

    http://davysweather.dyndns.org/systeminfo.html

    to make the wireless scan output a bit nicer…

    thanks,

    Dave

  8. loonix says:

    Is the script still available? I get file not found?

    • jintoreedwine says:

      I fixed the link to the script for you. A few months ago I played around with my domain configuration and apparently broke all the links on my blog. I’ll have to update them all at some point, but for now I fixed this link for you. I haven’t run this script in 6 or 7 months, so I hope it has not fallen prey to bit rot… πŸ™‚

  9. atanas says:

    hey, can you please help me a bit, im trying to make your script show the frequency of the channel too but i don’t know how to initialize the $frequency value. PLEASE help, i really need it πŸ™‚

    sub handleFrequency ()
    {
    $frequency = HOW
    }

    • jintoreedwine says:

      Hi there. I can’t help without getting a bit more information. When you say ‘frequency’ do you mean the radio frequency of the channel or how often a given channel occurs in the output?

      If you let me know that I think we can work something out. In fact, I can probably just update the script for you myself unless you really wanted to give it a go on your own πŸ™‚ . anyway, I’m happy to help either way.

      • atanas says:

        I meant the radio frequency itself in GHz – Frequency:2.412 GHz (Channel 1) – this part of the iwlist scan output and yeah, i would be very grateful if you update it πŸ™‚

      • jintoreedwine says:

        Alright. I’ll take a look at this either tomorrow or Monday depending on how my schedule looks and I’ll let you know when it is complete!

      • atanas says:

        aand can i connect to you somehow (via facebook ,or skype or etc.) i’m working on an important project and i need some help ?

      • jintoreedwine says:

        As promised, I gave the script an update to include the new column which will show the channel and frequency in a new column. Unfortunately, the output is no longer 80 characters wide, so hopefully that isn’t a big issue … :). At the top of the post I have the new version “wiscanv2.pl.” If you’d like to get in contact with me the best way is my e-mail address: AbinNumber1@gmail.com. I have been very busy lately, so I’m not sure how much help I’ll be… ^_^.

      • atanas says:

        okay, thank you very much πŸ™‚

  10. […] the limits of my bash scripting. So I decided to rewrite it in Perl. A bit more googling threw up this blog on using Perl to neaten up iwlist’s output which provided some great ideas on parsing iwlist output with Perl. I rewrote that script as a Perl […]

  11. Alun Jones says:

    Thanks for a useful script.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: