tuxmartin

wifi scan (parser)

Nov 12th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # # iwlist wlan0 scan | bash wifi.sh
  4.  
  5. # http://stackoverflow.com/questions/25394772/parsing-iwlist-scan-using-bash
  6.  
  7. ## print header lines
  8. #echo ""
  9. #echo " mac essid frq chn qual lvl enc ie protocol bitRates groupCipher authenticationSuites"
  10.  
  11.  
  12. cat <<EOF
  13. {
  14. "wifi" : [
  15. EOF
  16.  
  17. while IFS= read -r line; do
  18.  
  19. ## test line contenst and parse as required
  20. [[ "$line" =~ Address ]] && mac=${line##*ss: }
  21. [[ "$line" =~ 'Bit Rates' ]] && bitRates=${line##*Bit Rates:}
  22. [[ "$line" =~ 'Group Cipher' ]] && groupCipher=${line##*Group Cipher :}
  23. [[ "$line" =~ 'Authentication Suites' ]] && authenticationSuites=${line##*Authentication Suites (*) :}
  24. [[ "$line" =~ Protocol ]] && protocol=${line##*Protocol:}
  25. [[ "$line" =~ IE: ]] && ie=${line##*IE: }
  26. [[ "$line" =~ \(Channel ]] && { chn=${line##*nel }; chn=${chn:0:$((${#chn}-1))}; }
  27. [[ "$line" =~ Frequen ]] && { frq=${line##*ncy:}; frq=${frq%% *}; }
  28. [[ "$line" =~ Quality ]] && {
  29. qual=${line##*ity=}
  30. qual=${qual%% *}
  31. lvl=${line##*evel=}
  32. lvl=${lvl%% *}
  33. }
  34. [[ "$line" =~ Encrypt ]] && enc=${line##*key:}
  35. [[ "$line" =~ ESSID ]] && {
  36. essid=${line##*ID:}
  37.  
  38. #echo " $mac | $essid | $frq | $chn | $qual | $lvl | $enc | $ie | $protocol | $bitRates | $groupCipher | $authenticationSuites" # output after ESSID
  39.  
  40. cat <<EOF
  41.  
  42. {
  43. "ssid" : $essid,
  44. "bssid" : "$mac",
  45. "freq" : $frq,
  46. "channel" : $chn ,
  47. "signal_level" : "$lvl",
  48. "quality" : "$qual",
  49. "enc" : "$enc",
  50. "ie" : "$ie",
  51. "protocol" : "$protocol",
  52. "bit_rates" : "$bitRates",
  53. "group_cipher" : "$groupCipher",
  54. "authenticationSuites" : "$authenticationSuites"
  55. },
  56.  
  57. EOF
  58.  
  59.  
  60. }
  61.  
  62. done
  63.  
  64. echo " ] }"
Advertisement
Add Comment
Please, Sign In to add comment