Pure BASH interactive CLI/TUI menu (single-/muliti-select/checkboxes)

First version. To be refactord.

asciicast

https://gist.github.com/blurayne/f63c5a8521c0eeab8e9afd8baa45c65e

Inspired by

Notes

  • This is a hacky first implementation for my shell tools/dotfiles (ZSH)
  • Intention is to use it for CLI wizards (my aim is NOT a full blown curses TUI window interface)
  • I converted TPUT to ANSII-sequences to spare command executions (e.g. tput ed | xxd --plain)

Permission to copy and modify is granted under the Creative Commons Attribution 4.0 license

Gedankenspiel: Automatisierung bei Big Corporate

Zielsetzung: Wir möchten von eigenes Autos auf Taxi als Dienstleister wechseln.

Wir kennen allerdings das Taxi-Business zu wenig, fühlen uns unsicher und bestellen daher erstmal Fahrer, die in unseren eigenen Autos Taxi fahren sollen. Die Fahrer sind aber keine ausgebildeten Taxi-Fahrer, kennen die Strassen nicht, und das Navi ist leider ein propritäres Produkt mit veralteter Karte. Allerdings von namhaften Hersteller, was nicht wirklich was bringt. Ebenso bieten wir auch unseren Fahrern Kaffee von Starbucks, damit sie nicht einschlafen und die Straßen lernen können.

Ziemlicher Käse, oder?

Kubernetes wäre in dem Fall das Car-Sharing. MyTaxi.eu oder Uber die Amazon-Services. Und weil Amazon es eh besser kann, denken wir, wir könnten unseren “Taxi-Fahrern ohne Plan” ja von Über gemietete Autos hinstellen, in der Hoffnung, das sie die Straßen dann effizienter abfahren.

Oder auf deutsch: warum Migration in Baby-Steps manchmal Unsinn ist. Automatisiert was geht, und nehmt bitte Abstand von halbgahren Lösungen, weil Kontrollfreaks nunnal FUD und Babysteps propagieren. Die Zeit, die für #reinventingthewheel oder das “wir wollen es erstmal verstehen oder sehen dass es funktioniert” drauf geht, kann man getrost auch ins Development stecken. Infrastructure first. Nehmt euch DevOps die euch ne Infrastruktur und ne CI hinstellen und lässt sie machen. Umso schneller steigt ihr in eure eigenes Taxis ein und müsst euch um den Weg zum Ziel keine Gedanken mehr machen. Je besserer die Automatisierung läuft, desto weniger Fehler gibt es. Eine Liste mit zig Punkten, die trotzdem manuell abgearbeitet werden muss, bleibt nunmal fehlerträchtig und es geht nur menschliche Zeit und Nerven darauf. Und Devs lernen auch nichts, außer menschliche Fehler zu wiederholen. Also, noch mehr Frust satt an einer CI zu arbeiten, die Dinge wirklich einfacher machen würde. Time to Rollback. Time to go live. Instant response. Zeit für Tests und Qualität statt immer der gleiche Bullshit.

Auch ja, und dann gab es noch diese fetten Limosinen aka. vertikale Bare Metal Skalierung. Hat sich leider im Berufsverkehr als unpraktikabel erwiesen. Aber hübsch sehen sie ja aus. Wer’s braucht…

PHP: just in case posix_isatty() is missing

From StackOverflow

Short function

function is_a_tty() 
{
    static $result;
    if (is_null(result)) {
        $fp = fopen('php://stdin', 'r');
        $stat = fstat($fp);
        $mode = $stat['mode'] & 0170000; // S_IFMT
        $result = $mode == 0020000; // S_IFCHR
        fclose($fp);
    }
    return $result;
}

Info Class from StackOverflow by leigh

<?php

class IOMode
{
    public $stdin;
    public $stdout;
    public $stderr;

    private function getMode(&$dev, $fp)
    {
        $stat = fstat($fp);
        $mode = $stat['mode'] & 0170000; // S_IFMT

        $dev = new StdClass;

        $dev->isFifo = $mode == 0010000; // S_IFIFO
        $dev->isChr  = $mode == 0020000; // S_IFCHR
        $dev->isDir  = $mode == 0040000; // S_IFDIR
        $dev->isBlk  = $mode == 0060000; // S_IFBLK
        $dev->isReg  = $mode == 0100000; // S_IFREG
        $dev->isLnk  = $mode == 0120000; // S_IFLNK
        $dev->isSock = $mode == 0140000; // S_IFSOCK
    }

    public function __construct()
    {
        $this->getMode($this->stdin, fopen('php://stdin', 'r'));
        $this->getMode($this->stdout, fopen('php://stdout', 'w'));
        $this->getMode($this->stderr, fopen('php://stderr', 'w'));
    }
}

$io = new IOMode;

Input

$ php io.php
// Character device as input
// $io->stdin->isChr  == true

$ echo | php io.php
// Input piped from another command
// $io->stdin->isFifo == true

$ php io.php < infile
// Input from a regular file (name taken verbatim from C headers)
// $io->stdin->isReg  == true

$ mkdir test
$ php io.php < test
// Directory used as input
// $io->stdin->isDir  == true
Output:

$ php io.php
// $io->stdout->isChr  == true

$ php io.php | cat
// $io->stdout->isFifo == true

$ php io.php > outfile
// $io->stdout->isReg  == true
Error:

$ php io.php
// $io->stderr->isChr  == true

$ php io.php 2>&1 | cat
// stderr redirected to stdout AND piped to another command
// $io->stderr->isFifo == true

$ php io.php 2>error
// $io->stderr->isReg  == true

I hope those PHP stuff finally dies 🙁

Infojunk November 2018

Hacking / MITM-API-Testing

Linux

Windows

Python

KataCode

  • KataCode Playground with fully funcional real browser shells for learning without barriers (using Containers?)
  • GoTTY
    How about gotty -w docker run -it --rm anapsix/nyancat:alpine?

Spectre/Meltdown

Project

Security

Tools

AWS

Development

Other

Connect to GitLab via SSH

Start an ssh-agent

If not yet done add to your rc-file ike .bashrc or .zshrc

$ eval `ssh-agent`

Add your generated key

$ ssh-add ~/.ssh/id_rsa

List keys

$  ssh-add -l

GitShell anyone?

GitLab should response with git-shell. You shall log in by git-user only!

$ ssh -T git@myserver.lab
Welcome to GitLab, Markus Geiger!

You should not get an Interactive Shell like sh!

Debugging
Git Version> 2.2
GIT_SSH_COMMAND="ssh -vvv -T git@gitserver" GIT_TRACE=2 git clone git@gitserver/foo.git
Git Version < 2.2
```bash


# Debugging

```bash
ssh -vv -i privatekey -l git $@
GIT_SSH="sshwrapper-script" GIT_TRACE=2 git clone git@gitserver/foo.git

Other way

git config --global credential.helper cache

bash: shell table output to json

You know that sometimes it would be really great to format a shell output to a more versatile format like JSON or YAML you can process with jq instead of writing long pipes with text-processing.

Ah yeah, you could use python instead of bash 😉

Before

$ virsh net-list                     
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

After

$ virsh net-list | table-to-json 
[
    {
        "autostart": "yes", 
        "name": "default", 
        "persistent": "yes", 
        "state": "active"
    }
]
$ virsh net-list | bin/table-to-json  | jq -r ".[0].name"
default

table-to-json

#!/usr/bin/env python

import sys
import re
import json

def parse_line(line):
    if line.find("\t") == -1:
        line = re.sub(r'\s+', '\t', line , flags=re.IGNORECASE)
    line = re.sub(r'^\s+', '', line , flags=re.IGNORECASE)
    line = re.sub(r'\s+$', '', line , flags=re.IGNORECASE)
    return [x for x in line.split("\t") if x]

lastparts = []
columns = None
data = []
for line in sys.stdin:
    parts = parse_line(line)
    if len(parts)>0:
        if len(parts)>0 and parts[0].startswith("-"):
            columns=[x.lower() for x in lastparts]
        elif len(parts)>0 and columns:
            data.append(dict(zip(columns, parts)))
    lastparts = parts

print json.dumps(data, sort_keys=True, indent=4)

UPDATE: Maybe the script should use asciitable

Infojunk October 2018

Browser Extensions

Collaborative Coding

Focusing on IDEs. Web-based solutions are mostly ignored.

Linux

NodeJS

DevOps

AI/MachineLearning

AWS

Jame’s Path Selector is not as much powerful as jq but Amazon AWS probably chose it since it might be faster and probably query-selectors are a bit more sophisticated (?)

Python

Android

Security

Font Ligatures

Other

Color-Laser-Printer: Xerox Workcentre 6515DNI

I finally gave up my 15 year old color laser printer Konica magicolor 2530DL due a failed firmware update. I cannot find out how to reset firmware via USB-Stick since most links now go to 404 and DL model from is2003 does not have a parallel port anymore.USB and Ethernet is dead. Help is appreciated!

In opposite to the perfect Linux drivers the Windows Drivers became frustrating with a Windows 10 update: printing ended often just with one page and the rest hang on “processing”. Sometimes the printer misplaced the layout. Sadly, time for a new one!

One thing: Coming from an agency background print quality is of course king. I spared Samsung, HP and Co. They may have good quality but I heard several times that they are not long-living products and get very hot 🙁

I was looking for

  • A Laser Printer (ever got a wet ink on paper?)
  • Good Printing Quality
  • A Duplex Printer
  • If Combi: A Good Scanner (CCD before CIS)
  • WLAN (CloudPrint)
  • Scan to USB
  • Copy
  • Linux Support

Final Round

Xerox Workcentre 6515DNI

If you search for a printer only: Xerox Versalink 500DN

Kyocera Ecosys M5526cdw

  • Printing @ 1200x1200dpi, text sucks because of toner diffusion
  • Eco friendly toner (finally!)
  • Mobile Print/Scan App looks OK though it may crash
  • Print Service for Android](https://play.google.com/store/apps/details?id=com.kyocera.print.service.plugin)

  • Released 08/2018

Samsung Xpress C1860FW

Brother DCP-L3550CDW

Trackback

PulseAudio: Mono-Sink Audio

Just in case your 10.000+ employee corporation doesn’t plug in the microphone-jack correctly and no one is allowed to ask questions (presentation-only).

Find the name of your audio sink by running

pacmd list-sinks | grep name:

Then run this command (taking care to remove the angled brackets):

pacmd load-module module-remap-sink sink_name=mono master=<name_of_audio > > sink_given_by_previous_command> channels=2 channel_map=mono,mono

or add the argument to pacmd to /etc/pulse/default.pa to have it run at startup.

Then in Sound Preferences choose “Mono” as the output, but remember to reduce volumes by half, since two channels are getting mixed into one, or else you’ll have distortion. To test, run:

speaker-test -c 2 -t sine

Same thing in a single command:

 pacmd load-module module-remap-sink sink_name=mono master=$(pacmd list-sinks | grep  -m 1 -oP 'name:\s<\K.*(?=>)') channels=2 channel_map=mono,mono

To remove the mono channel, just use:

pacmd unload-module module-remap-sink

Source: StackOverflow

Thanks to ondrejch!