Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Wireless SSID and key configuration script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
martinoc
n00b
n00b


Joined: 12 Jan 2005
Posts: 12
Location: Ireland

PostPosted: Tue Jan 25, 2005 6:42 am    Post subject: Wireless SSID and key configuration script Reply with quote

Code:
#!/sbin/runscript

#NB: Config is in /etc/conf.d/ssid

depend() {
   before net
}

checkconfig() {
   if [ -z $IFACES ]; then
           eerror "You need to setup IFACE /etc/conf.d/ssid first"
           return 1
   fi
                  
}

start() {
   checkconfig || return 1
   ebegin "Setting ssids"
   for iface in $IFACES; do
      available=`iwlist scanning 2>&1 | grep ESSID | sed "s/ *//"| sed "s/ESSID:\"//" | sed "s/\"//"`
      eval SSIDS=( \$${iface}_SSIDS )
      if [ -z $SSIDS ]; then
      ssid="any"
      key="off"
      eend 1 "${iface}_SSIDS not set in /etc/conf.d/ssid"
      else
         for ssid in $SSIDS; do
            for avail in $available; do
               if [ $avail = $ssid ]; then
                  eval key=( \$${iface}_${ssid} )
                  if [ -z $key ]; then
                     eend 1 "${iface}_$ssid not set in /etc/conf.d/ssid"
                     key="off"
                     ssid="any"
                  fi
                  break 2
               fi
            done
            eend 0  "No configured ssids available for $iface"
            ssid="any"
            key="off"
         done
      fi
      ebegin "Setting ssid to $ssid on $iface"
      iwconfig $iface essid "$ssid"
      iwconfig $iface key $key
   done
   eend 0
}

stop() {
   checkconfig || return 1
   for iface in $IFACES; do
      ebegin "Unsetting ssids"
      ebegin "Unsetting ssid on $iface"
      iwconfig $iface essid any
      iwconfig $iface key off
   done
   eend 0
}


This requires the following variables configures in /etc/init.d/ssid

IFACES -> a list of interfaces to individually set up
<iface>_SSIDS -> a list of SSIDS in preferential order to set up for <iface>
<iface>_<ssid> -> the parameter to
Code:
iwconfig $iface key
for the combination of <iface> and <ssid>


The script will configure the first available ssid in the preferential list for each interface as obtained from iwlist scanning.
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6835
Location: Blighty

PostPosted: Tue Jan 25, 2005 9:55 am    Post subject: Reply with quote

Nice script - just a few pointers though ....

[*] Why can't you detect if an interface is wireless and configure it automatically?
[*] What happens if the ESSID cotains any of the following characters "\/'-
[*] What about AP's that don't broadcast the ESSID?

Code:
available=`iwlist scanning 2>&1 | grep ESSID | sed "s/ *//"| sed "s/ESSID:\"//" | sed "s/\"//"`


Yoiks! That's 3 chained sed statements with a grep! Here's a better one (which also fixes a bug for you)

Code:
available=$( iwlist ${iface} scanning 2>&1 | sed -ne 's/^ *ESSID:"\(.*\)"$/\1/p' )


Aside from all that, we already have wireless support in baselayout-1.11.8 :)
Back to top
View user's profile Send private message
martinoc
n00b
n00b


Joined: 12 Jan 2005
Posts: 12
Location: Ireland

PostPosted: Tue Jan 25, 2005 10:25 am    Post subject: Reply with quote

Not bad for my first attempt :)

here is an update
Code:
#!/sbin/runscript

#NB: Config is in /etc/conf.d/ssid

depend() {
   before net
}

checkconfig() {
   if [ -z $IFACES ]; then
           eerror "You need to setup IFACE /etc/conf.d/ssid first"
           return 1
   fi
                  
}

start() {
   checkconfig || return 1
   ebegin "Setting ssids"
   for iface in $IFACES; do
      available=$( iwlist ${iface} scanning 2>&1 | sed -ne 's/^ *ESSID:"\(.*\)"$/\1/p' )
      eval SSIDS=( \$${iface}_SSIDS )
      if [ -z $SSIDS ]; then
      ssid="any"
      key="off"
      eerror "${iface}_SSIDS not set in /etc/conf.d/ssid"
      else
         for ssid in $SSIDS; do
            for avail in $available; do
               if [ $avail = $ssid ]; then
                  eval key=( \$${iface}_${ssid} )
                  if [ -z $key ]; then
                     eerror "${iface}_$ssid not set in /etc/conf.d/ssid"
                     key="off"
                     ssid="any"
                  fi
                  break 2
               fi
            done
            ewarn "No configured ssids available for $iface"
            ssid="any"
            key="off"
         done
      fi
      einfo "Setting ssid to $ssid on $iface"
      iwconfig $iface essid "$ssid"
      iwconfig $iface key $key
   done
   eend 0
}

stop() {
   checkconfig || return 1
   for iface in $IFACES; do
      ebegin "Unsetting ssids"
      einfo "Unsetting ssid on $iface"
      iwconfig $iface essid any
      iwconfig $iface key off
   done
   eend 0
}


can you explain the portion of the sed command after ESSID:
the rest i can understand.


answers to your questions
1) automatic config - i dont because the user has more control over the interfaces configured and not configured, and control over which ssids get assigned

2) - didn't see that one - hadn't had much sleep lately i guess ---> I guess this was the bug your replacement available= fixed

3) - I suppose I could select the most preferable (first) on the list instead of going to any and off
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6835
Location: Blighty

PostPosted: Tue Jan 25, 2005 11:19 am    Post subject: Reply with quote

martinoc wrote:

can you explain the portion of the sed command after ESSID:
the rest i can understand.


Sure!
\( \) means "define a group"
. means "match anything"
* means "match any amount of the precedding match"

So what we are saying is "from the start of the line match any amount of spaces followed by ESSID:". Then define group 1 which is anything upto the last " on the line.

The \1/p means print group 1

http://dev.gentoo.org/~ciaranm/docs/IGNORE_the-doc/#content-manipulation-sed
may be of some help

Quote:

answers to your questions
1) automatic config - i dont because the user has more control over the interfaces configured and not configured, and control over which ssids get assigned


I have a laptop and 2 pccards - one wired and the other wireless. I can only have one inserted at any time, and both report themselves as eth0. How do you handle this?

Quote:

3) - I suppose I could select the most preferable (first) on the list instead of going to any and off


What if you had 2 AP's that didn't broadcast?
Back to top
View user's profile Send private message
martinoc
n00b
n00b


Joined: 12 Jan 2005
Posts: 12
Location: Ireland

PostPosted: Tue Jan 25, 2005 1:43 pm    Post subject: Reply with quote

Code:
#!/sbin/runscript

#NB: Config is in /etc/conf.d/ssid

depend() {
   before net
}

checkconfig() {
   if [ -z $IFACES ]; then
           eerror "You need to setup IFACE /etc/conf.d/ssid first"
           return 1
   fi
                  
}

start() {
   checkconfig || return 1
   ebegin "Setting ssids"
   for iface in $IFACES; do
      available=$( iwlist ${iface} scanning 2>&1 | sed -ne 's/^ *ESSID:"\(.*\)"$/\1/p' )
      eval SSIDS=( \$${iface}_SSIDS )
      if [ -z $SSIDS ]; then
      ssid="any"
      key="off"
      eerror "${iface}_SSIDS not set in /etc/conf.d/ssid"
      else
         for ssid in $SSIDS; do
            for avail in $available; do
               if [ $avail = $ssid ]; then
                  eval key=( \$${iface}_${ssid} )
                  if [ -z $key ]; then
                     eerror "${iface}_$ssid not set in /etc/conf.d/ssid"
                     key="off"
                     ssid="any"
                  fi
                  break 2
               fi
            done
            ewarn "No configured ssids broadcasting on $iface"
            eval IFACE_default=( \$${iface}_default )
            if [ -z $IFACE_default ]; then
               ssid="any"                                                    key="off"
               ewarn "No default ssid set for ${iface}"
            else
               ssid=$IFACE_default
               eval key=( \$${iface}_${ssid} )
               einfo "Using default ssid for ${iface}"
            fi
         done
      fi
      einfo "Setting ssid to $ssid on $iface"
      iwconfig $iface essid "$ssid"
      iwconfig $iface key $key
   done
   eend 0
}

stop() {
   checkconfig || return 1
   for iface in $IFACES; do
      ebegin "Unsetting ssids"
      einfo "Unsetting ssid on $iface"
      iwconfig $iface essid any
      iwconfig $iface key off
   done
   eend 0
}


in your case, I suppose an automatic wireless capability detection would suit. any ideas? (but only detection of the interfaces in the IFACES variable)

for the case of two access points without SSIDs then the best one would be chosen (think windows xp wireless utility (which *requires* ssid broadcast) ) only better
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6835
Location: Blighty

PostPosted: Tue Jan 25, 2005 2:20 pm    Post subject: Reply with quote

martinoc wrote:

in your case, I suppose an automatic wireless capability detection would suit. any ideas? (but only detection of the interfaces in the IFACES variable)


Code:

if ! grep -q "${iface}:" /proc/net/wireless ; then
   echo "${iface} is not a wireless interface"
fi


Quote:

for the case of two access points without SSIDs then the best one would be chosen (think windows xp wireless utility (which *requires* ssid broadcast) ) only better


Who defines the best one? You? How? You have no code to detect if you've associated with any AP.

The driver defines the best one? Different drivers select different AP's based on different criteria.

I suggest you emerge >=baselayout-1.11.8 and checkout the /lib/rcscripts/net.modules.d/iwconfig file for sample code to test association, picking AP's based on availability, signal strength and other things.

BTW, the XP wireless Zero Configuration only requires ssid broadcast to configure the AP on the client. Once it's been configured you can turn off broadcasting.
Back to top
View user's profile Send private message
martinoc
n00b
n00b


Joined: 12 Jan 2005
Posts: 12
Location: Ireland

PostPosted: Tue Jan 25, 2005 3:33 pm    Post subject: Reply with quote

Quote:

for the case of two access points without SSIDs then the best one would be chosen (think windows xp wireless utility (which *requires* ssid broadcast) ) only better


by best one, i mean best preference from the user's point of view based on how they have ordered their $<iface>_$<ssid> list.

The functionality works for me in the environment in which i use wireless equipment.

btw, although the support exists in the newer baselayout, I am taking this primarily as a scripting excersise for my own benefit. But if any aspect of my script lends any ideas to the baselayout then im happy.

anyone not ready to use baselayout-1.11.X could find this a quick and dirty script to use in the mean time
Back to top
View user's profile Send private message
UberLord
Retired Dev
Retired Dev


Joined: 18 Sep 2003
Posts: 6835
Location: Blighty

PostPosted: Tue Jan 25, 2005 3:42 pm    Post subject: Reply with quote

martinoc wrote:

btw, although the support exists in the newer baselayout, I am taking this primarily as a scripting excersise for my own benefit. But if any aspect of my script lends any ideas to the baselayout then im happy.

anyone not ready to use baselayout-1.11.X could find this a quick and dirty script to use in the mean time


I'm always open for ideas about how to improve baselayout :) Although, if you write any patches you may want to post them to bugs.gentoo.org instead of here.

I hope I was able to help your scripting skills - or at least a little sed :)
Back to top
View user's profile Send private message
martinoc
n00b
n00b


Joined: 12 Jan 2005
Posts: 12
Location: Ireland

PostPosted: Fri Jan 28, 2005 1:11 pm    Post subject: Reply with quote

well, here is the most up to date version of my script.
I've worked out most of the bugs.

Code:
#!/sbin/runscript

#NB: Config is in /etc/conf.d/ssid

depend() {
   use hotplug pcmcia
   before net.*
}

checkconfig() {
   if [ -z "$IFACES" ]; then
           eerror "You need to setup IFACE /etc/conf.d/ssid first"
           return 1
   fi
                  
}

start() {
   checkconfig || return 1
   ebegin "Setting ssids"
   for iface in $IFACES; do
      if [ -z "`iwconfig 2>&1 | grep $iface`" ]; then
         ewarn "$iface not found, skipping"
         continue
      fi
      if ! grep -q "${iface}:" /proc/net/wireless ; then
         ewarn "$iface is not a wireless device, skipping"
         continue
      fi
      available=$( iwlist ${iface} scanning 2>&1 | sed -ne 's/^ *ESSID:"\(.*\)"$/\1/p' )
      eval SSIDS=( \"\$${iface}_SSIDS\" )
      if [ -z "$SSIDS" ]; then
      ssid="any"
      key="off"
      ewarn "${iface}_SSIDS not set in /etc/conf.d/ssid"
      else
         local ssid_found=0
         for ssid in $SSIDS; do
            for avail in $available; do
               if [ $avail = $ssid ]; then
                  eval key=( \"\$${iface}_${ssid}\")
                  if [ -z "$key" ]; then
                     ewarn "${iface}_$ssid not set in /etc/conf.d/ssid"
                     key="off"
                     ssid="any"
                  fi
                  local ssid_found=1
                  break 2
               fi
            done
         done
         if [ $ssid_found -eq 0 ]; then
            ewarn "No configured ssids broadcasting on $iface"
            eval default_IFACE=( \"\$default_${iface}\" )
            if [ -z "$default_IFACE" ]; then
               ssid="any"
               key="off"
               ewarn "No default ssid set for ${iface}"
            else
               ssid=$default_IFACE
               eval key=( \"\$${iface}_${ssid}\" )
               einfo "Using default ssid for ${iface}"
            fi
         fi
      fi
      einfo "Setting ssid to $ssid on $iface"
      iwconfig $iface essid "$ssid"
      iwconfig $iface key $key
   done
   eend 0
}

stop() {
   checkconfig || return 1
   ebegin "Unsetting ssids"
   for iface in $IFACES; do
      if [ -z "`iwconfig 2>&1 | grep $iface`" ]; then
              ewarn "$iface not found, skipping"
         continue
      fi
      if ! grep -q "${iface}:" /proc/net/wireless ; then
                   ewarn "$iface is not a wireless device, skipping"
                   continue
           fi
      einfo "Unsetting ssid on $iface"
      iwconfig $iface essid any
      iwconfig $iface key off
   done
   eend 0
}


hope it proves usefull to someone else as it does to me
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum