5

I use my personal laptop at both home and work and in both cases I mount nfs shares. I would like to automate the mounting of these shares as they are done every time I turn the laptop on.

I can't make it a normal start up command to be run, because the wireless connection takes about a half a minute to locate the network and connect.

So I wanted to write up a script that checks every 5 seconds if I'm connected to a network and what the essid of the network so i can mount the appropriate shares.

I looked at iwconfig, and ifconfig and none of them return an essid, only an actual ip address.

ifconfig return...

eth0      Link encap:Ethernet  HWaddr 00:26:2d:a2:01:a8  
      UP BROADCAST MULTICAST  MTU:1500  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
      Interrupt:16 

eth1      Link encap:Ethernet  HWaddr f0:7b:cb:45:36:b2  
      inet addr:192.168.0.48  Bcast:192.168.3.255  Mask:255.255.252.0
      inet6 addr: fe80::f27b:cbff:fe45:36b2/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:30972 errors:0 dropped:0 overruns:0 frame:124824
      TX packets:31414 errors:16 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:25279206 (25.2 MB)  TX bytes:4716009 (4.7 MB)
      Interrupt:17 

lo        Link encap:Local Loopback  
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:336 errors:0 dropped:0 overruns:0 frame:0
      TX packets:336 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:48501 (48.5 KB)  TX bytes:48501 (48.5 KB)

iwconfig returns...

lo        no wireless extensions.

eth0      no wireless extensions.

eth1      IEEE 802.11  Access Point: Not-Associated   
      Link Quality:5  Signal level:210  Noise level:165
      Rx invalid nwid:0  invalid crypt:0  invalid misc:0

vboxnet0  no wireless extensions.

iwlist didn't return anything useful...

iwlist scan
lo        Interface doesn't support scanning.

eth0      Interface doesn't support scanning.

eth1      Interface doesn't support scanning.

vboxnet0  Interface doesn't support scanning.

Does anyone know an easy method to find out the essid of the Wireless network you are connected to via the linux command-line.

Update I added the ifconfig and iwconfig information above, also I'm running Ubuntu 10.10 and it was upgraded from 10.04 which seems to have messed with the wireless somewhat. They no longer use the old Network-Manager app from gnome. I can't connect to my schools wireless network either, so there is definitely something funky with the wireless to begin with.

iwevent return...

iwevent
Waiting for Wireless Events from interfaces...
16:34:33.614296   eth1     Set ESSID:"" [13]
16:34:33.615723   eth1     Custom driver event:
16:34:33.615774   eth1     New Access Point/Cell address:Not-Associated
16:34:33.714835   eth1     Set ESSID:"" [62]
16:34:41.869636   eth1     Set Mode:Managed
16:34:41.869688   eth1     Set ESSID:"" [254]
16:34:42.815795   eth1     Custom driver event:
16:35:07.141450   eth1     Set Mode:Managed
16:35:07.141502   eth1     Set Frequency:24.12 GHz
16:35:07.141601   eth1     Set ESSID:"" [121]
16:35:08.160515   eth1     New Access Point/Cell address:00:1E:00:00:19:0E
16:35:08.252912   eth1     Registered node:00:1E:52:79:19:0E
16:35:08.252970   eth1     Custom driver event:

5 Answers 5

8

Running iwgetid you get the wireless device and the SSID of the connected network. It's part of the wireless-tools - that you appear to have already installed because iwevent is also part of it.

2
  • 2
    specifically iwgetid -r will return the just the SSID of the network
    – Andy
    Jun 3, 2015 at 9:33
  • -r is undocumented in my man-page, but has the same output as --raw.
    – Tom Hale
    May 10, 2017 at 13:24
1

iwlist scan should certainly work to give you the ssid. However from your example I don't see any actual wireless interfaces. I suggest pasting the output of iwconfig and ifconfig into your question to help figure that out.

Also from looking at the iwevent man page, I think perhaps your best bet is to run a loop that calls iwevent instead. When the ssid changes, iwevent will detect and report the new ssid.

Note that polling every 5 seconds sounds pretty intensive, perhaps you should run it less frequently?

5
  • Posted additional info in the original question. Also the polling would stop once the connection was made, or it would time out after a few minutes. I didn't plane on it being a continuous service.
    – Aglystas
    Apr 6, 2011 at 13:34
  • So eth1 is your wireless interface. Weird that it appears to be working yet reports 'Access Point: Not-Associated'. Does iwevent do anything useful? Apr 6, 2011 at 16:43
  • I ran iwevent, then turned my wireless off and back on. Posted the contents above. The essid still showd blank for whatever reason.
    – Aglystas
    Apr 6, 2011 at 20:36
  • What happens if you force the essid with iwconfig eth1 essid Foo and then look at the iwevent output? Apr 7, 2011 at 0:10
  • When running the command to connect to my works wireless this is returned... 08:56:26.981161 eth1 Set ESSID:"" [121] 08:56:27.977062 eth1 New Access Point/Cell address:00:1E:00:00:19:0E 08:56:28.032219 eth1 Registered node:00:1E:52:79:19:0E 08:56:28.032277 eth1 Custom driver event: I'm probably just going to use the ip that I connect to. To much of a hassle troubleshooting why the essid isn't appearing.
    – Aglystas
    Apr 8, 2011 at 12:59
1

I have an old script that handles a check like this using iwconfig that's worked for several years now across several Fedora distributions:

iwconfig wlan0 | grep ESSID | awk -F: '{print $2}'
Then I just make a decision in the script based on what that returns, like start up a home service or a work service. Does your iwconfig command not return info like that once associated with an access point?

2
  • iwconfig doesn't return any access point information. I posted the command output above with the ifconfig.
    – Aglystas
    Apr 6, 2011 at 13:32
  • If iwconfig is not showing any ESSID then it's possible that your wireless card can't provide it to iwconfig. If that's the case then the solution from @christopher-evans here is your best bet - make the decision based on the IP/subnet.
    – zilla
    Apr 6, 2011 at 22:21
1

using iw

Assuming that your interface is wlp3s0, you can get the SSID with iw and grep:

iw wlp3s0 info | grep -Po '(?<=ssid ).*'

The flags -P and -o are explained in this answer.

1
  • 1
    or iw wlp3s0 info | grep -Po 'ssid \K.*' or iw wlp3s0 info | pcregrep -o1 'ssid (.*)' Jun 16, 2020 at 13:09
0

You could also have your script work based on the ip address it gets, and have your home network give out a different subnet.

Or base it on the default route's mac address. You will just need to update it if the router ever changes.

1
  • This seems like the best solution at the moment, thanks.
    – Aglystas
    Apr 6, 2011 at 13:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .