分享

手把手教你通过串口调试模块连接wifi

 xiaofenglib 2013-09-16
首先你要下载两个软件:
  1. sudo apt-get install vim dialog
复制代码
然后编写一个脚本:
  1. sudo vim wifi.sh
复制代码
脚本内容如下:
  1. #!/bin/sh

  2. # From wlanconfig:
  3. # $FreeBSD: src/usr.sbin/bsdinstall/scripts/wlanconfig,v 1.3.2.1 $

  4. #
  5. # Author: gihnius@gmail.com
  6. #

  7. # default wlan
  8. iface=wlan0
  9. # driver device
  10. dev=$(sysctl -n net.wlan.0.%parent)
  11. if ifconfig $dev >/dev/null 2>&1 ; then
  12.     phy_iface=$dev
  13. else
  14.     phy_iface=ath0
  15. fi
  16. # use system wpa_supplicant.conf
  17. wpa_supplicant_conf=/etc/wpa_supplicant.conf
  18. # network id -1 non exist
  19. current_id=-1
  20. # use default wpa_supplicant.conf
  21. use_conf=1

  22. usage () {
  23.     cat <<EOF
  24. Usage:
  25. $0               default
  26. $0 -l/list       use saved configured network, do not scan. So make sure the AP exist.
  27. $0 stop          stop wifi connection
  28. $0 reconfig      re-configure device
  29. $0 newconf       create new /etc/wpa_supplicant.conf
  30. $0 addif         if not set rc.conf wlan configure; means does not exist $iface currently.
  31. $0 ns 127.0.0.1  set this nameserver
  32. $0 help/-h/--help
  33. EOF
  34.     exit 0
  35. }

  36. check_network () {
  37.     local _ssid=$1
  38.     local _id=`wpa_cli -i $iface list_networks | awk '$2~/^'"$_ssid"'$/{print $1}' | head -1`
  39.     if [ -z $_id ] ; then
  40.         echo -1
  41.     else
  42.         echo $_id
  43.     fi
  44. }

  45. update_psk () {
  46.     local _ssid=$1
  47.     local _psk=$2
  48.     if [ ${#_psk} -lt 8 ] ; then
  49.         echo psk length max be 8..63
  50.         exit 1
  51.     fi
  52.     local _id=`check_network "$_ssid"`
  53.     local _pass=`wpa_passphrase $_ssid $_psk | sed -n 's/[^#]psk=\(.*\)/\1/p'`
  54.     wpa_cli -i $iface set_network $_id psk $_pass
  55. }

  56. stop_wifi () {
  57.     ifconfig $iface down delete
  58.     exit 0
  59. }

  60. cfg_dev () {
  61.     ifconfig $iface create wlandev $phy_iface
  62.     ifconfig $iface up
  63.     /etc/rc.d/wpa_supplicant start $iface
  64. }

  65. recfg_dev () {
  66.     ifconfig $iface down delete
  67.     ifconfig $iface destroy
  68.     ifconfig $phy_iface down
  69.     ifconfig $phy_iface up
  70.     ifconfig $iface create wlandev $phy_iface
  71.     ifconfig $iface up
  72. }

  73. wpa_lookup () {
  74. # Try to reach wpa_supplicant. If it isn't running and we can modify the
  75. # existing system, start it. Otherwise, fail.
  76. # Here use the existing system script /etc/rc.d/wpa_supplicant
  77.     (wpa_cli -i $iface ping >/dev/null 2>/dev/null ||        /etc/rc.d/wpa_supplicant start $iface) || \
  78.             (dialog --backtitle "FreeBSD Wifi" --title "Error" --msgbox \
  79.             "Could not start wpa_supplicant!" 0 0; exit 1) || exit 1

  80. # See if we succeeded
  81.     wpa_cli -i $iface ping >/dev/null 2>/dev/null
  82.     if [ $? -ne 0 ] ; then
  83.             dialog --backtitle "FreeBSD Wifi" --title "Error" --msgbox \
  84.                 "Wireless cannot be configured without making changes to the local system!" \ 0 0
  85.             exit 1
  86.     fi
  87. }

  88. final () {
  89.     local nw=$1
  90.     local nid=$2
  91.     wpa_cli -i $iface disconnect
  92.     ifconfig $iface ssid $nw
  93.     wpa_cli -i $iface select_network $nid
  94.     wpa_cli -i $iface enable_network $nid
  95.     wpa_cli -i $iface reconnect
  96.     wpa_cli -i $iface save_config
  97.     dhclient -b $iface
  98.     exit 0
  99. }


  100. case $1 in
  101.     stop)
  102.         stop_wifi ;;
  103.     -l|list)
  104.         wpa_lookup
  105.         wpa_cli -i $iface list
  106.         echo "Please enter the network id:"
  107.         read current_id
  108.         if [ $current_id -lt 0 -o $current_id -ge 64 ] ; then
  109.             # too small or too big try again
  110.             wpa_cli -i $iface list
  111.             echo "Please enter the network id:"
  112.             read current_id
  113.         fi
  114.         NETWORK=`wpa_cli -i $iface list | awk '{if($1=='"$current_id"'){print $2}}'`
  115.         final $NETWORK $current_id
  116.         ;;
  117.     reconfig)
  118.         recfg_dev ;;
  119.     newconf)
  120.         use_conf=0 ;;
  121.     addif)
  122.         cfg_dev ;;
  123.     ns)
  124.         if [ ! -z $2 ] ; then
  125.             if ! grep -q $2 /etc/resolv.conf ; then
  126.                 echo $2 >> /etc/resolv.conf
  127.             fi
  128.         fi
  129.         ;;
  130.     -h|--help|help)
  131.         usage ;;
  132. esac

  133. use_wificfg () {
  134.     if [ -f $wpa_supplicant_conf ] ; then
  135.         if [ $use_conf = 1 ] ; then
  136.             # using wificfg
  137.             return 0
  138.         else
  139.             echo "Backup $wpa_supplicant_conf ..."
  140.             mv $wpa_supplicant_conf ${wpa_supplicant_conf}.old
  141.             return 1
  142.         fi
  143.     else
  144.         # can use wificfg
  145.         return 1
  146.     fi
  147. }

  148. if use_wificfg ; then
  149.     echo "Using $wpa_supplicant_conf ."
  150. else
  151.     echo -n > $wpa_supplicant_conf
  152.     chmod 0600 $wpa_supplicant_conf
  153.     echo "ctrl_interface=/var/run/wpa_supplicant" >> $wpa_supplicant_conf
  154.     echo "eapol_version=2" >> $wpa_supplicant_conf
  155.     echo "fast_reauth=1" >> $wpa_supplicant_conf
  156.     echo "update_config=1" >> $wpa_supplicant_conf
  157.     echo >> $wpa_supplicant_conf
  158. fi

  159. ## progress ...

  160. wpa_lookup
  161. wpa_cli -i $iface ap_scan 1
  162. wpa_cli -i $iface scan
  163. dialog --backtitle "FreeBSD Wifi" --title "Scanning" --ok-label "Skip" \
  164.         --pause "Waiting 5 seconds to scan for wireless networks..." \
  165.         9 40 5 || exit 1

  166. SCAN_RESULTS=`wpa_cli -i $iface scan_results`
  167. NETWORKS=`echo "$SCAN_RESULTS" | awk -F '\t' \
  168.     '/..:..:..:..:..:../ {if (length($5) > 0) printf("\"%s\"\t%s\n", $5, $4);}' |
  169.     sort | uniq`

  170. if [ -z "$NETWORKS" ] ; then
  171.         dialog --backtitle "FreeBSD Wifi" --title "Error" \
  172.             --yesno "No wireless networks were found. Rescan?" 0 0 && \
  173.             exec $0 $@
  174.         exit 1
  175. fi

  176. exec 3>&1
  177. NETWORK=`sh -c "dialog --extra-button --extra-label \"Rescan\" \
  178.     --backtitle \"FreeBSD Wifi\" --title \"Network Selection\" --menu \
  179.     \"Select a wireless network to connect to.\" 0 0 0 \
  180.     $(echo $NETWORKS | tr '\n' ' ')" 2>&1 1>&3`
  181. case $? in
  182.     0)        # OK
  183.             ;;
  184.     1)        # Cancel
  185.             exit 1
  186.             ;;
  187.     3)        # Rescan
  188.             exec $0 $@
  189.             ;;
  190. esac
  191. exec 3>&-

  192. current_id=`check_network "$NETWORK"`
  193. if [ $current_id -ge 0 ] ; then
  194.     sh -c "dialog --backtitle \"FreeBSD Wifi\" --title \"Connect to Network\" --yesno \"Connect to configured network: $NETWORK ?\" 0 0"
  195.     if [ $? -eq 0 ] ; then
  196.         final $NETWORK $current_id
  197.     fi
  198. fi

  199. ENCRYPTION=`echo "$NETWORKS" | awk -F '\t' \
  200.     "/^\"$NETWORK\"\t/ {printf(\"%s\n\", \\\$2 );}"`

  201. if echo $ENCRYPTION | grep -q 'PSK' ; then
  202.         exec 3>&1
  203.         PASS=`dialog --insecure --backtitle "FreeBSD Wifi" \
  204.             --title "WPA Setup" --mixedform "" 0 0 0 \
  205.                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
  206.                 "Password" 2 0 "" 2 12 15 63 1 \
  207.                 2>&1 1>&3` \
  208.             || exec $0 $@
  209.         exec 3>&-
  210.     if [ $current_id -ge 0 ] ; then
  211.         update_psk "$NETWORK" "$PASS"
  212.     else
  213.         current_id=`wpa_cli -i $iface add_network | tail -1`
  214.         wpa_cli -i $iface set_network $current_id ssid "\"$NETWORK\""
  215.         update_psk "$NETWORK" "$PASS"
  216.     fi
  217. elif echo $ENCRYPTION | grep -q WEP ; then
  218.         exec 3>&1
  219.         WEPKEY=`dialog --insecure --backtitle "FreeBSD Wifi" \
  220.             --title "WEP Setup" --mixedform "" 0 0 0 \
  221.                 "SSID" 1 0 "$NETWORK" 1 12 0 0 2 \
  222.                 "WEP Key 0" 2 0 "" 2 12 15 0 1 \
  223.                 2>&1 1>&3` \
  224.             || exec $0 $@
  225.     if [ $current_id -ge 0 ] ; then
  226.         wpa_cli -i $iface set_network $current_id wep_key0 "\"$WEPKEY\""
  227.     else
  228.         current_id=`wpa_cli -i $iface add_network | tail -1`
  229.         wpa_cli -i $iface set_network $current_id ssid "\"$NETWORK\""
  230.         wpa_cli -i $iface set_network $current_id key_mgmt "NONE"
  231.         wpa_cli -i $iface set_network $current_id wep_tx_keyidx 0
  232.         wpa_cli -i $iface set_network $current_id wep_key0 "\"$WEPKEY\""
  233.     fi
  234. else        # Open
  235.     if [ $current_id -ge 0 ] ; then
  236.         echo "Use Open Network $NETWORK"
  237.     else
  238.         current_id=`wpa_cli -i $iface add_network | tail -1`
  239.         wpa_cli -i $iface set_network $current_id ssid "\"$NETWORK\""
  240.         wpa_cli -i $iface set_network $current_id key_mgmt "NONE"
  241.     fi
  242. fi

  243. final $NETWORK $current_id
复制代码
最后给它一个执行权限
  1. sudo chmod +x wifi.sh
复制代码
执行
  1. ./wifi.sh
复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多