Skip to content

Instantly share code, notes, and snippets.

@thearchitect
thearchitect / deref.rs
Created July 15, 2015 03:49
Golang like defers for Rust
// http://stackoverflow.com/a/29963675/983835
struct ScopeCall<F: FnOnce()> {
c: Option<F>
}
impl<F: FnOnce()> Drop for ScopeCall<F> {
fn drop(&mut self) {
self.c.take().unwrap()()
}
}
package main
import (
"log"
"net"
"time"
)
func perr(err error) {
if err != nil {
package main
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
# nixos-rebuild switch
building Nix...
building the system configuration...
updating GRUB 2 menu...
activating the configuration...
setting up /etc...
# add this to configuration.nix
virtualisation = {
libvirtd = {
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo $DIR
@thearchitect
thearchitect / scan.sh
Created October 21, 2018 13:09 — forked from kirkins/scan.sh
Second version of script to turn nmcli into a json object
#!/usr/bin/env bash
#
# Scan availible wifi networks
# https://unix.stackexchange.com/questions/399222/jq-parse-colon-separated-value-pairs
# TODO see if you can pipe this directly into jq
nmcli_output=$(nmcli --mode multiline dev wifi)
# set all variables from nmcli
network=$(echo "$nmcli_output" | grep SSID: \
@thearchitect
thearchitect / create-hotspot.md
Created October 21, 2018 13:09 — forked from narate/create-hotspot.md
Create Wi-Fi Hotspot on Linux using nmcli

Create a Wi-Fi hotspot on Linux using nmcli

Original post : https://unix.stackexchange.com/a/310699

nmcli con add type wifi ifname wlan0 con-name Hostspot autoconnect yes ssid Hostspot
nmcli con modify Hostspot 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared
nmcli con modify Hostspot wifi-sec.key-mgmt wpa-psk
nmcli con modify Hostspot wifi-sec.psk "veryveryhardpassword1234"
@thearchitect
thearchitect / create-hotspot.md
Created October 21, 2018 13:09 — forked from gowrav/create-hotspot.md
Create Wi-Fi Hotspot on Linux using nmcli

Create a Wi-Fi hotspot on Linux using nmcli

Original post : https://unix.stackexchange.com/a/310699

nmcli con add type wifi ifname wlx7cdd9046ee19 con-name AiRpaperHostspot autoconnect yes ssid AiRpaperHostspot
#Connection 'AiRpaperHostspot' (81bd8ff0-1473-4bda-b00d-5759cd1c3a27) successfully added.
nmcli con modify AiRpaperHostspot 802-11-wireless.mode ap 802-11-wireless.band bg ipv4.method shared
nmcli con modify AiRpaperHostspot wifi-sec.key-mgmt none

Opening and closing an SSH tunnel in a shell script the smart way

I recently had the following problem:

  • From an unattended shell script (called by Jenkins), run a command-line tool that accesses the MySQL database on another host.
  • That tool doesn't know that the database is on another host, plus the MySQL port on that host is firewalled and not accessible from other machines.

We didn't want to open the MySQL port to the network, but it's possible to SSH from the Jenkins machine to the MySQL machine. So, basically you would do something like

ssh -L 3306:localhost:3306 remotehost

@thearchitect
thearchitect / tmux.sh
Created November 2, 2019 13:53 — forked from B-Galati/tmux.sh
tmux script example
#!/bin/bash
tmux has-session -t dev
if [ $? != 0 ]
then
tmux new-session -s dev -n "TEST" -d
tmux split-window -h -t dev:0
tmux split-window -v -t dev:0.1
tmux send-keys -t dev:0.0 'cd ~/foo/bar' C-m
tmux send-keys -t dev:0.1 'autossh -M 0 -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" test@test -t "cd ~/bar;bash"' C-m