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 Thu 14 Dec 2017, 04:24
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 1 of 3 [33 Posts]   Goto page: 1, 2, 3 Next
Author Message
stemsee

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

PostPosted: Sat 25 Mar 2017, 14:25    Post subject:  Multi-Wifi-Interface scan and connect "Wifi-Scanner-2"
Subject description: SOLVED!
 

Multi-Interface Wifi Connector and Scanner.

Can use upto 8 wireless interfaces in tabbed windows. Select AP from any tab to connect to it. Continue scanning on other interfaces.

Supports WPA2-PSK, PEAP, OPEN network configurations as I have not encountered WPA/WEP recently so I didn't add support but help yourself.

Creates saved profiles according to ssid and bssid.

Code:
#!/bin/sh
# multi-interface wifi scanner and connection app
# copyright (c) 2017 Marcos Contant
#
#
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))"

if [[ "$1" = admin ]]; then
   secs=6
else
   secs=30
fi

interfacefn () {
for wit in `ls /sys/class/net`
do
   case $wit in
      eth0|enp2s0f2) eth=${wit};;
      eth1) eths=${wit};;
      enp0s20u3|usb0) usb0=${wit};;
   esac
done
}

depsfn () { # check dependencies
   dhclu=`busybox which udhcpc`
   dhclu=`echo $dhclu | rev | cut -f1 -d'/' | rev`
   dhcld=`busybox which dhcpcd`
   dhcld=`echo $dhcld | rev | cut -f1 -d'/' | rev`
   if [ -z "$dhcld" ]; then
      dhl1="udhcpc -A 8 -p /var/run/udhcpc.pid -s /etc/udhcpc.script -i"
      dhl2="/etc/udhcpc.script deconfig"
   elif [[ ! -z "$dhcld" ]]; then
      dhl1="dhcpcd"
      dhl3="dhcpcd -z"
      dhl2="dhcpcd -k"
   elif [[ -z "$dhclu" && -z "$dhcld" ]]; then
      zenity --info --text "$dhclu and $dhcld not found"
      echo "$dhclu and $dhcld not found"
   fi
}
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" = wlan0-d ]]; then
   ifconfig $int down
   ${dhl2} $int
   exit 0
fi
if [[ "$1" = wlan0-u ]]; then
   ifconfig $int up
   ${dhl3} $int
   exit 0
fi
if [[ "$1" = wlan1-d ]]; then
   wlanfn
   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

# unst 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 0 1 2 3 4 5 6 7 8
do
   if [[ -e /dev/wlan$i ]]; then
      ifconfig wlan$i down
      iproute flush dev wlan$i
      ifconfig wlan$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
echo "$inters"
Q=1
for i in  $inters
do
   echo  "int${Q}=${i}" >> /tmp/interfaces
   Q=$((Q + 1))
done
}
wlanfn

menufn () {    #generate tray menu
[[ -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'`

yad --notification --kill-parent \
    --image=/usr/share/pixmaps/wifiS.png --text="$address:
    $scanres" \
    --command="bash -c on_scanclicks" --menu="Wifi-Scanner-2!killall Wifi-Scanner-2!$pics/wirelessx.png|New-Connection!Wifi-Connect!$pics/wifi224.png|Saved Profiles!Wifi-Profiles!$pics/wireless.png|Browser!defaultbrowser!$pics/browser24.png|Re-connect Wifi!Wifi-Scanner-2 wlan0-u!$pics/wifi124.png|Disconnect Wifi!Wifi-Scanner-2 wlan0-d!$pics/wifi124x.png|+Scanner!Wifi-Scanner-2!$pics/wifi224.png|Stop Scanner!killall Wifi-Scanner-2!$pics/wifi224.png|Ethernet connect!Wifi-Scanner-2 eth0-u!$pics3/network-wired.png|Ethernet disconnect!Wifi-Scanner-2 eth0-d!/usr/share/icons/oxygen/22x22/devices/network-wiredx.png|Ethernet 2 connect!Wifi-Scanner-2 eth1-u!$pics3/network-wired.png|Ethernet 2 disconnect!Wifi-Scanner-2 eth1-d!/usr/share/icons/oxygen/22x22/devices/network-wiredx.png|Connect mobile!Wifi-Scanner-2 usb0-u!/usr/share/icons/oxygen/22x22/devices/phone.png|Disconnect mobile!Wifi-Scanner-2 usb0-d!/usr/share/icons/oxygen/22x22/devices/phonex.png|Close Tray!/tmp/wifitray!/usr/share/icons/oxygen/22x22/actions/application-exit.png" &
mpid=$!
task='kill $mpid'
echo "#!/bin/sh
mpid=$mpid
$task
" > /tmp/wifitray
chmod 755 /tmp/wifitray
}

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 ${dhl1} 2> /dev/null
killall -9 wpa_supplicant 2> /dev/null
rm -f /var/db/dhcpcd-${nit}.lease 2> /dev/null
rm -f /var/lib/dhcpcd/dhcpcd-${nit}.lease 2> /dev/null
rm -f /var/db/udhcpc-${nit}.lease 2> /dev/null
rm -f /var/lib/udhcpc/udhcpc-${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
}
startinfn

startinfn2 () {    #prepares connection interface
for tin in $int1 $int2 $int3 $int4 $int5 $int6 $int7 $int8
do
   yad --geometry=$pos --undecorated --no-buttons --skip-taskbar --text "Resetting $tin" --timeout 2 &
   CURRENTIPS=$(ip addr show | grep $tin | grep inet | awk '{print $2}')
   ifconfig $tin up
   ifconfig $tin down
   for I in $CURRENTIPS; do ip addr del $I dev $tin; done 2> /dev/null
   ip route flush dev $tin 2> /dev/null
   rm -f /var/db/dhcpcd-${tin}.lease 2> /dev/null
   rm -f /var/lib/dhcpcd/dhcpcd-${tin}.lease 2> /dev/null
   rm -f /var/db/udhcpc-${tin}.lease 2> /dev/null
   rm -f /var/lib/udhcpc/udhcpc-${tin}.lease 2> /dev/null
   rm -f /var/run/wpa_supplicant/$tin 2> /dev/null
   rfkill unblock $tin 2>/dev/null
   ifconfig $tin up 2> /dev/null
done
}

uhohfn () {    #checks connection profile has data
uhoh=`cat /tmp/netsel`
if [[ -z "$uhoh" ]]; then
   rm -f /tmp/netsel
   $0
   exit 0
fi
}

passwordfn () {     #password username
userdata=`yad --geometry=$pos --window-icon=/usr/share/pixmaps/wifi24S.png --image="/usr/share/pixmaps/wifiS.png" --title "User login for ${netsel1}" --form --field=Password:H --field=User-id`
password=`echo $userdata | cut -f1 -d '|'`
usernm=`echo $userdata | cut -f2 -d '|'`
}

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=$netsel1
   bssid=$address
   identity='$usernm'
   password='$password'
   key_mgmt=WPA-EAP
   eap=PEAP
   #ca_cert='/path/to/certificate'
   #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=$netsel1
   bssid=$address
   key_mgmt=NONE
}
" | tr "'" '"' > /tmp/netsel
elif [[ -z "$usernm" && ! -z "$password" ]]; then #Password only normal AP
netsel1=`echo "$netsel1" | cut -f2 -d'"'`
wpa_passphrase "$netsel1" "$password" > /tmp/netsel
fi
diss=`grep 'ssid="' /tmp/netsel | cut -f2 -d'"'`
if [[ /root/.wifi-connect/profiles/"${diss}" ]]; then
   cat /tmp/netsel > /root/.wifi-connect/profiles/"${diss}"+"${address}"
else
   cat /tmp/netsel > /root/.wifi-connect/profiles/"${diss}"+"${address}"
fi

}

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 --selectable-labels --geometry "40x20-10-35" --text "$TITLE" --undecorated --no-buttons --skip-taskbar --timeout 4 &
}
export -f on_scanclicks

connectfn () {                #actual connection function
[[ -f /tmp/wifitray ]] && /tmp/wifitray 2>/dev/null
ocfn
if [[ ! -z "${netsel1}" ]]; then
      startinfn
      ranmacfn
      ranmacfn
      ifconfig $int down
      ifconfig $int hw ether $CMD #apply temporary mac address
      ifconfig $int up
      sleep 1
      if [[ -z "$usernm" && ! -z "$password" ]]; then
         driver=""
      else
         driver="-D wext"
      fi
      wpa_supplicant "$driver" -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
      done
   fi
yad --geometry=$pos --undecorated --no-buttons --skip-taskbar --top-left --text " Requesting ip address for\n '$netsel1' " --timeout 3 &
${dhl3} $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
   done
fi
menufn &
if [[ "${netsel1}" = BTWifi-with-FON ]]; then #if AP is btwifi this automates login
   bt &
fi

}
[[ -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
   echo "$inters"
   Q=1
   for i in $inters
   do
      rfkill unblock $i
      ifconfig $i down
      [[ ! -z "$2" ]] && iwconfig $i mode $2
      ifconfig $i up
      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
         wlanfn
      fi
   else
      inters=${inters}
   fi
   
   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
      ranmacfn
      ifconfig ${int} down
      ifconfig ${int} hw ether ${CMD} #apply temporary mac address
      ifconfig ${int} up
   #make sure associate interface variables
   int1=${int1}
   int2=${int2}
   int3=${int3}
   int4=${int4}
   int5=${int5}
   int6=${int6}
   int7=${int7}
   int8=${int8}
   
   #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 's/          //g' | sed 's/   / /g' | sed 's/   / /g' | sed '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/  //'  | sed 's/ - //g' | 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 's/          //g' | sed 's/   / /g' | sed 's/   / /g' | sed '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/  //'  | sed 's/ - //g' | 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 's/          //g' | sed 's/   / /g' | sed 's/   / /g' | sed '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/  //'  | sed 's/ - //g' | 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 's/          //g' | sed 's/   / /g' | sed 's/   / /g' | sed '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/  //'  | sed 's/ - //g' | 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 's/          //g' | sed 's/   / /g' | sed 's/   / /g' | sed '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/  //'  | sed 's/ - //g' | 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 's/          //g' | sed 's/   / /g' | sed 's/   / /g' | sed '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/  //'  | sed 's/ - //g' | 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 's/          //g' | sed 's/   / /g' | sed 's/   / /g' | sed '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/  //'  | sed 's/ - //g' | 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 's/          //g' | sed 's/   / /g' | sed 's/   / /g' | sed '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/  //'  | sed 's/ - //g' | 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 #eit on cancel
      empty=`cat /tmp/net` #check if selection made
      
    #parse selected network datafields
      if [[ ! -z "$empty" ]]; then
      netsel1=`grep --invert-match 'null' /tmp/net | head -1 | grep '+' | cut -f1 -d'+'`
      address=`cat /tmp/net | cut -f3 -d'+'`
      int=`cat /tmp/net | cut -f4 -d'+'`
         if [[ ! -z "${netsel1}" ]]|[[ ! -z "$address" ]]; then 
         profile=`ls /root/.wifi-connect/profiles | grep -w "${netsel1}+${address}"|"${netsel1}"` # search saved profiles for selected AP
         if [[ ! -z "${profile}" ]]; then
            Wifi-Starter Wifi-Profiles & # start Wifi-Profiles script
            sleep 3
            exec $0
            exit
         fi
           ONN=`grep -e '~on~' /tmp/net` # check ap security is on
           if [ ! -z "$ONN" ]; then
            passwordfn
             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 2
         fi
         fi
     fi
           #unset variables ready for next loop iteration
    for i in int1 int2 int3 int4 int5 int6 int7 int8 these1 these2 these3 these4 these5 these6 these7 these8
    do
      unset $i
    done
   done
done
xscreenshot-20170325T182812.png
 Description   
 Filesize   46.19 KB
 Viewed   557 Time(s)

xscreenshot-20170325T182812.png


Last edited by stemsee on Thu 26 Oct 2017, 09:48; edited 5 times in total
Back to top
View user's profile Send private message MSN Messenger 
6502coder


Joined: 23 Mar 2009
Posts: 410
Location: Western United States

PostPosted: Sat 25 Mar 2017, 14:50    Post subject:  

Instead of a whole series of sed's piped together, wouldn't it be more efficient to throw all those sed edits into a single sed script? Just a thought.... Maybe you have a good reason why the entire input stream needs to be passed through each sed edit one at a time.
Back to top
View user's profile Send private message 
stemsee

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

PostPosted: Sat 25 Mar 2017, 17:27    Post subject:  

There's no good reason ... only ignorance!
Back to top
View user's profile Send private message MSN Messenger 
stemsee

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

PostPosted: Sun 26 Mar 2017, 06:02    Post subject:  

I got it working! In a round about way!
Last edited by stemsee on Thu 26 Oct 2017, 09:43; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1398
Location: Japan

PostPosted: Sun 26 Mar 2017, 06:05    Post subject:  

6502coder wrote:
Instead of a whole series of sed's piped together, wouldn't it be more efficient to throw all those sed edits into a single sed script?
Just tried it. No grep and only one sed statement would be almost 10 times more efficient in terms of CPU usage. Also MUCH shorter.

But what is this? Are you trying to comment out code lines programmatically? Won't work
Code:
if [[ ! -z "$int" ]]; then
    case $int in
        $int1) op1=#
               these1="";;
        $int2) op2=#
               these2="";;
    esac
fi
$op1 [[ ! -z "$int1" ]] &&   ecann1=`iwlist $int1 scan | ...
     [[ ! -z "$int1" ]] &&   echo "$ecann1" | ...
$op2 [[ ! -z "$int2" ]] &&   ecann2=`iwlist $int2 scan | ...
     [[ ! -z "$int2" ]] &&   echo "$ecann2" | ...

Let's say $int equals $int1, then you want to comment out the first [[ ! -z "$int1" ]] line? Not possible. May "work" for you as expected, but for a different reason. Bash will throw an error. And what about the following line? Should not work either since it depends on the result of the (faulty) previous line.

Still much room for improvement, but this may replace above code and should work without errors:
Code:
[[ "$int" != "$int1" ]] &&   ecann1=`iwlist $int1 scan | ...
[[ "$int" != "$int1" ]] &&   echo "$ecann1" | ...
[[ "$int" != "$int2" ]] &&   ecann2=`iwlist $int2 scan | ...
[[ "$int" != "$int2" ]] &&   echo "$ecann2" | ...
Back to top
View user's profile Send private message 
stemsee

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

PostPosted: Mon 27 Mar 2017, 05:55    Post subject:  

MochiMoppel wrote:
6502coder wrote:
Instead of a whole series of sed's piped together, wouldn't it be more efficient to throw all those sed edits into a single sed script?
Just tried it. No grep and only one sed statement would be almost 10 times more efficient in terms of CPU usage. Also MUCH shorter.

Where is the improved statement then?

Quote:
But what is this? Are you trying to comment out code lines programmatically? Won't work

Yes, that was my intent. It didn't work also because the int1 variable got assigned a different interface anyway! Only the tabs got reassigned incorrectly with different labels but same interfaces.
Quote:

Code:
if [[ ! -z "$int" ]]; then
    case $int in
        $int1) op1=#
               these1="";;
        $int2) op2=#
               these2="";;
    esac
fi
$op1 [[ ! -z "$int1" ]] &&   ecann1=`iwlist $int1 scan | ...
     [[ ! -z "$int1" ]] &&   echo "$ecann1" | ...
$op2 [[ ! -z "$int2" ]] &&   ecann2=`iwlist $int2 scan | ...
     [[ ! -z "$int2" ]] &&   echo "$ecann2" | ...

Let's say $int equals $int1, then you want to comment out the first [[ ! -z "$int1" ]] line? Not possible. May "work" for you as expected, but for a different reason. Bash will throw an error. And what about the following line? Should not work either since it depends on the result of the (faulty) previous line.

Still much room for improvement, but this may replace above code and should work without errors:
Code:
[[ "$int" != "$int1" ]] &&   ecann1=`iwlist $int1 scan | ...
[[ "$int" != "$int1" ]] &&   echo "$ecann1" | ...
[[ "$int" != "$int2" ]] &&   ecann2=`iwlist $int2 scan | ...
[[ "$int" != "$int2" ]] &&   echo "$ecann2" | ...


I will try this!
Back to top
View user's profile Send private message MSN Messenger 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1398
Location: Japan

PostPosted: Mon 27 Mar 2017, 08:34    Post subject:  

stemsee wrote:
Where is the improved statement then?
On my USB stick.

I said that it's more efficient, I didn't say that it's more reliable. The problem is that when I run iwlist wlan0 scan the output is probably not what it should be and what you tested your code against:
Code:
# iwlist wlan0 scan
wlan0     Scan completed :
          Cell 01 - Address: 74:76:C5:73:30:FA
                    ESSID:"AirPort13306"
                    Mode:Managed
                    Frequency:2.452 GHz (Channel 9)
                    Quality:4/5  Signal level:-59 dBm  Noise level:-92 dBm
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (1) : PSK
                    IE: Unknown: DDB30050F204104A0001101044
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (1) : PSK
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
                              18 Mb/s; 36 Mb/s; 54 Mb/s; 6 Mb/s; 12 Mb/s
                              24 Mb/s; 48 Mb/s

Notice the 'Quality' line. It's 'Quality:' and not 'Quality=' and there are 2 levels. Your code doesn't work here, and so does mine. Unless you provide a sample output of iwlist and state exactly what result you expect after your sed statement it makes no sense for me to post any code. Anyway my sed statement would be different from yours. Instead of deleting all the stuff that I'm not interested in I would match only the stuff that I need and ignore the rest. Makes a shorter statement but needs a bit of regex voodoo. If you are not comfortable with it you'd better try yourself with the tools and ways you can safely manage.
Back to top
View user's profile Send private message 
stemsee

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

PostPosted: Mon 27 Mar 2017, 17:12    Post subject:  

Thanks for that MochiMoppel

Here is a sample output of iwlist scan on my machine

Code:
iwlist wlan0 scan
wlan0     Scan completed :
          Cell 01 - Address: 32:8A:AE:8C:5E:7E
                    Channel:1
                    Frequency:2.412 GHz (Channel 1)
                    Quality=31/70  Signal level=-79 dBm 
                    Encryption key:off
                    ESSID:"BTWifi-with-FON"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=0000010a626c371f
                    Extra: Last beacon: 63ms ago
                    IE: Unknown: 000F4254576966692D776974682D464F4E
                    IE: Unknown: 010882848B960C121824
                    IE: Unknown: 030101
                    IE: Unknown: 0706474220010D14
                    IE: Unknown: 2A0100
                    IE: Unknown: 32043048606C
                    IE: Unknown: 2D1AAC011BFFFF000000000000000000008000000000000000000000
                    IE: Unknown: 3D1601080400000000000000000000000000000000000000
                    IE: Unknown: 4A0E14000A002C01C800140005001900
                    IE: Unknown: 7F080100000000000040
                    IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                    IE: Unknown: DD0900037F01010000FF7F
          Cell 02 - Address: 52:8A:AE:8C:5E:7E
                    Channel:1
                    Frequency:2.412 GHz (Channel 1)
                    Quality=35/70  Signal level=-75 dBm 
                    Encryption key:on
                    ESSID:"BTWifi-X"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=0000010a626bdbb4
                    Extra: Last beacon: 63ms ago
                    IE: Unknown: 00084254576966692D58
                    IE: Unknown: 010882848B960C121824
                    IE: Unknown: 030101
                    IE: Unknown: 0706474220010D14
                    IE: Unknown: 2A0100
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : CCMP TKIP
                        Authentication Suites (1) : 802.1x
                       Preauthentication Supported
                    IE: Unknown: 32043048606C
                    IE: Unknown: 2D1AAC011BFFFF000000000000000000008000000000000000000000
                    IE: Unknown: 3D1601080400000000000000000000000000000000000000
                    IE: Unknown: 4A0E14000A002C01C800140005001900
                    IE: Unknown: 7F080100000000000040
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : CCMP TKIP
                        Authentication Suites (1) : 802.1x
                    IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                    IE: Unknown: DD0900037F01010000FF7F
          Cell 03 - Address: A4:2B:8C:16:E2:B9
                    Channel:1
                    Frequency:2.412 GHz (Channel 1)
                    Quality=41/70  Signal level=-69 dBm 
                    Encryption key:on
                    ESSID:"BTHub5-6HRQ_EXT"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
                              18 Mb/s; 36 Mb/s; 54 Mb/s
                    Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
                    Mode:Master
                    Extra:tsf=000000289c0bad23
                    Extra: Last beacon: 63ms ago
                    IE: Unknown: 000F4254487562352D364852515F455854
                    IE: Unknown: 010882848B961224486C
                    IE: Unknown: 030101
                    IE: Unknown: 2A0100
                    IE: Unknown: 32040C183060
                    IE: Unknown: 2D1AEC0117FFFF000000000000000000000000000000000000000000
                    IE: Unknown: 3D1601000400000000000000000000000000000000000000
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
                    IE: Unknown: 0B05020016127A
                    IE: Unknown: 7F0101
                    IE: Unknown: DD07000C4303000000
                    IE: Unknown: 0706444520010D10
                    IE: Unknown:
longline [....]
          Cell 04 - Address: 00:8A:AE:8C:5E:7E
                    Channel:1
                    Frequency:2.412 GHz (Channel 1)
                    Quality=29/70  Signal level=-81 dBm 
                    Encryption key:on
                    ESSID:"BTHub5-7PK2"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s
                    Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                    Mode:Master
                    Extra:tsf=0000010a626afd65
                    Extra: Last beacon: 63ms ago
                    IE: Unknown: 000B4254487562352D37504B32
                    IE: Unknown: 010882848B960C121824
                    IE: Unknown: 030101
                    IE: Unknown: 0706474220010D14
                    IE: Unknown: 2A0100
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    IE: Unknown: 32043048606C
                    IE: Unknown: 2D1AAC011BFFFF000000000000000000008000000000000000000000
                    IE: Unknown: 3D1601080400000000000000000000000000000000000000
                    IE: Unknown: 4A0E14000A002C01C800140005001900
                    IE: Unknown: 7F080100000000000040
                    IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
                    IE: Unknown: DD0900037F01010000FF7F
                    IE: Unknown:
long line [....]


All I need to glean from the info is , Mac Address **:**:**:**:**:**; ESSID:"SomeName"; Encryption key: on or off; Signal Strength 29/70 Quality: -87 dBm (possibly frequency 2.4ghz Channel: 1)

for each AP found, from each card. Only need the data not the labels! IFS=" " e.g.
Code:
"BTWifi-with-Fon" **:**:**:**:** on 29/70 -87 2.4 1


cheers
stemsee

Last edited by stemsee on Thu 30 Mar 2017, 17:32; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger 
stemsee

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

PostPosted: Mon 27 Mar 2017, 19:22    Post subject:  

So I came up with code to list selected interfaces, then when connected, remove connected from that list for the continued scan loop using the other interfaces. Mostly working as well as any other connection tool I have used. Also admin mode, CLI, usage can change card mode and also update interfaces each loop, facilitating plug'n'play on the fly! Tested using once pci card and three usb cards; BCM (pci), ralink, Atheros, Ralink on a laptop.

Code:
Wifi-Connect-2 admin managed


Can select one or many interfaces!

Can use dhcpcd or udhcpc.

Connects with both ESSID and Mac address. Stores profiles likewise.

To do: change selection output to individual files to enable multi AP selections and connect each interface to one of its found APs, simultaneously! (Because why not!)

Also add harvesting mode to simply store AP by ESSID and MAC from each card.

Last edited by stemsee on Thu 26 Oct 2017, 09:46; edited 2 times in total
Back to top
View user's profile Send private message MSN Messenger 
stemsee

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

PostPosted: Wed 29 Mar 2017, 16:24    Post subject:  

Question

Let us suppose I connected 6 wifi cards on one pc.

Each card connected by long aerial cable to different sides of the building.

All connected to different APs.

How then could the user select which 'connection' to pipe traffic through? Assuming the user is running FatDog64-710.

I want to be able to switch which active internal connection to use, by script.
Back to top
View user's profile Send private message MSN Messenger 
stemsee

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

PostPosted: Thu 30 Mar 2017, 04:36    Post subject:  

So I found some info that pointed me to the 'metric' ranking. Together with appropriate changes for the DNS, Gateway settings. Should be easy to script.
Back to top
View user's profile Send private message MSN Messenger 
stemsee

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

PostPosted: Thu 30 Mar 2017, 04:42    Post subject:  

@MochiMoppel

I think that you might be the one to know a way to have the gui update the info from subsequent scans without having to close the gui to refresh. Is that possible with yad --listen? If so or if you have an idea for some other way would be great. Smile
Back to top
View user's profile Send private message MSN Messenger 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1398
Location: Japan

PostPosted: Thu 30 Mar 2017, 07:45    Post subject:  

@stemsee
Please edit your sample output and remove the 2 extremely long lines. This forum page has become unreadable for me.
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1398
Location: Japan

PostPosted: Sat 01 Apr 2017, 22:10    Post subject:  

Thanks for editing your post.

stemsee wrote:
...have the gui update the info from subsequent scans without having to close the gui to refresh. Is that possible with yad --listen?
More than possible. It's the purpose of this option Wink
Back to top
View user's profile Send private message 
slavvo67

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

PostPosted: Thu 08 Jun 2017, 17:25    Post subject:  

Forgive my ignorance but why more than one wifi interface?
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 3 [33 Posts]   Goto page: 1, 2, 3 Next
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.1253s ][ Queries: 14 (0.0056s) ][ GZIP on ]