website

My personal website
git clone git://git.ckyln.com/website
Log | Files | Refs

20200828-wpa-add-script.html (5138B)


      1 <!DOCTYPE HTML>
      2 <html lan=en>
      3   <head>
      4     <title>wpa_add script | Cem's Website</title>
      5     <meta charset="utf-8">
      6     <meta name="Description" content="Cem Keylan's Website">
      7     <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      8     <style>
      9       html {font-family:monospace;font-size:16px;color:#282a36;}
     10       body {
     11 	  width: 90%;
     12 	  max-width: 1050px;
     13 	  margin-left: auto;
     14 	  margin-right: auto;
     15 	  margin-top: 20px;
     16 	  overflow: none;
     17           overflow-y: scroll;
     18 	  padding-right: 10px;
     19 	  padding-left: 10px;
     20       }
     21       a{text-decoration:none;font-weight:bold;color:#282a36;}
     22       a:hover{text-decoration:underline;}
     23       @media (prefers-color-scheme: dark) {
     24           html {color: white;background:#282a36;}
     25           a{color:white;}
     26       }
     27     </style>
     28     <link rel="stylesheet" href="/static/syntax.css">
     29     <script src="/static/highlight.pack.js"></script>
     30     <script>hljs.initHighlightingOnLoad();</script>
     31   </head>
     32   <body>
     33     <div class="header">
     34       <nav>
     35         <a href='/'>index</a> |
     36         <a href="/software.html">software</a> |
     37         <a href="/blog.html">blog</a> |
     38         <a href="/contact.html">contact</a> |
     39         <a href="/sysmgr">sysmgr</a> |
     40       </nav>
     41     </div>
     42     <hr>
     43     <p>
     44 <h1>wpa_add script</h1>
     45 
     46 <p>I have this script named <code>wpa_add</code>, which I use to easily add new WiFi when I
     47 am outside, possibly in a cafe. I have written this script because I don&rsquo;t like
     48 the way my girlfriend looks at me while thinking that I am an absolute moron for
     49 not using Windows 10, and the entirety of Linux is a circlejerk. It is only
     50 natural that she thinks this way. I use my own distribution that doesn&rsquo;t have
     51 things like <code>dbus</code>, or <code>NetworkManager</code>, or one of those common desktop
     52 environments. You could install it by creating a simple package, but I am happy
     53 to not have any of those in my system.</p>
     54 
     55 <p>This script uses wpa-supplicant to add a new network and reconfigure. It uses
     56 dmenu for input, however you could replace dmenu calls with some command line
     57 prompts. I am doing the following assumptions:
     58 - You can manipulate <code>wpa_supplicant</code> without root access.
     59 - The configuration is on <code>/etc/wpa_supplicant.conf</code>.
     60 - You can edit <code>/etc/wpa/supplicant.conf</code>.</p>
     61 
     62 <p>If you want to ensure the above just do the following (as root):</p>
     63 
     64 <pre><code class="sh"># Add yourself to the wheel group if you aren't already.
     65 adduser user wheel
     66 
     67 # Change the ownership of /etc/wpa_supplicant.conf
     68 chown root:wheel /etc/wpa_supplicant.conf
     69 
     70 # Make sure the configuration can be edited by the wheel group.
     71 chmod 664 /etc/wpa_supplicant.conf
     72 </code></pre>
     73 
     74 <p>Your <code>wpa_supplicant</code> configuration must include the following line (or something similar):</p>
     75 
     76 <pre><code class="plaintext">ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
     77 </code></pre>
     78 
     79 <p>Here is the script</p>
     80 
     81 <pre><code class="sh">#!/bin/sh
     82 # Script to add wpa_supplicant networks through dmenu
     83 
     84 if [ "$1" ]; then
     85     name=$1
     86 else
     87     name=$(dmenu -p "Please enter network name, leave empty if you want to search" &lt;&amp;-)
     88 fi
     89 
     90 [ "$name" ] || {
     91     wpa_cli scan
     92     name=$(
     93     wpa_cli scan_results | sed 1,2d | while read -r _ _ _ _ ssid _; do
     94         # Hidden wifi are not to be returned
     95         [ "$ssid" ] || continue
     96         echo "$ssid"
     97     done | sort -u | dmenu -l 10 -p "Please choose WiFi")
     98     [ "$name" ] || exit 1
     99 }
    100 
    101 pass=$(dmenu -P -p "Please enter your password, leave empty if the network has open access.")
    102 
    103 if [ "$pass" ]; then
    104     wpa_passphrase "$name" &lt;&lt;EOF&gt;&gt; /etc/wpa_supplicant.conf
    105 $pass
    106 EOF
    107 else
    108     printf 'network={\n\tssid="%s"\n\tkey_mgmt=NONE\n\tpriority=-999\n}\n' "$name" &gt;&gt; /etc/wpa_supplicant.conf
    109 fi
    110 
    111 wpa_cli reconfigure
    112 </code></pre>
    113 
    114 <p>As I have said, you could do something similar in a command-line-only tool as
    115 well. This one uses <code>fzf</code> on WiFi selection.</p>
    116 
    117 <pre><code class="sh">#!/bin/sh -e
    118 
    119 stty="$(stty -g)"
    120 trap "stty $stty" EXIT INT TERM HUP
    121 
    122 if [ "$1" ]; then
    123     name=$1
    124 else
    125     printf 'Network Name, leave empty if you want to search: '
    126     read -r name
    127 fi
    128 
    129 [ "$name" ] || {
    130     wpa_cli scan &gt;/dev/null
    131     name=$(
    132     wpa_cli scan_results | sed 1,2d | while read -r _ _ _ _ ssid _; do
    133         # Hidden wifi are not to be returned
    134         [ "$ssid" ] || continue
    135         echo "$ssid"
    136     done | sort -u | fzf --prompt "Please choose WiFi: ")
    137 }
    138 
    139 [ "$name" ] || exit 1
    140 
    141 stty -echo
    142 printf 'Please enter your password, leave empty if the network has open access.\nPassword: '
    143 read -r pass
    144 
    145 if [ "$pass" ]; then
    146     wpa_passphrase "$name" &lt;&lt;EOF&gt;&gt; /etc/wpa_supplicant.conf
    147 $pass
    148 EOF
    149 else
    150     printf 'network={\n\tssid="%s"\n\tkey_mgmt=NONE\n\tpriority=-999\n}\n' "$name" &gt;&gt; /etc/wpa_supplicant.conf
    151 fi
    152 
    153 wpa_cli reconfigure
    154 </code></pre>
    155 
    156 <p>These scripts can be found as a gist <a href="https://git.io/JULL6">here</a></p>
    157     </p>
    158     <a href="/blog/20200828-wpa-add-script.txt">This page in plain-text</a>
    159     <hr>
    160     <p class=footer>Copyright &copy; 2019-2021 Cem Keylan</p>
    161   </body>
    162 </html>