Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 04-08-2007, 03:41 PM   #1
Registered: Sep 2005
Location: Seattle
Distribution: Ubuntu, Debian flavors
Posts: 119

Rep: Reputation: 15
help with Bash script (test)


I am trying to write a small script that tests to see what wireless network I am connected to and automatically turning on or off samba sharing.
I want samba to be on while I'm on my own wireless network, but I want it to automatically shut off if I connect to a public network.

The best way I can figure out to do this is by running:
gksu iwlist ath0 scan | grep "ESSID:"Cafe Wireless""
to see if the public network is in the list (presuming I would be connected to it.)

First question: is there a better way to find out? perhaps:
gksu iwconfig ath0 | grep "ESSID:"Cafe Wireless""
this would tell me what I'm actually connected to. I am using NetworkManager to deal with the wireless. Is there something I can query in NetworkManager to find this info?

Second question: is gksu appropriate? I want the script to run as non-root user but I want it to pop up a password dialog to shutdown samba if required. I have also heard of gksudo.

Third question: I am having trouble with running a test in the script. Here's what I have:
if [ -n $(/usr/bin/gksu iwlist ath0 scan | grep "ESSID:"Cafe Wireless"") ] ; then
/usr/bin/gksu /etc/init.d/samba stop
This seems very erratic. I have been testing it here while I am connected to that network but sometimes it shuts down samba and sometimes not. However, iwlist always finds "ESSID:"Cafe Wireless". Is this the correct use of test? or is it more for checking for actual files and whatnot.

Any help would be great.


Old 04-09-2007, 03:18 AM   #2
Senior Member
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
I am neither a wireless nor a security expert so take the following for what it is worth.

I would think you would be better off using iwconfig since (as I understand it) it shows what access point you are actually connected to rather than what is in the area. And I would think you would want to test against your own ESSID (reversing the sense of the test). Otherwise you will still allow SAMBA sharing on other public networks. Finally, I would hope something in your SAMBA configuration itself keeps out unwanted users, since this script, by itself, seems like rather weak protection.

That said, it looks like your quotation marks in what you posted above are wrong. Were you trying to nest quotation marks? If so, you need to escape the inner quotation marks. But I don't see why you would need to nest at all.

What you are doing above is testing for presence of output from grep. That is fine (except for the points I've already made) but FWIW, the way I would probably do this is:

/usr/bin/gksu iwconfig ath0 | grep "ESSID:" | grep -q "<your network>" || \
   /usr/bin/gksu /etc/init.d/samba stop
or, alternatively

/usr/bin/gksu iwconfig ath0 | grep -q "ESSID:[[:space:]]*<your network>" || \
   /usr/bin/gksu /etc/init.d/samba stop
I don't know enough to comment on your choice of gksu versus alternatives.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to test for a keypress during a bash script? davecs Linux - Software 10 02-27-2007 11:50 AM
Test output from bash script estratos Programming 6 11-16-2006 09:01 PM
bash script 'test' question linmix Programming 5 11-29-2005 11:49 AM
bash script test file operators... bulliver Programming 3 10-17-2003 12:06 PM
benchmark test bash script PTBmilo Programming 3 02-14-2003 04:37 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:52 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration