element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
STEM Academy
  • Learn
  • Learning Center
  • STEM Academy
  • More
  • Cancel
STEM Academy
Blog Who are the Networks in Your Neighborhood?  Using the Raspberry Pi 3 as a WiFi Sniffer
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STEM Academy to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: mikedavis
  • Date Created: 23 Mar 2016 7:04 PM Date Created
  • Views 3185 views
  • Likes 2 likes
  • Comments 17 comments
  • wifi
  • sniffer
  • raspberry-pi3
  • raspberry-pi
Related
Recommended

Who are the Networks in Your Neighborhood?  Using the Raspberry Pi 3 as a WiFi Sniffer

mikedavis
mikedavis
23 Mar 2016

A copy of this blog post can be found at my own blow (Faradaysclub.com).

As you all probably know, the Raspberry Pi turned 4 on February 29th, and this was celebrated with the release of the Raspberry Pi 3.  What’s new?  Well the big things are:

  • Built in wireless access (no more using 25% of your available USB ports for WiFi).
  • Built in Bluetooth
  • 64 Bit Cortex A-53 processor
  • 1GB of memory
  • Still just $35  (not really new)

I decided I would try to play with one of the new features and make a simple WiFi detector.  I am a big fan of the Best American Non-Required Reading series, which used to have a front section that included some of the most creative WiFi names around.  I was certain that there would be some fun names in my neighborhood, so I thought I would use my Pi3 as an SSID ‘sniffer’.

Since the WiFi is built in to the Pi3, I can strip this project down to just the Pi and a 5V cell phone charger.

WiFi_5

There are quite a few examples of these out there, and they vary in complexity.  The one I worked on uses the built in WiFi to scan all available networks every 10 seconds and record them in a .csv file.  My thought was that I would put this in my backpack and walk around the neighborhood.

The first thing to do is try this command:

sudo iwlist wlan0 scan

You should see a long list of things come back.  The command asked the Pi to scan the wireless signals and report back on everything it finds.  I am most interested in the SSIDs (shown below as ESSID:”coyote”).

WiFi_2

Once that works, I found and modified a python program that will output all of the SSIDs.

I created a folder called ‘wifi’ and a file called ‘test.py’.

mkdir wifi nano test.py

The code below gets copied into test.py.

from subprocess import check_output  
scanoutput = check_output([“iwlist”, “wlan0”, “scan”]) 

for line in scanoutput.split(): 
  if line.startswith(“ESSID”):
  line=line[7:-1]
    print line

The program is really simple.  First it imports the subprocess library.  Then it uses the check_output command and stores the results in a variable called scanoutput.  There are lots of lines in scanoutput, so we split them out individually and look for ones that startswith “ESSID”.  We strip those lines to get rid of the first 7 characters (ESSID:”) and the last character (“), and then print it.

If you see a short list of networks, then its working.

WiFi_3

That is the simple version of the program.  What I would like to do know is have that run as a loop every ten seconds and spit the values out into a .csv file.  This complicates the program a little bit, but the main parts are still there, and we can figure everything else out.

from subprocess import check_output 
import csv
import time 

#get the current time to name a file later on
timestamp = time.strftime("%m-%d-%Y-%I:%M:%S") 

#define a function for doing a wifi scan
def wifiscan(): 
  ssid = []
  scanoutput = check_output(["iwlist", "wlan0", "scan"])
  curtime = time.strftime("%I:%M:%S")
  ssid.append(curtime) 

  for line in scanoutput.split():
    line=str(line)
    if line.startswith("ESSID"):
       line=line[7:-1]
       ssid.append(line)
  with open('/home/pi/wifi/'+timestamp+'.csv','a') as csvfile:
  csvwriter = csv.writer(csvfile,delimiter=',')
  csvwriter.writerow(ssid)
  print ssid 

while True: #repeat the program in an infinite loop 
  ssid=[] #clear the list
  wifiscan() 
  time.sleep(10)

This program works just like the previous one but it has some enhancements.  My python skills are still pretty weak, and I was having a tough time getting all of the networks to show up instead of just the first one, so I started saving everything in a list called ssid.  It starts empty, and then gets populated with the current time (curtime) and SSIDs.  The program then uses csvwriter to open a file that is named with another date stamp that is captured just once when the program starts.  When the SSIDs are captured, it becomes a row in the CSV file, and the first cell in each row is the date stamp.  The infinite loop runs the wifiscan, clears ssid, waits for 10 seconds, and starts over again.

How does it look?

WiFi_4

It works pretty well.  I am really only interested in the names, and the number of names for each scan.  The iwlist command gives a lot more information like signal strength, frequency, and MAC Address.  I live in a pretty densely populated place, so I am curious about how many of these things I can detect as I walk to the coffee shop, or drive to work.

I can visualize it like this by doing a little work in Excel.

WiFi_5

An obvious next step would be to GeoCode this information through the use of a GPS HAT.  As I go through various neighborhoods it would be interesting to see how connectivity changes.

Enjoy!

  • Sign in to reply

Top Comments

  • clem57
    clem57 over 9 years ago +2
    Nice way to tie pieces together for a useful end result. Thanks mikedavis , Clem
  • DAB
    DAB over 9 years ago +1
    I have considered looking into my neighbors networks, but lack the time. If I get further along, I now know that I can use the RPi to look into the options. DAB
  • mikedavis
    mikedavis over 9 years ago in reply to jkutzsch +1
    I've been using the 5V battery, and I've been able to run it for a few hours with this one. I haven't been able to run a full experiment on the time limits, but based on other experiments we do with high…
  • Problemchild
    Problemchild over 9 years ago in reply to Former Member

    Not sure if it's already in the repositories upstream. I'll have to check.

    That said it isn't that difficult to build from Source.

    You can visualise the dataset usinging something like GISKismet which is OK although I've just checked their home page and it seems tohave disappeared and now the major references to it are in theKali Linux doco'.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to Problemchild

    I've had a look and it looks like a great idea but all tutorials how to install it on Pi seems to be few years old.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Problemchild
    Problemchild over 9 years ago

    Why don't you just use Kismet and collect a lot more data more easily ?

    It also works with broken and more marginal packets with a nice live interface and GPS integration.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • mcb1
    mcb1 over 9 years ago in reply to clem57

    I used them to show the alarm stats from the various sites.

    While some were high, it was generally because of Tech activity, and the pivot table made it really obvious.

    Because of that we could focus on the other sites that actually needed attention.

     

     

    I haven't used them lately, but it would make some sense and highlight the problem sites.

     

    Sadly we tend to plod along using the tools we are used to, unless someone shows us a new method, or we start seeking a better solution.

    That's why this forum is great.

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • clem57
    clem57 over 9 years ago in reply to mcb1

    I used pivot tables till I was blue in the face. image (At work)

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube