{"id":444,"date":"2011-01-28T00:30:38","date_gmt":"2011-11-16T21:51:50","guid":{"rendered":"http:\/\/kraftek.com\/blog\/script-to-connect-to-a-wifi-ap-if-we-got-a-key-in-our-database-use-it\/"},"modified":"2011-01-28T00:30:38","modified_gmt":"2011-11-16T21:51:50","slug":"script-to-connect-to-a-wifi-ap-if-we-got-a-key-in-our-database-use-it","status":"publish","type":"post","link":"\/blog\/index.php\/script-to-connect-to-a-wifi-ap-if-we-got-a-key-in-our-database-use-it\/","title":{"rendered":"Script to connect to a wifi AP, if we got a key in our database, use it"},"content":{"rendered":"<p>We got many keys for some wifi APs around, so we put them in a delimited text file like:<br \/>\nap8599:5537801570<br \/>\nsd6980:5202140314<br \/>\nwq0858:5953230520<br \/>\nnow, when we are around, we want a script that we can tell: &#8220;connect to ap6980 if you see it around, if you<br \/>\nneed a key, take it from the text file&#8221;. Called without arguments, it scans all available networks, and tries to<br \/>\nconnect to all of the ones that we have a key or that are open.<br \/>\nThis is our connection script:<br \/>\n#!\/bin\/bash<br \/>\nap=$@<br \/>\ni=wlan0<br \/>\nen=0<br \/>\n#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>\n# Scan for wireless networks, and pretty print the quality, the essid, and if we require encryption<br \/>\n#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nscanw () {<br \/>\ni=$1<br \/>\n( ifconfig $i up<br \/>\niwlist $i scan | egrep -i &#8216;essid|freq|qual|encr&#8217; |nawk &#8216;ORS=NR%4?&#8221; &#8220;:&#8221;n&#8221;&#8216;| tr -s &#8216; &#8216;| while read l;do<br \/>\nAP=`echo &#8220;$l &#8220;|awk &#8216;{s=substr($0,index($0,&#8221;ESSID:&#8221;) +7);print substr(s,1,index (s,&#8221;&#8221;&#8221;)-1)}&#8217;`<br \/>\nEN=`echo &#8220;$l &#8220;|awk &#8216;{s=substr($0,index($0,&#8221;Encryption key:&#8221;)+15);print substr(s,1,index (s,&#8221; &#8220;)-1)}&#8217;`<br \/>\nQU=`echo &#8220;$l &#8220;|awk &#8216;{s=substr($0,index($0,&#8221;Quality=&#8221;)+8);print substr(s,1,index (s,&#8221;\/&#8221;)-1)}&#8217;`<br \/>\nK=`grep &#8220;^$AP&#8221; keys.txt| awk -F&#8217;:&#8217; &#8216;{ print $2 }&#8217;`<br \/>\necho &#8220;$QU:$EN:$AP:$i: $K&#8221;<br \/>\ndone<br \/>\n) | sort -n<br \/>\n}<br \/>\n#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n# Connect to the accespoint determined by the &#8220;$ap&#8221; variable<br \/>\n#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nconn(){<br \/>\nL=`grep -i &#8220;$ap&#8221; ~\/keys.txt| head -1`<br \/>\nC=`echo -n $L | wc -c`<br \/>\nindb=1<br \/>\nif [ $C -lt 1 ];then<br \/>\n40\/433<br \/>\n# echo &#8220;&#8212;&#8212;&#8212;&#8212;- WARNING: AP $ap not found in database&#8221;<br \/>\nindb=0<br \/>\nfi<br \/>\nS=`grep &#8220;$ap&#8221; \/tmp\/scan.$$|wc -c`<br \/>\nif [ $S -lt 1 ];then<br \/>\necho &#8220;&#8212;&#8212;&#8212;&#8212;- ERROR: AP $ap is not in range, cannot connect&#8221;<br \/>\nexit 1<br \/>\nfi<br \/>\nif [ $indb -eq 1 ];then<br \/>\n#&#8212;if we found an accesspoint in our database work with it<br \/>\nAP=`echo $L | awk -F&#8217;:&#8217; &#8216;{ print $1 }&#8217;`<br \/>\nK=`echo $L | awk -F&#8217;:&#8217; &#8216;{ print $2 }&#8217;`<br \/>\necho &#8220;&#8212;&#8212;&#8212;&#8212;- Connecting to $AP with key $K&#8221;<br \/>\niwconfig $i mode managed key $K essid &#8220;$AP&#8221; rate auto<br \/>\nelse<br \/>\nAP=`grep &#8220;$ap&#8221; \/tmp\/scan.$$|awk -F&#8217;:&#8217; &#8216;{ print $3 }&#8217;`<br \/>\nen=`grep &#8220;$AP&#8221; \/tmp\/scan.$$|awk -F&#8217;:&#8217; &#8216;{ print $2 }&#8217;`<br \/>\nif [ $en == &#8220;off&#8221; ];then<br \/>\necho &#8220;&#8212;&#8212;&#8212;&#8212;- Connecting to $AP without key&#8221;<br \/>\niwconfig $i mode managed key off essid &#8220;$AP&#8221; rate auto<br \/>\nelse<br \/>\necho &#8220;&#8212;&#8212;&#8212;&#8212; ERROR: Encryption needed for $AP but i don&#8217;t have the key&#8221;<br \/>\nreturn 1<br \/>\nfi<br \/>\nfi<br \/>\nx=0<br \/>\necho -n &#8220;&#8212;&#8212;&#8212;&#8212;- Associating with $AP&#8221;<br \/>\nwhile [ $x -lt 10 ];do<br \/>\nA=`iwconfig $i | grep -i &#8216;Not-Associated&#8217;|wc -c`<br \/>\nif [ ${A} -eq 0 ];then<br \/>\necho &#8220;&#8221;;echo &#8220;&#8212;&#8212;&#8212;&#8212;- Associated to $AP !!!&#8221;<br \/>\ndhclient -q -r $i<br \/>\ndhclient $i<br \/>\nexit 0<br \/>\nelse<br \/>\necho -n &#8220;.&#8221;<br \/>\nfi<br \/>\nsleep 1<br \/>\nx=`echo &#8220;$x + 1&#8221; | bc`<br \/>\ndone<br \/>\necho &#8220;&#8221;;echo &#8220;&#8212;&#8212;&#8212;&#8212;- ERROR: Could not associate&#8221;<br \/>\n}<br \/>\n#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# Connect to any accesspoint we can sniff<br \/>\n#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nauto () {<br \/>\ncat \/tmp\/scan.$$ | awk -F&#8217;:&#8217; &#8216;{ print $3 }&#8217; |while read ap;do<br \/>\necho &#8220;&#8212;&#8212;&#8212;- trying to connect to $ap&#8221;<br \/>\nconn $ap<br \/>\ndone<br \/>\n}<br \/>\n#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# Main<br \/>\n41\/433<br \/>\n#&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nifconfig -a | egrep -i &#8216;mon|wlan&#8217; | awk &#8216;{ print $1 }&#8217; |while read i;do airmon-ng stop $i; done &gt; \/dev\/null 2&gt;&amp;1<br \/>\nifconfig $i down<br \/>\nifconfig $i up<br \/>\nscanw $i &gt; \/tmp\/scan.$$<br \/>\ncat \/tmp\/scan.$$<br \/>\nif [ $# -lt 1 ];then<br \/>\necho &#8220;&#8212;&#8212;&#8211; WARNING: no Accesspoint specified, will try to connect to the ones i see alive&#8221;<br \/>\nauto<br \/>\nelse<br \/>\nconn<br \/>\nfi<br \/>\nrm \/tmp\/scan.$$<br \/>\nScan for wireless accesspoints, parse the output<br \/>\nso, iwlist throws its output in different order depending of the wifi nic, so we need to scan and parse, to see 3 columns:<br \/>\nsignal strength: encryption needed: ESSID<br \/>\n27:on:CASA<br \/>\n27:on:AP1133<br \/>\n30:on:CC5763<br \/>\n32:on:XX1330<br \/>\n36:off:gg54g<br \/>\nSo, we wrote a script called &#8220;scan&#8221; that loops to all wlan interfaces and scans for APs.<br \/>\n#!\/bin\/bash<br \/>\nifconfig -a |grep -i wlan | awk &#8216;{ print $1 }&#8217;|while read i;do<br \/>\necho &#8220;#&#8212;- scanning on $i&#8221;<br \/>\n( ifconfig $i up<br \/>\niwlist $i scan | egrep -i &#8216;essid|freq|qual|encr&#8217; |nawk &#8216;ORS=NR%4?&#8221; &#8220;:&#8221;n&#8221;&#8216;| tr -s &#8216; &#8216;| while read l;do<br \/>\nAP=`echo &#8220;$l &#8220;|awk &#8216;{s=substr($0,index($0,&#8221;ESSID:&#8221;) +7);print substr(s,1,index (s,&#8221;&#8221;&#8221;)-1)}&#8217;`<br \/>\nEN=`echo &#8220;$l &#8220;|awk &#8216;{s=substr($0,index($0,&#8221;Encryption key:&#8221;)+15);print substr(s,1,index (s,&#8221; &#8220;)-1)}&#8217;`<br \/>\nQU=`echo &#8220;$l &#8220;|awk &#8216;{s=substr($0,index($0,&#8221;Quality=&#8221;)+8);print substr(s,1,index (s,&#8221;\/&#8221;)-1)}&#8217;`<br \/>\necho &#8220;$QU:$EN:$AP&#8221;<br \/>\ndone<br \/>\n) | sort -n<br \/>\ndone<br \/>\n42\/433<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We got many keys for some wifi APs around, so we put them in a delimited text file like: ap8599:5537801570 sd6980:5202140314 wq0858:5953230520 now, when we are around, we want a script that we can tell: &#8220;connect to ap6980 if you see it around, if you need a key, take it from the text file&#8221;. Called [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-444","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=444"}],"version-history":[{"count":0,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/posts\/444\/revisions"}],"wp:attachment":[{"href":"\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}