How do I scan for Wireless Access Points?
sudo iwlist wlan0 scanning | egrep 'Cell |Encryption|Quality|Last beacon|ESSID'
should help.
It's the combination of sudo
(run as root, do privileged operations), iwlist wlan0 scanning
(produce some output on STDOUT), the pipe symbol "|" (connecting STDOUT of the command(s) to the left to the STDIN of the process on the right), and an egrep
command with a "single quoted" (to prevent the shell from interpreting the "|" characters) Regular Expression to filter STDIN. See man bash
, man sudo
, man iwlist
, man egrep
, and man re_format
for details.
ALWAYS do man whatever (as above) BEFORE you execute a command string from someone else. Self-education is much safer than blind trust.
Using iw
I don't have nm-tool
installed so I use iw
.
This command sorts access points by signal strength, strongest first:
sudo iw dev wlan0 scan | egrep "signal:|SSID:" | sed -e "s/\tsignal: //" -e "s/\tSSID: //" | awk '{ORS = (NR % 2 == 0)? "\n" : " "; print}' | sort
Each command explained:
iw dev wlan0 scan
: Scan for access points reachable via interface wlan0
egrep "signal:|SSID:"
: Get the lines with signal strength and the SSIDs from iw
's output. The output looks like this now:
signal: -77.00 dBm SSID: nameOfAccessPoint1 signal: -71.00 dBm SSID: nameOfAccessPoint2
sed -e "s/\tsignal: //" -e "s/\tSSID: //"
: Reduce egrep
's output to this:
-77.00 dBm nameOfAccessPoint1 -71.00 dBm nameOfAccessPoint2
awk '{ORS = (NR % 2 == 0)? "\n" : " "; print}'
: Bring the signal strength and the SSID on the same line. More specifically, when the line number (NR
) is even, i.e., we are on a line showing an access point, the output record separator (ORS
) should be a line break. Otherwise, we are on the line containing signal strength, so we join the line by making ORS
a simple space.
If we sort
this output, we end up with a list of signal strengths and access points, showing the access point with the strongest signal on top:
-71.00 dBm nameOfAccessPoint2
-77.00 dBm nameOfAccessPoint1
Beware: Some access points can have an extended capability: Extended capabilities: * SSID List
So, grepping "SSID:" instead of "SSID" helps avoiding this extra ouput which would make the command fail otherwise.
nm-tool | grep "Freq.*Strength" | sed -ne "s|\(.*Strength \([0-9]\+\).*\)|\2}\1|p" | sort -n -r
- Use output of
nm-tool
to get list of Wireless Access Points - Filter to get access points only
- Use
sed
to append signal level in front of each line - sort output as numbers in reverse order (largest first)
nm-tool
is part of "network-manager" package that is obviously installed in a typical Ubuntu system.