Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 21 Nov 2017, 11:57
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Multi-Wifi-Interface scan and connect "Wifi-Scanner-2"
Post new topic   Reply to topic View previous topic :: View next topic
Page 2 of 2 [30 Posts]   Goto page: Previous 1, 2
Author Message
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Fri 09 Jun 2017, 18:31    Post subject:  

I own 5 usb wifi dongles plus the inbuilt pcie. Each card has different sensitivities and I use extension aerial cables to receive scan data from different sides of my building. Scanning with several wifi cards lets me compare the strengths and weaknesses of he cards. Also two interfaces for joining network and creating hotspot. Also just because I wanted to program it.

Eventually i want to develope auto switching between several cards.
Back to top
View user's profile Send private message MSN Messenger 
slavvo67

Joined: 12 Oct 2012
Posts: 1426
Location: The other Mr. 305

PostPosted: Fri 09 Jun 2017, 19:03    Post subject:  

I have a number of wifi dongles, as well. Never thought to use them in that way. I've been wanting to try the hotspot idea for a while.... Any tips?
Back to top
View user's profile Send private message 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Sat 10 Jun 2017, 02:43    Post subject:  

Wifi-HotSpot uses create_ap backend. 'create_ap' gets compiled on first use but the devx must be loaded. Or you can simply cd to /usr/sbin/create_ap-master then open terminal there and
Code:
make install

check installation and get useful info

Code:
create_ap


Wifi-HotSpot gui has information button
xscreenshot-20170610T073644.png
 Description   
 Filesize   29.39 KB
 Viewed   478 Time(s)

xscreenshot-20170610T073644.png

whm.png
 Description   click larger icon for full info.
 Filesize   23.58 KB
 Viewed   479 Time(s)

whm.png

xscreenshot-20170610T073658.png
 Description   
 Filesize   20.43 KB
 Viewed   483 Time(s)

xscreenshot-20170610T073658.png

Back to top
View user's profile Send private message MSN Messenger 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Thu 06 Jul 2017, 15:53    Post subject:  

see last or first post
Last edited by stemsee on Thu 26 Oct 2017, 09:47; edited 8 times in total
Back to top
View user's profile Send private message MSN Messenger 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Sat 08 Jul 2017, 05:29    Post subject:  

The one thing I would like to add now is a static tab for control options.
Back to top
View user's profile Send private message MSN Messenger 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Tue 05 Sep 2017, 15:07    Post subject:  

This is my main wifi ap connection tool. It is now faster than previously. There are also un-stated options; for example closing the gui using the 'x' in the top right corner causes immediate rescan and gui display reopen. Using 'close' ends all scanning. Using 'ok' with a selected ap causes connection, without selected ap causes immediate rescan. Leaving alone cause timed close and rescan.
Back to top
View user's profile Send private message MSN Messenger 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Fri 20 Oct 2017, 19:02    Post subject:  

I had not been able to connect to an android wpa-psk secure hotspot until today!

I spent hours searching for the reason. Finally using wpa_supplicant without the -B but option revealed a list of errors which I researched online. Finally I found that the command without the -D driver specification is the only one that works... eureka!
Code:
wpa_supplicant -B -i $int -c /tmp/netsel.conf

is all that is required having first generated the .conf using
Code:
wpa_passphrase "$ssid" "$password" > /tmp/netsel.conf


So I see just how frustrating using my Wifi-Scanner-2 must have been for anything other than open networks and those with 2nd stage security protocols such as eduroam.

one problem solved!

EDIT: Actually the reason seems more to do with the broadcom chipset or driver! As using all plugin cards work fine with wext.

Last edited by stemsee on Tue 14 Nov 2017, 16:14; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Thu 26 Oct 2017, 09:47    Post subject:  

see update
Back to top
View user's profile Send private message MSN Messenger 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Tue 14 Nov 2017, 16:16    Post subject:  

Update provides: psk correctly implemented.
used interface eliminated from continued scan option.
while loops have sleep to stop uncontrolled cpu use.
a pop-up gui input box which lets the user specify driver + options for wpa_supplicant connection.
It is an extended password and id box which greys out unused selections.

It is much faster now too. The fastest wifi and wired connection utility on the planet!!!

Still to do: remove sed commands. add wps. add auto connect. add use saved profiles.
Wifi-Scanner-2.fake.gz
Description 
gz

 Download 
Filename  Wifi-Scanner-2.fake.gz 
Filesize  24.73 KB 
Downloaded  2 Time(s) 
xscreenshot-20171114T193453.png
 Description   
 Filesize   26.08 KB
 Viewed   205 Time(s)

xscreenshot-20171114T193453.png

xscreenshot-20171114T193733.png
 Description   
 Filesize   24.85 KB
 Viewed   206 Time(s)

xscreenshot-20171114T193733.png


Last edited by stemsee on Yesterday, at 18:20; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Wed 15 Nov 2017, 12:27    Post subject:  

Added autoconnect/no-auto feature which starts wifi connection with a default profile. The script is /etc/xdg/Startup/autocon.

Also corrected udhcpc hook default.script location to /usr/share/udhcpc/default.script in fatdog anyhow. Dhcpcd is still available if 'busybox which udhcpc' returns empty.
Back to top
View user's profile Send private message MSN Messenger 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Thu 16 Nov 2017, 11:10    Post subject:  

I have tidied up the formatting within the script - no more long lines!

wpa_cli option in menu with interface selection available. So wps is available for those who know how to
Code:
wpa_pin/wpa_pbs


see further on for script

Last edited by stemsee on Yesterday, at 17:43; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1392
Location: Japan

PostPosted: Sat 18 Nov 2017, 08:50    Post subject:  

Code:
   #make sure associate interface variables
   int1=${int1}
   int2=${int2}
   int3=${int3}
   int4=${int4}
   int5=${int5}
   int6=${int6}
   int7=${int7}
   int8=${int8}
What is the purpose of this exercise?
Back to top
View user's profile Send private message 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Yesterday, at 10:06    Post subject:  

@MochiMoppel

The purpose was at one point to make sure that after $int was used in a connection the other wireless interfaces '$inters' selected for scanning would fully occupy the variables with '$int' removed from scanning interfaces, as it should be connected. For me it was a temporary measure that I forgot about even though it no longer made any difference. Now removed.

Sed statements have also been reduced.

now I cannot yet get the saved profilesfn selected profile to connect.
Code:
#!/bin/sh
# multi-interface wifi scanner and connection app
# copyright (c) 2017 Marcos Contant
# provides wpa_cli option in menu
# udhcpc plus dhcpcd fallback
# random mac and mac edit
# profile creation
# autoconnect function
# manage saved profiles
# wpa/psk
# eduroam 2 phase authentication
#
res=`xrandr | cut -f8,9,10 -d' ' | cut -f1 -d',' | head -1 | sed 's/ //g'`
XX=`echo $res | cut -f1 -d'x'`
YY=`echo $res | cut -f2 -d'x'`
pos="+$((XX - 640))+$((YY - YY + 20))"
export pos
export XX
export YY
if [[ "$1" = admin ]]; then
   secs=16
else
   secs=32
fi
ranmacfn () {
INTERFACE=$int
OUI="/opt/local/etc/oui.txt"
OUI_LEN=28000
        R=$(((RANDOM % ${OUI_LEN})+1))
        PREFIX=`head -$R $OUI | tail -1`
        CMD="ifconfig ${INTERFACE} hw ether ${PREFIX}:"
        for i in `seq 1 6`; do
            R=$(((RANDOM % 15)+1))
            C=`echo "obase=16; $R" | bc`
            CMD="$CMD$C"
            if [ $((i % 2)) = 0 ] && [ $i != 6 ]; then
                CMD="$CMD:"
            fi
        done
        RESULT=`$CMD`
          CMD=`echo $CMD | cut -f5 -d' '`
          export CMD
}
export -f ranmacfn
function wpaclifn (){
netwad=`ls /sys/class/net | grep --invert-match 'ap' | tr '\n' ','`
options=`yad --window-icon=/usr/share/pixmaps/wifi24.png --title="Wifi-Scanner-2" \
--item-separator="," --geometry=200$pos --form --field="Select Interfaces":CBE "$netwad" \
--field="Driver Options":CBE "wext,nl80211,wired" \
--field="MODE":CBE "managed,master,ad-hoc,monitor,normal" \
--field="wpa_supplicant.conf":TXT "ctrl_interface=/var/run/wpa_supplicant\nctrl_interface_group=0\nupdate_config=1"`
ret=$?
[[ $ret -eq 1 ]] && exit 0
card=`echo $options | cut -f1 -d'|'`
driver=`echo $options | cut -f2 -d'|'`
mde=`echo $options | cut -f3 -d'|'`
wconf=`echo $options | cut -f4 -d'|'`
export card
export mde
if [[ "$int" != "$card" ]]; then
   bash -c startinfn2
fi
echo -e "$wconf" > /tmp/wpa_supplicant.conf
wpa_supplicant -B -D"$driver" -i"$card" -c/tmp/wpa_supplicant.conf
sleep 1
xterm -e wpa_cli -i$card
}
export -f wpaclifn

interfacefn () {
for wit in `ls /sys/class/net`
do
   case $wit in
      eth0|eth2|eth4|eth6|enp2s0f2) eth=${wit};;
      eth1|eth3|eth5|eth7) eths=${wit};;
      enp0s20u3|usb0|usb1) usb0=${wit};;
   esac
done
export eth
export eths
export usb0
}
interfacefn
depsfn () {        # check dependencies
   dhclu=`busybox which udhcpc`
   dhcld=`busybox which dhcpcd`
   if [[ ! -z "$dhclu" ]]; then
      dhl1="udhcpc -A 8 -p /var/run/udhcpc.pid -s /usr/share/udhcpc/default.script -i"
      dhl2="/usr/share/udhcpc/default.script deconfig"
      dhl3="udhcpc"
   elif [[ -z "$dhclu" ]]; then
      dhl1="dhcpcd -z"
      dhl2="dhcpcd -k"
      dhl3="dhcpcd"
   elif [[ -z "$dhclu" && -z "$dhcld" ]]; then
      zenity --info --text "$dhclu and $dhcld not found"
      echo "$dhclu and $dhcld not found"
   fi
   export dhl1
   export dhl2
   export dhl3
}
depsfn
# Menu activated options for dis/connecting wired interfaces etc
if [[ ! -z "$1" ]]; then
   interfacefn
fi
if [[ "$1" = eth0-u ]]; then
   ifconfig $eth up
   ${dhl3} $eth
   exit 0
fi
if [[ "$1" = eth0-d ]]; then
   ifconfig $eth down
   ${dhl2} $eth
   exit 0
fi
if [[ "$1" = eth1-u ]]; then
   ifconfig ${eths} up
   ${dhl3} ${eths}
   exit 0
fi
if [[ "$1" = eth1-d ]]; then
   ifconfig ${eths} down
   ${dhl2} ${eths}
   exit 0
fi
if [[ "$1" = wlan0-d ]]; then
   ifconfig $int down
   ${dhl2} $int
   exit 0
fi
if [[ "$1" = wlan0-u ]]; then
   ifconfig $int up
   ${dhl1} $int
   exit 0
fi
if [[ "$1" = wlan1-d ]]; then
   ifconfig $int down
   ${dhl2} $int
   exit 0
fi
if [[ "$1" = usb0-u ]]; then
   ifconfig ${usb0} up
   ${dhl3} ${usb0}
   exit 0
fi
if [[ "$1" = usb0-d ]]; then
   ifconfig ${usb0} down
   ${dhl2} ${usb0}
   exit 0
fi
   # cleanup
rm /tmp/interfaces 2> /dev/null
rm /tmp/interface 2> /dev/null
rm /tmp/net 2> /dev/null
rm /tmp/netsel 2> /dev/null
[[ /tmp/wifitray ]] && /tmp/wifitray 2> /dev/null
rm -f /tmp/wifitray 2> /dev/null
rm -f /tmp/onclick 2> /dev/null
rm -f /tmp/scans* 2> /dev/null
   # unset variables
for i in int int1 int2 int3 int4 int5 int6 int7 int8 these1 \
these2 these3 these4 these5 these6 these7 these8
do
   unset $i 2> /dev/null
done
for i in int int1 int2 int3 int4 int5 int6 int7 int8
do
   if [[ -e /dev/$i ]]; then
      ifconfig $i down
      iproute flush dev $i
      ifconfig $i up
   fi
done
wlanfn () {     # select interfaces to scan with
[[ /tmp/interfaces ]] && rm /tmp/interfaces
inters=`ls /sys/class/net | grep --invert-match 'eth' | grep --invert-match 'ap' | \
grep --invert-match 'lo' | yad --window-icon=/usr/share/pixmaps/wifi24.png \
--title="Wifi-Scanner-2" --item-separator=" " --geometry=200$pos --list \
--multiple --column="Select Interfaces" | cut -f1 -d'|' | tr '\n' ' '`
ret=$?
[[ $ret -eq 1 ]] && exit 0
export inters
Q=1
for i in  $inters
do
   echo  "int${Q}=${i}" >> /tmp/interfaces
   Q=$((Q + 1))
done
}
export -f wlanfn
wlanfn
. /tmp/interfaces
function autocon (){
cp -f /root/.wifi-connect/profiles/"${diss}"+"${address}" $HOME/.wifi-connect/profiles/default
echo "#!/bin/sh
iface $int inet $dhl3
pre-up wpa_supplicant -i$int -D$driver -c$HOME/.wifi-connect/profiles/default
${dhl1} $int
" > /etc/xdg/Startup/autocon
chmod 755 /etc/xdg/Startup/autocon
exit
}
export -f autocon

menufn () {        #generate tray menu
. /tmp/interfaces
if [[ -z "$int" ]]; then
   int=$int1
fi
[[ -f /tmp/wifitray ]] && /tmp/wifitray
pics3=/usr/share/icons/oxygen/22x22/devices
pics2=/usr/share/pixmaps/puppy
pics=/usr/share/pixmaps
scanres=`iwconfig $int | grep --invert-match 'bytes'`
first="Wifi-Scanner-2!killall Wifi-Scanner-2!$pics/wirelessx.png|AutoConnect!bash -c autocon!$pics/wifi224.png|"
second="Stop Auto!rm -f /etc/xdg/Startup/autocon|Saved Profiles!bash -c profilesfn!$pics/wireless.png|"
third="WPA_CLI!bash -c wpaclifn|Browser!defaultbrowser!$pics/browser24.png|"
fourth="Re-connect Wifi!Wifi-Scanner-2 wlan0-u!$pics/wifi124.png|Disconnect Wifi!Wifi-Scanner-2 wlan0-d!$pics/wifi124x.png|"
fifth="+Scanner!Wifi-Scanner-2!$pics/wifi224.png|Stop Scanner!killall Wifi-Scanner-2!$pics/wifi224.png|"
sixth="Ethernet connect!Wifi-Scanner-2 eth0-u!$pics3/network-wired.png|"
seventh="Ethernet disconnect!Wifi-Scanner-2 eth0-d!/usr/share/icons/oxygen/22x22/devices/network-wiredx.png|"
eigth="Ethernet 2 connect!Wifi-Scanner-2 eth1-u!$pics3/network-wired.png|"
ninth="Ethernet 2 disconnect!Wifi-Scanner-2 eth1-d!/usr/share/icons/oxygen/22x22/devices/network-wiredx.png|"
tenth="Connect mobile!Wifi-Scanner-2 usb0-u!/usr/share/icons/oxygen/22x22/devices/phone.png|"
eleventh="Disconnect mobile!Wifi-Scanner-2 usb0-d!/usr/share/icons/oxygen/22x22/devices/phonex.png|"
twelfth="Close Tray!/tmp/wifitray!/usr/share/icons/oxygen/22x22/actions/application-exit.png"
yad --notification --kill-parent \
    --image=/usr/share/pixmaps/wifiS.png --text="$address:
    $scanres" \
    --command="bash -c on_scanclicks" \
    --menu="$first$second$third$fourth$fifth$sixth$seventh$eigth$ninth$tenth$eleventh$twelfth" &
mpid=$!
task='kill $mpid'
echo "#!/bin/sh
mpid=$mpid
$task
" > /tmp/wifitray
chmod 755 /tmp/wifitray
}
export -f menufn
menufn &
startinfn () {      #prepares interfaces
for nit in ${inters}
do
yad --geometry=$pos --undecorated --no-buttons --skip-taskbar --text "Resetting $nit" --timeout 2 &
CURRENTIPS=$(ip addr show | grep $nit | grep inet | awk '{print $2}')
ifconfig $nit up
ifconfig $nit down
for I in ${CURRENTIPS}; do ip addr del $I dev ${nit}; done 2> /dev/null
ip route flush dev $nit 2> /dev/null
killall -9 ${dhl3} 2> /dev/null
killall -9 wpa_supplicant 2> /dev/null
rm -f /var/db/${dhl3}-${nit}.lease 2> /dev/null
rm -f /var/lib/${dhl3}/${dhl3}-${nit}.lease 2> /dev/null
rm -f /var/run/wpa_supplicant/$nit 2> /dev/null
rfkill unblock $nit 2>/dev/null
ifconfig $nit up 2> /dev/null
done
}
export -f startinfn
bash -c startinfn
startinfn2 () {    #prepares connection interface
   if [[ ! -z "$card" ]]; then
      int="$card"
   fi
   yad --geometry=$pos --undecorated --no-buttons --skip-taskbar --text "Resetting $int" --timeout 2 &
   CURRENTIPS=$(ip addr show | grep $int | grep inet | awk '{print $2}')
   ifconfig $int up
   ifconfig $int down
   for I in $CURRENTIPS; do ip addr del $I dev $int; done 2> /dev/null
   ip route flush dev $int 2> /dev/null
   rm -f /var/db/${dhl3}-${int}.lease 2> /dev/null
   rm -f /var/lib/${dhl3}/${dhl3}-${int}.lease 2> /dev/null
   rm -f /var/run/wpa_supplicant/$int 2> /dev/null
   rfkill unblock $int 2>/dev/null
   if [[ ! -z "$mde" ]]; then
      iwconfig $int mode $mde
   fi
   ifconfig $int up 2> /dev/null
}
export -f startinfn2
uhohfn () {           #checks connection profile has data
uhoh=`cat /tmp/netsel`
if [[ -z "$uhoh" ]]; then
   rm -f /tmp/netsel
   $0
   exit 0
fi
}
export -f uhohfn
passwordfn () {     #password username wpasupplicant driver and wifi mode eg Managed/Master/Wired/
   if [[ ! -z "$ONN" ]]; then
      pstx="Password Required:H"
      xtsp="User-ID"
   else
      pstx="Non-Password AP:RO"
      xtsp="User-ID:RO"
   fi
mcad=`ifconfig $int | grep -w HWaddr | awk '{printf $5}'`
userdata=`yad --geometry=$pos --window-icon=/usr/share/pixmaps/wifi24S.png \
--image="/usr/share/pixmaps/wifiS.png" --title "User login for ${nets1}" \
--item-separator=',' --form --field="$pstx" "" --field="$xtsp" "" \
--field="Driver Options":CBE "wext,nl80211,wired" \
--field="Mode":CBE "managed,master,monitor,ad-hoc,normal" \
--field="MacEditor" "$mcad" --field="Random Mac":CHK "TRUE" --timeout=40`
ret=$?
[[ $ret -gt 0 ]] && exec $0 && exit
password=`echo $userdata | cut -f1 -d '|'`
usernm=`echo $userdata | cut -f2 -d '|'`
driver=`echo $userdata | cut -f3 -d '|'`
mde=`echo $userdata | cut -f4 -d '|'`
CMD=`echo $userdata | cut -f5 -d '|'`
RM=`echo $userdata | cut -f6 -d '|'`
export driver
export mde
export usernm
export password
export CMD
}
export -f passwordfn
ocfn () {        #profile templates and save profile
if [[ ! -z "$usernm" && ! -z "$password" ]]; then  #username and password,\
 two phase authentication aka eduroam
   echo "ctrl_interface=/var/run/wpa_supplicant
ap_scan=1
update_config=1

network={
   ssid=$nets1
   bssid=$address
   identity='$usernm'
   password='$password'
   key_mgmt=WPA-EAP
   eap=PEAP
   ca_cert='$HOME/.wifi-connect/ca.pem'
   #phase1='peaplabel=0'
   phase2='auth=MSCHAPV2'
}
" | tr "'" '"' > /tmp/netsel
elif [[ -z "$password" && -z "$usernm" ]]; then #open AP template
   echo "ctrl_interface=/var/run/wpa_supplicant
ap_scan=1
update_config=1

network={
   ssid=$nets1
   bssid=$address
   key_mgmt=NONE
}
" | tr "'" '"' > /tmp/netsel
elif [[ -z "$usernm" && ! -z "$password" ]]; then #Password only normal AP
nets1=`echo "$nets1" | cut -f2 -d'"'`
wpa_passphrase "$nets1" "$password" > /tmp/netsel
fi
cat /tmp/netsel > /root/.wifi-connect/profiles/"${nets1}"+"${address}"
chmod 600 /root/.wifi-connect/profiles/"${nets1}"+"${address}"
}
export -f ocfn
function on_scanclicks () {      #connection information from click on tray icon
OLDIFCONFIG=`ifconfig | grep -e 'ESSID' -e 'bytes' -e 'bssid'|  grep \
--invert-match '\x00' | rev | cut -f1 -d ':' | rev | tr " " "#"`
echo -en "$OLDIFCONFIG" > /tmp/onscanclicks
TITLE="$(cat /tmp/onscanclicks 2> /dev/null)"
yad --on-top  --geometry "40x20-10-35" --text "$TITLE" --undecorated \
--no-buttons --skip-taskbar --timeout 3 &
}
export -f on_scanclicks
connectfn () {                #actual connection function
[[ ! -e /tmp/netsel ]] && bash -c ocfn
if [[ ! -z "${nets1}" ]]; then
      bash -c startinfn2
      [[ ! -z "$mde" ]] && iwconfig $int mode $mde
      if [[ "$RM" = TRUE ]]; then
         ifconfig $int down
         bash -c ranmacfn
         ifconfig $int hw ether $CMD #apply temporary mac address
         ifconfig $int up
      fi
      if [[ -z "$driver" ]]; then
         fif="-Dwext"
      else
         fif="-D$driver"
      fi
      wpa_supplicant "$fif" -i$int -c/tmp/netsel -B  #This is the line that connects to AP
   dep=`busybox which ifplugstatus`
   if [ -z "$dep" ]; then
      yad --geometry=$pos --undecorated --no-buttons --skip-taskbar --top \
      --text "ifplugstatus NOT found. After connecting please install it." --timeout 4
   else
      while true
      do
         state=`ifplugstatus $int | grep -e 'link beat detected'` #check if connected
         if [[ ! -z "$state" ]]; then
            break
         fi
         sleep 1
      done
   fi
yad --geometry=$pos --undecorated --no-buttons --skip-taskbar --top-left \
 --text " Requesting ip address for\n '$nets1' " --timeout 3 &
${dhl1} $int
   while true
   do
      ipad=`ifconfig | grep inet | sed 's/inet addr/ip/g' | grep \
      --invert-match inet6 | grep Bcast | sed 's/         //'` #check for ip address
      if [[ ! -z "$ipad" ]]; then
         yad --geometry=$pos --undecorated --no-buttons --skip-taskbar \
         --top-left --text "Ip address obtained '$ipad'" --timeout 2
         break
      fi
      sleep 1
   done
fi
[[ -f /tmp/wifitray ]] && /tmp/wifitray 2>/dev/null
bash -c menufn &
if [[ "${nets1}" = BTWifi-with-FON ]]; then #if AP is btwifi this automates login
   bt &
fi
}
export -f connectfn

profilesfn () {

prode (){
profiles=`ls $HOME/.wifi-connect/profiles | tr "\n" "|" | tr ' ' '#'`
[[ -f /tmp/profs ]] && rm /tmp/profs
y=1
for zx in "$profiles"
do
echo "prof${y}='${zx}'" >> /tmp/profs
y=$((y + 1))
done
. /tmp/profs
stin=`ls /sys/class/net | sed 's/ //g' | grep --invert-match 'lo' | \
grep --invert-match 'enp' | grep --invert-match 'eth' | tr '\n' '|'`
poof=`cat /tmp/profs | tr '#' ' ' | sed "s/|'//g" | sed "s/='/=/g" | cut -f2 -d'='`
apply=$(ls $HOME/.wifi-connect/profiles | yad --geometry=$pos \
--window-icon=/usr/share/pixmaps/wireless.png \
--title "Saved Profiles: $dhclu $dhcld" \
--window-icon="/usr/share/pixmaps/wireless.png" --geometry=100x60 \
--item-separator="|" --form --field="Select Profile":CB "$poof" \
--field="DeleteProfile:CHK" "" --field="Autoconnect":CHK \
--field="Interface":CBE "" --field="Preview":CHK "$stin")
ret=$?
[[ "$ret" -eq 1 ]] && exit 0
nest=`echo "$apply" | cut -f1 -d'|'`
delp=`echo "$apply" | cut -f2 -d'|'`
auto=`echo "$apply" | cut -f3 -d'|'`
tni=`echo "$appply" | cut -f4 -d'|'`
prev=`echo "$apply" | cut -f5 -d'|'`
export auto
export nest
export tni
if [[ "$auto" = TRUE ]]; then
cp -f /root/.wifi-connect/profiles/"$nest" $HOME/.wifi-connect/profiles/default
echo "#!/bin/sh
iface $tni inet $dhl3
pre-up wpa_supplicant -i$tni -D$driver -c$HOME/.wifi-connect/profiles/default
${dhl1} $tni
" > /etc/xdg/Startup/autocon
chmod 755 /etc/xdg/Startup/autocon
fi
if [[ "$prev" = TRUE ]]; then
 pv=`cat /root/.wifi-connect/profiles/"$nest"`
 yad --width=400 --window-icon=/usr/share/pixmaps/wireless.png \
 --form --field="Preview":TXT "$pv" &
   prode
fi
if [[ "$delp" = TRUE ]]; then
   rm -f $HOME/.wifi-connect/profiles/"$nest"
   prode
fi
if [ -z "$nest" ]; then
   exit 0
fi
}
prode
[[ -f /tmp/profiscan ]] && rm -f /tmp/profiscan
iwlist $tni scan | grep "ESSID" >> /tmp/profiscan
ne=`echo "$nest" | cut -f2 -d'"'`
nets1=`ls $HOME/.wifi-connect/profiles/ | grep "${ne}"`
XZX=`grep "${ne}" /tmp/profiscan`
if [[ ! -z "$XZX" ]]; then
   yad --undecorated --no-buttons --skip-taskbar --top \
   --text "Network $nest is available" --timeout 2 &
   pid=$!
   cp -f $HOME/.wifi-connect/profiles/"${nest}" /tmp/netsel
   int=$tni
   export int
   export ne
   export nets1
   connectfn &
else
   yad --undecorated --no-buttons --skip-taskbar \
   --text "Network $ne was not found by $tni. Try another profile!" --timeout 2
   bash -c profilesfn
fi

}
export -f profilesfn
[[ -f /tmp/wifitray ]] && /tmp/wifitray 2>/dev/null
menufn &
   #main loop for scanning
while [[ -z "$pyp" ]]; do
if [[ "$1" = admin ]]; then
   if [[ ! -z "$int" ]]; then
   #filter out connected wireless card
      inters=`ls /sys/class/net | sed 's/ //g' | grep \
      --invert-match 'lo' | grep --invert-match 'usb' | grep \
      --invert-match 'eth' | grep --invert-match "$int" | grep \
      --invert-match "ap" | tr ' ' '\n'`
   else
   #find all connected wireless cards
      inters=`ls /sys/class/net | sed 's/ //g' | grep \
      --invert-match 'lo' | grep --invert-match 'usb' | grep \
      --invert-match 'eth' | grep --invert-match "ap" | tr ' ' '\n'`
   fi
export inters
   Q=1
   for i in $inters
   do
      rfkill unblock $i
      ifconfig $i down
      [[ ! -z "$mde" ]] && iwconfig $i mode $mde
      ifconfig $i up
      sleep 1
      echo "int$Q=$i" >> /tmp/interfaces
      Q=$((Q + 1))
   done
fi
while true
do
   if [[ ! -z "$int" ]]; then
      inters=`echo ${inters} | tr ' ' '\n' | grep --invert-match "$int"`
      if [[ -z "${inters}" ]]; then
         exit
      else
         inters=`echo ${inters} | tr ' ' '\n' | grep --invert-match "$int"`
      fi
   else
      inters="${inters}"
   fi
   [[ -e /tmp/interfaces ]] && rm /tmp/interfaces
   Q=1
   for i in  $inters
   do
      CURRENTIPS=$(ip addr show | grep $i | grep inet | awk '{print $2}')
      ifconfig $i down
      [[ ! -z "$2" ]] && iwconfig ${i} mode ${2}
      for Ik in $CURRENTIPS; do ip addr del ${Ik} dev ${i}; done 2> /dev/null
      ip route flush dev ${i} 2> /dev/null
      rm -f /var/db/dhcpcd-${i}.lease 2> /dev/null
      rm -f /var/lib/dhcpcd/dhcpcd-${i}.lease 2> /dev/null
      rm -f /var/db/udhcpc-${i}.lease 2> /dev/null
      rm -f /var/lib/udhcpc/udhcpc-${i}.lease 2> /dev/null
      rm -f /var/run/wpa_supplicant/${i} 2>/dev/null
      rfkill unblock ${i} 2>/dev/null
      ifconfig ${i} up 2>/dev/null
      echo  "int${Q}=${i}" >> /tmp/interfaces #creates list of interfaces
      Q=$((Q + 1))
   done
   #filter out connected interface from scanning
   if [[ ! -z "${int}" ]]; then
      grep --invert-match "${int}" /tmp/interfaces > /tmp/interface
      . /tmp/interface
   else
      . /tmp/interfaces
   fi
      for i in $inters
      do
      ifconfig ${int}$i down
      bash -c ranmacfn
      ifconfig ${int}$i hw ether ${CMD} #apply temporary mac address for scan
      ifconfig ${int}$i up
      done

   #label tabs by interface: label on gui per interface
   if [[ ! -z "${int1}"  ]]; then
      these1="--tab=${int1}"
   else
      these1=""
   fi
   if [[ ! -z "${int2}" ]]; then
      these2="--tab=${int2}"
   else
      these2=""
   fi
   if [[ ! -z "${int3}" ]]; then
      these3="--tab=${int3}"
   else
      these3=""
   fi
   if [[ ! -z "${int4}" ]]; then
      these4="--tab=${int4}"
   else
      these4=""
   fi
   if [[ ! -z "${int5}" ]]; then
      these5="--tab=${int5}"
   else
      these5=""
   fi
   if [[ ! -z "${int6}" ]]; then
      these6="--tab=${int6}"
   else
      these6=""
   fi
   if [[ ! -z "${int7}" ]]; then
      these7="--tab=${int7}"
   else
      these7=""
   fi
   if [[ ! -z "${int8}" ]]; then
      these8="--tab=${int8}"
   else
      these8=""
   fi
   #these9="--tab=Options"
   #make used interface tab empty
   if [[ ! -z "$int" ]]; then
   case $int in
      ${int1}) these1="";;
      ${int2}) these2="";;
      ${int3}) these3="";;
      ${int4}) these4="";;
      ${int5}) these5="";;
      ${int6}) these6="";;
      ${int7}) these7="";;
      ${int8}) these8="";;
   esac
   fi
   #accumulate tabs into one variable
   these="${these1} ${these2} ${these3} ${these4} ${these5} ${these6} ${these7} ${these8}"
   [[ /tmp/net ]] && rm -f /tmp/net    # remove previously selected network output

   #scan on available interfaces
   [[ ! -z "${int1}" ]] && ecann1=`iwlist ${int1} scan | grep -e 'ESSID' \
   -e 'Quality' -e 'Address' -e 'key' | sed -e 's/  \+/ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
   sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
   sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac | \
   yad --plug=$$ --tabnum=1 --list --separator=';' --editable --column="${int1} found these APs" \
   --column=Password --column=Quality----Level --column=MAC --column=${int1} --hide-column=5 | \
   cut -f1,2,4 -d';' | sed 's/;/+/g' | sed "s/$/+${int1}+/" > /tmp/net &`
   [[ ! -z "${int2}" ]] && ecann2=`iwlist ${int2} scan | grep -e 'ESSID' \
   -e 'Quality' -e 'Address' -e 'key' | sed -e's/  */ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
   sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
   sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac | \
   yad --plug=$$ --tabnum=2 --list --separator=';' --editable --column="${int2} found these APs" \
   --column=Password --column=Quality----Level --column=MAC --column=${int2} --hide-column=5 | \
   cut -f1,2,4 -d';' | sed 's/; /+/g' | sed "s/$/+${int2}+/" > /tmp/net &`
   [[ ! -z "${int3}" ]] && ecann3=`iwlist ${int3} scan | grep -e 'ESSID' \
   -e 'Quality' -e 'Address' -e 'key' | sed -e's/  */ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
   sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
   sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac | \
   yad --plug=$$ --tabnum=3 --list --separator=';' --editable --column="${int3} found these APs" \
   --column=Password --column=Quality----Level --column=MAC --column=${int3} --hide-column=5 | \
   cut -f1,2,4 -d';' | sed 's/;/+/g' | sed "s/$/+${int3}+/" > /tmp/net &`
   [[ ! -z "${int4}" ]] && ecann4=`iwlist ${int4} scan | grep -e 'ESSID' \
   -e 'Quality' -e 'Address' -e 'key' | sed -e's/  */ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
   sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
   sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac | \
   yad --plug=$$ --tabnum=4 --list --separator=';' --editable --column="${int4} found these APs" \
   --column=Password --column=Quality----Level --column=MAC --column=${int4} --hide-column=5 | \
   cut -f1,2,4 -d';' | sed 's/;/+/g' | sed "s/$/+${int4}+/" > /tmp/net &`
   [[ ! -z "${int5}" ]] && ecann5=`iwlist ${int5} scan | grep -e 'ESSID' \
   -e 'Quality' -e 'Address' -e 'key' | sed -e's/  */ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
   sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
   sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac | \
   yad --plug=$$ --tabnum=5 --list --separator=';' --editable --column="${int5} found these APs" \
   --column=Password --column=Quality----Level --column=MAC --column=${int5} --hide-column=5 |
   cut -f1,2,4 -d';' | sed 's/;/+/g' | sed "s/$/+${int5}+/" > /tmp/net &`
   [[ ! -z "${int6}" ]] && ecann6=`iwlist ${int6} scan | grep -e 'ESSID' \
   -e 'Quality' -e 'Address' -e 'key' | sed -e's/  */ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
   sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
   sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac | \
   yad --plug=$$ --tabnum=6 --list --separator=';' --editable --column="${int6} found these APs" \
   --column=Password --column=Quality----Level --column=MAC --column=${int6} --hide-column=5 | \
   cut -f1,2,4 -d';' | sed 's/;/+/g' | sed "s/$/+${int6}+/" > /tmp/net &`
   [[ ! -z "${int7}" ]] && ecann7=`iwlist ${int7} scan | grep -e 'ESSID' \
   -e 'Quality' -e 'Address' -e 'key' | sed -e's/  */ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
   sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
   sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac | \
   yad --plug=$$ --tabnum=7 --list --separator=';' --editable --column="${int7} found these APs" \
   --column=Password --column=Quality----Level --column=MAC --column=${int7} --hide-column=5 | \
   cut -f1,2,4 -d';' | sed 's/;/+/g' | sed "s/$/+${int7}+/" > /tmp/net &`
   [[ ! -z "${int8}" ]] && ecann8=`iwlist ${int8} scan | grep -e 'ESSID' \
   -e 'Quality' -e 'Address' -e 'key' | sed -e's/  */ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
   sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
   sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac | \
   yad --plug=$$ --tabnum=8 --list --separator=';' --editable --column="${int8} found these APs" \
   --column=Password --column=Quality----Level --column=MAC --column=${int8} --hide-column=5 | \
   cut -f1,2,4 -d';' | sed 's/;/+/g' | sed "s/$/+${int8}+/" > /tmp/net &`
   #notebook tabbed gui
   [[ ! -z "${these1}" ]] && yad --window-icon=/usr/share/pixmaps/leafpad.png \
   --geometry=700x280+$((XX - 700))+$((YY - 320)) --on-top --notebook --key=$$ \
   --title="Wifi-Scanner-2: Multi-Interface-Wifi" --tab-pos=bottom $these --timeout=$secs
      ret=$?
   [[ $ret -eq 1 ]] && rm -f /tmp/{netsel,netself,net*,interface} && exit 0 #exit on cancel
      empty=`cat /tmp/net` #check if selection made
    #parse selected network datafields
     if [[ ! -z "$empty" ]]; then
      nets1=`cat /tmp/net | cut -f1 -d'+' | tr '""' '"'`
      address=`cat /tmp/net | cut -f3 -d'+'`
      int=`cat /tmp/net | cut -f4 -d'+'`
           ONN=`grep -e '~on~' /tmp/net` # check ap security is on
           export address
           export int
           export nets1
           export ONN
           passwordfn
           if [[ ! -z "$ONN" ]]; then
             if [[ ! -z "$ONN" && -z "$password" ]]; then
               yad --geometry=300$pos --undecorated --skip-taskbar --no-buttons \
               --text="AP needs a password: so NOT connecting" \
               --timeout=2 #continue script if no password supplied
               unset int
             else
               connectfn &
             fi
         elif [[ -z "$ONN" ]]; then
            connectfn &
            sleep 1
         fi
     elif [[ -z "$empty" && -e /tmp/netsel && ! -z "$nets1" ]]; then
         connectfn &
     fi
     #unset variables ready for next loop iteration
    for p in int1 int2 int3 int4 int5 int6 int7 int8 these1 these2 these3 \
    these4 these5 these6 these7 these8 tni nest ne
    do
      unset $p
    done
   done
done


contents of /usr/share/udhcpc/default.script needed for udhcpc

Code:
#!/bin/sh
# udhcpc script edited by Tim Riker <Tim@Rikers.org>

RESOLV_CONF="/etc/resolv.conf"

[ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }

NETMASK=""
[ -n "$subnet" ] && NETMASK="netmask $subnet"
BROADCAST="broadcast +"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"

case "$1" in
   deconfig)
      echo "Setting IP address 0.0.0.0 on $interface"
            yad --no-buttons --text="Setting IP address 0.0.0.0 on $interface" --timeout=1

      ifconfig $interface 0.0.0.0
      ;;

   renew|bound)
      echo "Setting IP address $ip on $interface"
         yad --no-buttons --text="Setting IP address $ip on $interface" --timeout=1

      ifconfig $interface $ip $NETMASK $BROADCAST

      if [ -n "$router" ] ; then
         echo "Deleting routers"
         yad --no-buttons --text="Deleting routers" --timeout=1

         while route del default gw 0.0.0.0 dev $interface ; do
            :
         done

         metric=0
         for i in $router ; do
            echo "Adding router $i"
            yad --no-buttons --text="Adding router $i" --timeout=1
            route add default gw $i dev $interface metric $metric
            : $(( metric += 1 ))
         done
      fi

      echo "Recreating $RESOLV_CONF"
      yad --no-buttons --text="Recreating $RESOLV_CONF" --timeout=2
      # If the file is a symlink somewhere (like /etc/resolv.conf
      # pointing to /run/resolv.conf), make sure things work.
      realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")
      tmpfile="$realconf-$$"
      > "$tmpfile"
      [ -n "$domain" ] && echo "search $domain" >> "$tmpfile"
      for i in $dns ; do
         echo " Adding DNS server $i"
         yad --no-buttons --text=" Adding DNS server $i" --timeout=1
         echo "nameserver $i" >> "$tmpfile"
      done
      mv "$tmpfile" "$realconf"
      ;;
esac

exit 0
Back to top
View user's profile Send private message MSN Messenger 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1392
Location: Japan

PostPosted: Today, at 07:58    Post subject:  

stemsee wrote:
Sed statements have also been reduced.
Not much. All these piped seds cost you performance.

Instead of 12 seds per scan ...
Code:
[[ ! -z "${int1}" ]] && ecann1=`iwlist ${int1} scan | grep -e 'ESSID' \
-e 'Quality' -e 'Address' -e 'key' | sed -e 's/  \+/ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac
.
.
.
[[ ! -z "${int2}" ]] && ecann2=`iwlist ${int2} scan | grep -e 'ESSID' \
-e 'Quality' -e 'Address' -e 'key' | sed -e's/  */ /g' | sed 's/Quality=/  /g' | sed 's/level=/    /g' | sed 's/ESSID://g' | \
sed 's/key:on/~on~/g' | sed 's/key:off/~off~/g' | sed 's/Encryption//g' | sed 's/ Signal//g' | \
sed 's/- Address: / /g' | sed 's|Cell ...|\n|g' | sed 's/ - //g' | sed "s/^[ \t]*//" | tac


you need only 1 sed statement. If you put the pattern into a variable you also don't need to repeat the pattern for each scan:
Code:
PATTERN='
s/^.*Address: ([0-9A-Z:]*)/\n\1/p
s/^.*Quality=([^ ]*).*Signal level=(.*)/\1 \2/p
s/^.*key:([onf]*)/~\1~/p
s/^.*ESSID://p'

[[ $int1 ]] && ecann1=`iwlist ${int1} scan | sed -rn "$PATTERN" | tac
.
.
.
[[ $int2 ]] && ecann2=`iwlist ${int2} scan | sed -rn "$PATTERN" | tac
Also note that [[ ! -z "${int1}" ]] is the same as [[ -n "${int1}" ]] and [[ "${int1}" ]] and [[ "$int1" ]] and [[ $int1 ]] ..take your pick.


With your previous sample data the result looks like this:
Code:
"BTWifi-X"
~on~
35/70 -75 dBm 
52:8A:AE:8C:5E:7E

"BTWifi-with-FON"
~off~
31/70 -79 dBm 
32:8A:AE:8C:5E:7E
Back to top
View user's profile Send private message 
stemsee

Joined: 27 Jun 2013
Posts: 1998
Location: In The Way

PostPosted: Today, at 10:12    Post subject:  

Thank you! Very Happy
Back to top
View user's profile Send private message MSN Messenger 
Display posts from previous:   Sort by:   
Page 2 of 2 [30 Posts]   Goto page: Previous 1, 2
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
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
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1078s ][ Queries: 12 (0.0031s) ][ GZIP on ]