WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Tool for setup of wireless connections  (Read 11612 times)

Offline helander

  • Full Member
  • ***
  • Posts: 183
Tool for setup of wireless connections
« on: July 03, 2009, 05:54:41 AM »
I have produced a small script that allows you to start up a wireless connection.
It presents a form of available wireless network devices (most systems just have one).
Just select device you intend to start and then you will be presented with the wireless networks currently "visible" via this interface. Just select the one to connect to and after a few seconds you should have a full IP connection via the selected network.

Depends on: wireless-tools extension and kernel modules for your wireless hardware.

Issues: No support for encryption.
             Only tested on a few systems.

Here is the script. Use it as is or just to get ideas on the steps required to start up a connection.

Code: [Select]
#!/bin/sh


exit_script()
{
   rm /tmp/wlans 2>/dev/null
   exit 0
}



DEVS=`cat /proc/net/wireless | grep : | sed 's/://' | awk '{ print $1 }' `

DEVICE_SELECTIONS=`for x in $DEVS; do echo $x $(ls /sys/class/net/$x/device/driver/module/drivers); done`

DEVICE="No device selected."
NET="No network selected."

devs_dialog()
{
   dialog --title "Select wireless network" --help-button --menu "Select Device" 12 40 5 \
      $DEVICE_SELECTIONS  \
      2>/tmp/wlans
   RESULT="$?"
   case $RESULT in
     0) DEVICE="$(cat /tmp/wlans)"
        nets_dialog;;
     2) dialog --clear --title "Select wireless network Help" --msgbox \
"Provides a selectable list of available wireless networks.\n\n\
Select a device to see the available networks for the device.\n\n\
Select a network to use." 11 70
        devs_dialog;;
     *) exit_script;;
   esac
}

nets_dialog()
{
   NETS=`sudo iwlist $DEVICE scan | grep ESSID | sed 's/ESSID://' | sed 's/\"//' | sed 's/\"//' | awk '{ print $1 }' `
   NET_SELECTIONS=`for x in $NETS; do echo $x net; done`

   dialog --title "Select wireless network" --help-button --menu "Select Network" 12 40 5 \
      $NET_SELECTIONS  \
      2>/tmp/wlans
   RESULT="$?"
   case $RESULT in
     0) NET="$(cat /tmp/wlans)";;
     2) dialog --clear --title "Select wireless network Help" --msgbox \
"Provides a selectable list of available wireless networks for $DEVICE.\n\n\
Select a network to use." 11 70
        nets_dialog;;
     *) exit_script;;
   esac
}


devs_dialog

sudo iwconfig $DEVICE essid off
sleep 2
sudo iwconfig $DEVICE essid $NET
sleep 2
iwconfig
sudo udhcpc -i $DEVICE
ifconfig $DEVICE

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11044
Re: Tool for setup of wireless connections
« Reply #1 on: July 03, 2009, 05:58:07 AM »
Idea: replace dialog with a variable, setting it to dialog in the start. This way you can make this a GUI, by checking if Xdialog is available, and if so, use it instead of dialog. Xdialog should be 100% compatible with dialog, requiring no changes to the commands.
The only barriers that can stop you are the ones you create yourself.

Offline helander

  • Full Member
  • ***
  • Posts: 183
Re: Tool for setup of wireless connections
« Reply #2 on: July 03, 2009, 06:36:10 AM »
Idea: replace dialog with a variable, setting it to dialog in the start. This way you can make this a GUI, by checking if Xdialog is available, and if so, use it instead of dialog. Xdialog should be 100% compatible with dialog, requiring no changes to the commands.

Thanks. Sounds like a good idea.

What happens if you run Xdialog when X is not started? Maybe you want to make the connection as part of the startup procedure, e.g. a bootlocal script or a tce.installed script. In case Xdialog itself automatically does not "revert" to "dialog" if X is not started,  what is the best way to check if X is running or not?

/Lars

Offline florian

  • Full Member
  • ***
  • Posts: 116
    • Home Page
Re: Tool for setup of wireless connections
« Reply #3 on: July 03, 2009, 07:11:12 AM »
Quote
I have produced a small script that allows you to start up a wireless connection.
...

I very much like this idea of tiny dialog-based gui wrapper around wireless_tools. There are some full blown guis to do this, but they often depends on qt, gtk2, python,etc... nothing really mini.

Couple of suggestions:
1) If there is only one wireless device available, as is the often the case, then perhaps do not show the device dialog (on the other hand, selected device could be shown the wireless access point dialog)
2) iwlist scan return information wether the access point is encrypted and about the signal quality. I think those two info would be important to also show in the selection list somehow


Quote
what is the best way to check if X is running or not?
you could use a function like this:

Code: [Select]
isxwin() {
  # return 0 if started under x-window, 1 otherwise
  [ -z "$DISPLAY" ] && return 1
  [ -n "$WINDOWID" ] && return 0
  case "$TERM" in xterm*|rxvt) return 0;; esac
  return 1
}

Personally, I'm not disturbed with running text-based applications in X. So I like dialog!

Also on a (personal) aesthetic point of view, I much prefer the default "traditional" look'n feel for dialog (with grey panels on blue background and apparent shadow for the widgets) than the appearance included in TC base. So I just "rm /etc/dialogrc" (in my /opt/bootlocal.sh) and now my dialogs-based interface look much better.

BR, Florian

Offline clivesay

  • Retired Admins
  • Jr. Member
  • *****
  • Posts: 65
Re: Tool for setup of wireless connections
« Reply #4 on: July 03, 2009, 08:36:25 AM »
I'm not good on developer type things but maybe ceni from sidux is a tool option? Since it's from a full KDE distro it may be too fat with deps. It's kind of a terminal type tool.
Chris
IRC Freenode #tinycorelinux

Offline helander

  • Full Member
  • ***
  • Posts: 183
Re: Tool for setup of wireless connections
« Reply #5 on: July 03, 2009, 10:42:00 AM »
Couple of suggestions:
1) If there is only one wireless device available, as is the often the case, then perhaps do not show the device dialog (on the other hand, selected device could be shown the wireless access point dialog)
2) iwlist scan return information wether the access point is encrypted and about the signal quality. I think those two info would be important to also show in the selection list somehow

I will look into making these modifications.

Thanks for the X detection code.

/Lars

Offline helander

  • Full Member
  • ***
  • Posts: 183
Re: Tool for setup of wireless connections
« Reply #6 on: July 03, 2009, 02:16:25 PM »
Here is an updated version of the script. The following modifications of the script has been applied, based on suggestions from other forum members.

  • Use Xdialog if installed and if you are in an X environment
  • Do not enter device selection menu in case exactly one wireless device exists in the system
  • Indicate if network requires encryption or not

Your preferences regarding Xdialog or dialog can easily be fixed by slight modifications to the code  :)

Code: [Select]
#!/bin/sh


exit_script()
{
   rm /tmp/wlans 2>/dev/null
   exit 0
}

count()
{
  echo $#
}

isxwin() {
  # return 0 if started under x-window, 1 otherwise
  [ -z "$DISPLAY" ] && return 1
  [ -n "$WINDOWID" ] && return 0
  case "$TERM" in xterm*|rxvt) return 0;; esac
  return 1
}

XDIALOG="`which Xdialog`"
DIALOG="`which dialog`"

isxwin && [ -n $XDIALOG ] && DIALOG=$XDIALOG

DEVS=`cat /proc/net/wireless | grep : | sed 's/://' | awk '{ print $1 }' `

DEVICE_SELECTIONS=`for x in $DEVS; do echo $x $(ls /sys/class/net/$x/device/driver/module/drivers); done`

DEVICE="No device selected."
NET="No network selected."



devs_dialog()
{
   $DIALOG --title "Select wireless network" --help-button --menu "Select Device" 12 40 5 \
      $DEVICE_SELECTIONS  \
      2>/tmp/wlans
   RESULT="$?"
   case $RESULT in
     0) DEVICE="$(cat /tmp/wlans)"
        nets_dialog;;
     2) dialog --clear --title "Select wireless network Help" --msgbox \
"Provides a selectable list of available wireless networks.\n\n\
Select a device to see the available networks for the device.\n\n\
Select a network to use." 11 70
        devs_dialog;;
     *) exit_script;;
   esac
}

nets_dialog()
{
   NETS=`sudo iwlist $DEVICE scan | grep -e ESSID -e Encryption | sed 's/ESSID://' | sed 's/\"//' | sed 's/\"//' | sed 's/Encryption key/Security/'`
   NET_SELECTIONS=`for x in $NETS; do echo $x; done`

   $DIALOG --title "Select wireless network" --help-button --menu "Select Network" 12 40 5 \
      $NET_SELECTIONS  \
      2>/tmp/wlans
   RESULT="$?"
   case $RESULT in
     0) NET="$(cat /tmp/wlans)";;
     2) dialog --clear --title "Select wireless network Help" --msgbox \
"Provides a selectable list of available wireless networks for $DEVICE.\n\n\
Select a network to use." 11 70
        nets_dialog;;
     *) exit_script;;
   esac
}

if [ `count $DEVS` = "1" ] ; then
  DEVICE=$DEVS
  nets_dialog
else
  devs_dialog
fi

sudo iwconfig $DEVICE essid off
sleep 2
sudo iwconfig $DEVICE essid $NET
sleep 2
iwconfig
sudo udhcpc -i $DEVICE
ifconfig $DEVICE


/Lars
« Last Edit: July 03, 2009, 02:18:02 PM by helander »

Offline clach04

  • Newbie
  • *
  • Posts: 31
Re: Tool for setup of wireless connections
« Reply #7 on: September 05, 2009, 06:39:44 PM »
Nice idea, there are a few other scripts like this around but they tend to rely on a lot of dependencies. I've made a couple of changes to it, see below. Is there any value in putting this in the excellent Wifi wiki page at http://wiki.tinycorelinux.com/tiki-index.php?page=Setting+up+Wifi ? I'm happy to go ahead and make the change to the wiki page (assuming I can create an account, I've not tried yet, I do know the my forums account doesn't work there).

Fixes a few problems that it had on my machine:
  • didn't work with my Orinoco Gold card
  • I don't have Xdialog installed (testing with TinyCore 2.3 today)

Chris

Code: [Select]
#!/bin/sh
# from http://forum.tinycorelinux.net/index.php?topic=2221.0
# can be called as regular user (assuming they have sudo perms, i.e. this script mans sudo calls)


exit_script()
{
   rm /tmp/wlans 2>/dev/null
   exit 0
}

count()
{
  echo $#
}

isxwin() {
  # return 0 if started under x-window, 1 otherwise
  [ -z "$DISPLAY" ] && return 1
  [ -n "$WINDOWID" ] && return 0
  case "$TERM" in xterm*|rxvt) return 0;; esac
  return 1
}

XDIALOG="`which Xdialog`"
DIALOG="`which dialog`"
if [ -z "$XDIALOG" ]
then
    if [ -z "$DIALOG" ]
    then
        echo no dialog implementation found
        exit 1
    fi
    XDIALOG=$DIALOG
fi

isxwin && [ -n $XDIALOG ] && DIALOG=$XDIALOG
#echo DEBUG DIALOG $DIALOG

list_wireless_alldevices()
{
    # Old drivers (e.g. orinoco is not listed in /proc)
    iwconfig 2>/dev/null |grep IEEE |awk '{print $1}'

    # New drivers
    cat /proc/net/wireless | grep : | sed 's/://' | awk '{ print $1 }'
}
list_wireless_devices()
{
   list_wireless_alldevices | sort -u
}
wireless_filter()
{
    # filter/process the output from "iwlist DEV scan"
    # Example, we could filter out all the locked down APs
    grep -e ESSID -e Encryption | sed 's/ESSID://' | sed 's/\"//' | sed 's/\"//' | sed 's/Encryption key/Security/'
}
list_wireless_essid()                                                                                 
{
    wlan=$1
    sudo ifconfig $wlan up
   
    # ensure essid is not already set, so we actually scan for all
    sudo iwconfig $wlan essid any
    # Works for most
    sudo iwlist $wlan scan
    # Could use: iwlist scanning
   
    return
    #exit 23
    # as yet unused code to loop through channels in case scan isn't working on the card
    for x in  0 1 2 3 4 5 6 7 8 9 10 11 12 13
    do
        #echo $x
        sudo iwconfig $wlan channel $x >/dev/null 2>&1
        sudo iwlist $wlan scan
    done
    # may need to unique sort above output....
}
DEVS=`list_wireless_devices`
#echo DEBUG DEVS $DEVS

DEVICE_SELECTIONS=`for x in $DEVS; do echo $x $(ls /sys/class/net/$x/device/driver/module/drivers); done`
#echo DEBUG DEVICE_SELECTIONS $DEVICE_SELECTIONS

DEVICE="No device selected."
NET="No network selected."



devs_dialog()
{
   $DIALOG --title "Select wireless network" --help-button --menu "Select Device" 12 40 5 \
      $DEVICE_SELECTIONS  \
      2>/tmp/wlans
   RESULT="$?"
   case $RESULT in
     0) DEVICE="$(cat /tmp/wlans)"
        nets_dialog;;
     2) dialog --clear --title "Select wireless network Help" --msgbox \
"Provides a selectable list of available wireless networks.\n\n\
Select a device to see the available networks for the device.\n\n\
Select a network to use." 11 70
        devs_dialog;;
     *) exit_script;;
   esac
}

nets_dialog()
{
   NETS=`list_wireless_essid $DEVICE | wireless_filter`
   #echo DEBUG NETS $NETS
   NET_SELECTIONS=`for x in $NETS; do echo $x; done`

   $DIALOG --title "Select wireless network" --help-button --menu "Select Network" 12 40 5 \
      $NET_SELECTIONS  \
      2>/tmp/wlans
   RESULT="$?"
   case $RESULT in
     0) NET="$(cat /tmp/wlans)";;
     2) dialog --clear --title "Select wireless network Help" --msgbox \
"Provides a selectable list of available wireless networks for $DEVICE.\n\n\
Select a network to use." 11 70
        nets_dialog;;
     *) exit_script;;
   esac
}

if [ `count $DEVS` = "1" ] ; then
  DEVICE=$DEVS
  nets_dialog
else
  devs_dialog
fi

sudo iwconfig $DEVICE essid off
sleep 2
sudo iwconfig $DEVICE essid $NET
sleep 2
iwconfig
sudo udhcpc -i $DEVICE
ifconfig $DEVICE

Offline ^thehatsrule^

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 1726
Re: Tool for setup of wireless connections
« Reply #8 on: September 10, 2009, 02:00:43 AM »
Is there any value in putting this in the excellent Wifi wiki page at http://wiki.tinycorelinux.com/tiki-index.php?page=Setting+up+Wifi ? I'm happy to go ahead and make the change to the wiki page (assuming I can create an account, I've not tried yet, I do know the my forums account doesn't work there).
Adding a section that refers to this thread is fine... and yes, it requires separate registration from the forum.

Offline gwalther

  • Full Member
  • ***
  • Posts: 139
Re: Tool for setup of wireless connections
« Reply #9 on: July 29, 2010, 09:21:37 AM »
This is a very useful script when travelling, however if the network has a space in the name (ie Windswept Guest),
 Xdialog gets confused and lists the second part of the name (ie Guest) as the encryption status.

Is there a easy fix.?.(.I tried and failed)

Offline clach04

  • Newbie
  • *
  • Posts: 31
Re: Tool for setup of wireless connections
« Reply #10 on: July 29, 2010, 09:21:04 PM »
This is a very useful script when travelling, however if the network has a space in the name (ie Windswept Guest),
 Xdialog gets confused and lists the second part of the name (ie Guest) as the encryption status.

Is there a easy fix.?.(.I tried and failed)

Easyish.... :-) See below, note I managed to find an access point that had a space in the name so this has been tested :-)

Code: [Select]
#!/bin/sh
# from http://forum.tinycorelinux.net/index.php?topic=2221.0
# can be called as regular user (assuming they have sudo perms, i.e. this script makes sudo calls)


exit_script()
{
   rm /tmp/wlans /tmp/wlans.sh 2>/dev/null
   exit 0
}

count()
{
  echo $#
}

isxwin() {
  # return 0 if started under x-window, 1 otherwise
  [ -z "$DISPLAY" ] && return 1
  [ -n "$WINDOWID" ] && return 0
  case "$TERM" in xterm*|rxvt) return 0;; esac
  return 1
}

XDIALOG="`which Xdialog`"
DIALOG="`which dialog`"
if [ -z "$XDIALOG" ]
then
    if [ -z "$DIALOG" ]
    then
        echo no dialog implementation found
        exit 1
    fi
    XDIALOG=$DIALOG
fi

isxwin && [ -n $XDIALOG ] && DIALOG=$XDIALOG
#echo DEBUG DIALOG $DIALOG

list_wireless_alldevices()
{
    # Old drivers (e.g. orinoco is not listed in /proc)
    iwconfig 2>/dev/null |grep IEEE |awk '{print $1}'

    # New drivers
    cat /proc/net/wireless | grep : | sed 's/://' | awk '{ print $1 }'
}
list_wireless_devices()
{
   list_wireless_alldevices | sort -u
}
wireless_filter()
{
    # filter/process the output from "iwlist DEV scan"
    # Example, we could filter out all the locked down APs
    #grep -e ESSID -e Encryption | sed 's/ESSID://' | sed 's/\"//' | sed 's/\"//' | sed 's/Encryption key/Security/'
    grep -e ESSID -e Encryption | sed 's/ESSID://' | sed 's/Encryption key/Security/'
}
list_wireless_essid()                                                                                 
{
    wlan=$1
    sudo ifconfig $wlan up
   
    # ensure essid is not already set, so we actually scan for all
    sudo iwconfig $wlan essid any
    # Works for most
    sudo iwlist $wlan scan
    # Could use: iwlist scanning
   
    return
    #exit 23
    # as yet unused code to loop through channels in case scan isn't working on the card
    for x in  0 1 2 3 4 5 6 7 8 9 10 11 12 13
    do
        #echo $x
        sudo iwconfig $wlan channel $x >/dev/null 2>&1
        sudo iwlist $wlan scan
    done
    # may need to unique sort above output....
}
DEVS=`list_wireless_devices`
#echo DEBUG DEVS $DEVS

DEVICE_SELECTIONS=`for x in $DEVS; do echo $x $(ls /sys/class/net/$x/device/driver/module/drivers); done`
#echo DEBUG DEVICE_SELECTIONS $DEVICE_SELECTIONS

DEVICE="No device selected."
NET="No network selected."

# Fake dialog, used for debugging (maybe handle missing dialog/xdialog case?)
fake_dialog()
{
    echo FAKE DIALOG
    echo ${*}
    # make a bunch of assumption
    title=${2}
    prompt_text=${5}
    echo Title: ${title}
    echo ${prompt_text}
    shift ; shift ; shift ; shift ; shift ; shift ; shift ; shift ;
    echo options "${*}"
    return 255
}
#DIALOG=fake_dialog

devs_dialog()
{
    echo $DEVICE_SELECTIONS
   $DIALOG --title "Select wireless network" --help-button --menu "Select Device" 12 40 5 \
      $DEVICE_SELECTIONS  \
      2>/tmp/wlans
   RESULT="$?"
   cat /tmp/wlans
   case $RESULT in
     0) DEVICE="$(cat /tmp/wlans)"
        nets_dialog;;
     2) dialog --clear --title "Select wireless network Help" --msgbox \
"Provides a selectable list of available wireless networks.\n\n\
Select a device to see the available networks for the device.\n\n\
Select a network to use." 11 70
        devs_dialog;;
     *) exit_script;;
   esac
}

nets_dialog()
{
    # debug scanner
    echo '---------------'
    list_wireless_essid $DEVICE | wireless_filter
    echo '---------------'
    # echo press return ; read xyz
   NETS=`list_wireless_essid $DEVICE | wireless_filter`
   #echo DEBUG NETS $NETS
   NET_SELECTIONS=`for x in $NETS; do echo $x; done`
   echo $DIALOG '--title "Select wireless network" --help-button --menu "Select Network" 12 70 20' $NET_SELECTIONS '2>/tmp/wlans' > /tmp/wlans.sh
   echo 'exit $?' >> /tmp/wlans.sh
   sh /tmp/wlans.sh
   
   RESULT="$?"
   case $RESULT in
     0) NET="$(cat /tmp/wlans)";;
     2) dialog --clear --title "Select wireless network Help" --msgbox \
"Provides a selectable list of available wireless networks for $DEVICE.\n\n\
Select a network to use." 11 70
        nets_dialog;;
     *) exit_script;;
   esac
}

if [ `count $DEVS` = "1" ] ; then
  DEVICE=$DEVS
  nets_dialog
else
  devs_dialog
fi

sudo iwconfig $DEVICE essid off
sleep 2
sudo iwconfig $DEVICE essid "$NET"
sleep 2
iwconfig
sudo udhcpc -i $DEVICE
ifconfig $DEVICE


# EDIT updated with wider/longer menu area (one line change to dialog call).
« Last Edit: July 29, 2010, 09:30:43 PM by clach04 »

Offline gwalther

  • Full Member
  • ***
  • Posts: 139
Re: Tool for setup of wireless connections
« Reply #11 on: July 30, 2010, 07:14:34 AM »
Works great...spaces in name now no problem......thanks so much.
I have a lot to learn about scripting