draft0 - a shared blog by just some people

Menu

Entries tagged 'cat:Code' (Page 2)

Bash script to quickly open and close a wifi hotspot

I needed a wifi hotspot to test some phones. So I looked up how to create one quickly and learned about nmcli. It wasn't quick and easy enough for my taste. So here is how I do it from now on.

The Bash script maintains only one connection (named quick-hotspot). You can't create multiple connections or access points with it. Not intentionally, but also not accidentally because you forgot that you still have a hotspot enabled from the last time you needed one. (I know I would at some point.)

Examples:


hotspot # Creates and enables a hotspot with a random name and a random 8-digit password.
hotspot down # Disables the hotspot if it was enabled.
hotspot ⊿ⴼͳΞⵖⵡ∃ doctorwho # Creates and enables a hotspot with the SSID "⊿ⴼͳΞⵖⵡ∃" and the password "doctorwho".
hotspot . password # My use case: quick and short and I don't want to type a complicated password on the phones. SSID: ".", PW: "password"
hotspot down # Deactivates the hotspot regardless of the SSID you used.

If you activate a hotspot while one is already active, it just changes the SSID and password so that no two connections created by this script are ever active at the same time. I'm sure there already is a program that solves this better. But it was fun for me to create and I felt like sharing.

Click here to download the bash script.

Content:

#!/bin/bash

# Quickly create and activate a WiFi hotspot - optionally defining SSID and password - and deactivate it again when you don't need it anymore.

usage() {
  echo "Usage: $(basename $0) ssid passphrase"
  echo "       $(basename $0) down"
  echo "       $(basename $0)"
  exit 1
}

activate() {
  # If the connection "quick-hotspot" doesn't already exist...
  if ! nmcli connection show | grep quick-hotspot; then
    # ... then create it.
    nmcli connection add type wifi ifname '*' con-name quick-hotspot autoconnect no ssid "$ssid"
  else
    # ... else change the SSID of the existing connection.
    nmcli connection modify quick-hotspot ssid "$ssid"
  fi
  # Make the connection an access point
  nmcli connection modify quick-hotspot 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared
  # Change the password
  nmcli connection modify quick-hotspot 802-11-wireless-security.key-mgmt wpa-psk 802-11-wireless-security.psk "$pw"
  # Enable the connection
  nmcli connection up quick-hotspot
}

case $# in
  0)
    ssid=$(</dev/urandom tr -dc A-Z] | head -c8; echo "")
    echo "SSID: $ssid"
    pw=$(shuf -i 10000000-99999999 -n 1) 
    echo "PW: $pw"
    activate
    ;;
  1)
    if [ $1 == down ]; then
      nmcli connection down "quick-hotspot"
      exit 0
    else usage
    fi
    ;;
  2)
    ssid="$1"
    if ! [ ${#2} -ge 8 ]; then
      echo "$(basename $0): Password needs to be 8 characters or longer."
      exit 1
    else
      pw="$2"
      activate
    fi
    ;;
  *)
    usage
    ;;
esac

See also: My Reddit Post

Comment via email

SBWG 0.8.9

Lately I'm making only slow progress with SBWG. I'm determined to make this something that I'm comfortable with sharing as 1.0.0 online. Something that works and could arguably be called a somewhat finished peace of software. That doesn't mean that the todo list won't be huge at the point I change the version number to 1.0.0. But it should be in a relatively good state for what I've decided it will encompass at version 1.0.0. "Relatively" because it's still also a learning project and I would write many things differently if I started over, which means I'll have to rewrite them at some point to be satisfied with SBWG.

So, the goal and roadmap is set for v0.9.0 and v1.0.0 and a large part of what is still to be done is documentation and other things I don't enjoy doing as much as starting to implement new features. I'm holding back on working on things that are not on that roadmap so that I don't introduce new bugs and the need of further testing. First I want to improve what SBWG can already do.

That means that there won't be many interesting changes in the coming weeks (or maybe even months). Today I've decided to call what I've currently got version 0.8.9 and publish it on the project page.

Comment via email

SBWG update - version 1.0 takes shape slowly

I rarely wrote updates on SBWG before now. I just worked on it whenever I had time and felt like it. And that was quite often. But this is my website where I want to feel fine with pretending that what I'm posting is of any real interest to anybody. So I may post more updates on what I did to approach my goals for SBWG 1.0 from now on. It is rare for me that I invest so much time in a single project. And since I'm starting this blog at the same time and want to fill it with a lot of content, it is time to combine the two.

Some things that I did and changed but didn't report on so far: The header format of entry and page source files has been changed completely (simplified), the generated HTML became more complete, the idea of a sourced settings file came (first to me, then reality), new tag types have been formed, the sorting of blog entries is much more mature now, many more customisation possibilities cropped up and I developed an overall vision of what SBWG will become.

The code grew so much over the time. But it was mostly for very good reasons. I'm feeling overall quite content with how clean the code became and how many contingencies I took into account. It's probably still not "professional" code. But it is a new level of professionalism for me and my Bash scripts. Should I ever come close to getting through all the tiny, small, irrelevant, important, stupid, huge, new and old to-dos, it would probably be quite presentable. So many small features have been added that I hadn't thought of when I startet to write SBWG. Multi-author blogs are supported now, tags can be substituted for icons, a basic RSS feed works, ...

Comment via email

SBWG - Topic Tagpage Sorting

The way tagpages of topic (top:) tags are displayed is pretty much complete and the way I intended it to look when I first thought of splitting blog entry tags into two tag types. Basically, categories (cat: tags) are like classic tags in blogs: a way to add keywords to entries and list all entries that are tagged with that keyword. Topic tags on the other hand are meant to tag entries that talk about a certain topic about which a visitor might want to read up regardless of when individual entries on the topic have been posted. A topic tapage represents an index on a topic. It lists all the entries that are relevant to the topic in a structured way. You could write a book, one chapter per blog entry and have it simultaniously presented as a weblog (with category tags) and as a orderly indexed book in which visitors up a certain chapter. I still have to make up my mind on how I want to structure my own blog entries. But the fuctionality in the software is there. I will very likely also add another feature that enables structuring entries and pages into custom menues. So the options for web site builders will be grand with this weird took.

Comment via email

SBWG 0.8.6-wip

By now the initial introduction of SBWG that I've written in the first entry in this category is not only incomplete but in its details largely obsolete. SBWG matured a great deal in the last months and I feel like publishing a new version. I have started documenting stuff properly in the README and other files included in the package since I have a set goal for version 1.0. I will likely add no new features until I have made something that I can satidfyingly call version 1.0. Right now I'm in between versions 0.8.6 and 0.8.7. I call in-between version wip (work in progress). I feel like publishing what I have so far right now. So here is a wip version: 0.8.6-wip.

Comment via email

Bash Dotfiles

I've finally taken some time to overhaul my dotfiles. I took a lot from git repositories and reddit posts that I've found and added some functions and aliases myself. I'm using Bash 4 and haven't tested any of this with any other shell. I've removed and redacted personal lines.

.bashrc

.bash_profile

.bash_aliases

.bash_functions

.bash_options

There are a lot of dotfiles that other's have shared that would be worth sharing here. But it's easy to find those and I don't know what you'd like or not. So I'll point out just one simple project, "Sensible Bash by Mattia Tezzele, that attempts to set saner Bash defaults.

Comment via email

Beep Wars

This is one of these things that gets copied around without crediting the author or mentioning any source. Yea, I'm doing it too. I don't know where I got it from anymore.

If you still have a physical pcspeaker, execute this in your Linux shell for some beep music:

beep -l 350 -f 392 -D 100 -n -l 350 -f 392 -D 100 -n -l 350 -f 392 -D 100 -n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 350 -f 392 -D 100 -n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 700 -f 392 -D 100 -n -l 350 -f 587.32 -D 100 -n -l 350 -f 587.32 -D 100 -n -l 350 -f 587.32 -D 100 -n -l 250 -f 622.26 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 350 -f 369.99 -D 100 -n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 -n -l 700 -f 392 -D 100 -n -l 350 -f 784 -D 100 -n -l 250 -f 392 -D 100 -n -l 25 -f 392 -D 100 -n -l 350 -f 784 -D 100 -n -l 250 -f 739.98 -D 100 -n -l 25 -f 698.46 -D 100 -n -l 25 -f 659.26 -D 100 -n -l 25 -f 622.26 -D 100 -n -l 50 -f 659.26 -D 400 -n -l 25 -f 415.3 -D 200 -n -l 350 -f 554.36 -D 100 -n -l 250 -f 523.25 -D 100 -n -l 25 -f 493.88 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 25 -f 440 -D 100 -n -l 50 -f 466.16 -D 400 -n -l 25 -f 311.13 -D 200 -n -l 350 -f 369.99 -D 100 -n -l 250 -f 311.13 -D 100 -n -l 25 -f 392 -D 100 -n -l 350 -f 466.16 -D 100 -n -l 250 -f 392 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 700 -f 587.32 -D 100 -n -l 350 -f 784 -D 100 -n -l 250 -f 392 -D 100 -n -l 25 -f 392 -D 100 -n -l 350 -f 784 -D 100 -n -l 250 -f 739.98 -D 100 -n -l 25 -f 698.46 -D 100 -n -l 25 -f 659.26 -D 100 -n -l 25 -f 622.26 -D 100 -n -l 50 -f 659.26 -D 400 -n -l 25 -f 415.3 -D 200 -n -l 350 -f 554.36 -D 100 -n -l 250 -f 523.25 -D 100 -n -l 25 -f 493.88 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 25 -f 440 -D 100 -n -l 50 -f 466.16 -D 400 -n -l 25 -f 311.13 -D 200 -n -l 350 -f 392 -D 100 -n -l 250 -f 311.13 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 300 -f 392.00 -D 150 -n -l 250 -f 311.13 -D 100 -n -l 25 -f 466.16 -D 100 -n -l 700 -f 392

I've converted it for FreeBSD:

echo "L15T120MSO4G...G...G...D#..A#G...D#..A#G.....>D...D...D...D#..<A#F#...D#..A#G.....>G...<G..G>G...F#..FED#E.~~~~<G#~>C#...C..<BA#AA#.~~~~D#~F#...D#..GA#...G..A#>D.....G...<G..G>G...F#..FED#E.~~~~<G#~>C#...C..<BA#AA#.~~~~D#~G...D#..A#G.._~D#...A#G....." > /dev/speaker

I'm no musician. I know it's not quite right. But if you ask me, for what it is it's almost good enough. (I think you can tell that I'm not a Star Wars fan.)

Comment via email

Generate Random Passwords in Linux

Two ways to generate random passwords on the command line:


#!/bin/bash
if [ $# -eq 0 ]
then
  echo "I'll not generate anything for you if you won't give me at least one argument."
  echo "1st argument: password length"
  echo "2nd argument: number of passwords (optional)"
  exit
fi
i=0
if [ $# -gt 1 ]
then
  num=$2
else
  num=1
fi
while [ $i -lt $num ]; do
  let i=i+1 
  </dev/urandom tr -dc '12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB' | head -c$1
  echo ""
done

Pass the length of the password as the first argument and the number of passwords as the second argument, if you want more than one.

Or you can use just this line to generate a single 16 character long password with the same rules.


</dev/urandom tr -dc '12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB' | head -c16; echo ""

Or just use pwgen (man page) to have more options. It's probably in your distro's repos.

Comment via email
Mastodon