From 95a4248e98e118024f5065636288406e8e8c9a30 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Tue, 22 Oct 2019 22:42:07 -0400 Subject: [PATCH] various fixes --- .bashrc | 33 +------ .iank/.bashrc | 1 - .iank/.inputrc | 1 - .iank/brc | 1 - Makefile | 2 +- brc | 21 ++++- brc2 | 147 +++++++++++++++++++++++++------- conflink | 12 +-- distro-end | 5 +- rootsshsync | 4 +- sl/.iank/.bashrc | 1 + sl/.iank/.inputrc | 1 + sl/.iank/brc | 1 + {.iank => sl/.iank}/ll-function | 0 ssh-emacs-setup | 4 +- system-status | 18 +++- 16 files changed, 171 insertions(+), 81 deletions(-) delete mode 120000 .iank/.bashrc delete mode 120000 .iank/.inputrc delete mode 120000 .iank/brc create mode 120000 sl/.iank/.bashrc create mode 120000 sl/.iank/.inputrc create mode 120000 sl/.iank/brc rename {.iank => sl/.iank}/ll-function (100%) diff --git a/.bashrc b/.bashrc index c27af43..ac3ec03 100644 --- a/.bashrc +++ b/.bashrc @@ -9,38 +9,9 @@ #exec 2>>/a/tmp/bashlog -# By default this file is sourced for ALL ssh commands. This is wonky. -# Normally, this file is not sourced when a script is run, but we can -# override that by having #!/bin/bash -l. I want something similar for ssh -# commands. when a local script runs an ssh command, this file should not be -# sourced by default, but we should be able to override that. -# -# So here we test for conditions of a script under ssh and return if so. -# And we don't keep the rest of the code in this file, because even -# though we return, we already parsed the whole code, and as I develop -# the code, the parsing can have errors, which can screw up cronjobs -# etc. -# -# To test for an overriding condition, we have a few options. one is to -# use an environment variable. env variables sent across ssh are -# strictly limited. ssh -t which sets $SSH_TTY, but within a script that -# won't work because tty allocation will fail. However, I do use -t for -# strange hosts, so we consider it an indicator. We could override an -# obscure unused LC_var, like telephone, but I don't want to run into -# some edge case where that messes things up. we could transfer a file -# which we could test for, but I can't think of a way to make that -# inherently limited to a single ssh command. I choose to set SendEnv -# and AcceptEnv ssh config vars to allow the environment variable -# BASH_LOGIN_SHELL to propagate across ssh. This also requires that we -# wrap ssh in interactive shells, because, once we export the var, it -# will go into scripts, and we want it to be nondefault there. -# -# -c is set whenever a command is passed to ssh -# -i is set whenever a command is not passed +# see comments in brc2 sl() function for background. if [[ $SSH_CONNECTION ]] \ - && [[ $- == *c* ]] \ - && [[ $- != *i* ]] \ - && { [[ ! $SSH_TTY ]] || [[ $BASH_LOGIN_SHELL == false ]] ; } ; then + && [[ $BRC != t ]]; then return 0 else diff --git a/.iank/.bashrc b/.iank/.bashrc deleted file mode 120000 index 988a415..0000000 --- a/.iank/.bashrc +++ /dev/null @@ -1 +0,0 @@ -../.bashrc \ No newline at end of file diff --git a/.iank/.inputrc b/.iank/.inputrc deleted file mode 120000 index 36b66bf..0000000 --- a/.iank/.inputrc +++ /dev/null @@ -1 +0,0 @@ -../.inputrc \ No newline at end of file diff --git a/.iank/brc b/.iank/brc deleted file mode 120000 index 4f1c8d0..0000000 --- a/.iank/brc +++ /dev/null @@ -1 +0,0 @@ -../brc \ No newline at end of file diff --git a/Makefile b/Makefile index 37aeef9..3c19e63 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ all: ~/.local/distro-begin ~/.local/distro-end -~/.local/distro-begin: distro-begin mail-setup /a/bin/buildscripts/emacs +~/.local/distro-begin: distro-begin mail-setup /a/bin/buildscripts/emacs ssh-emacs-setup distro-begin ~/.local/distro-end: distro-end distro-pkgs pkgs primary-setup /a/bin/buildscripts/tor-browser /a/bin/buildscripts/go /a/bin/buildscripts/rust /a/bin/buildscripts/misc diff --git a/brc b/brc index 33e6646..9e368d4 100644 --- a/brc +++ b/brc @@ -424,10 +424,25 @@ dus() { # du, sorted, default arg of e() { echo "$@"; } -# echo var. print var including escapes, etc +# echo args +ea() { + if (( ! $# )); then + echo no args + fi + for arg; do + printf "%qEOL\n" "${arg}" + printf "%s" "${arg}" |& hexdump -C + done +} +# echo vars. print var including escapes, etc ev() { - printf "%qEOL\n" "${!1}" - printf "%s" "${!1}" |& hexdump -C + if (( ! $# )); then + echo no args + fi + for arg; do + printf "%qEOL\n" "${!arg}" + printf "%s" "${!arg}" |& hexdump -C + done } diff --git a/brc2 b/brc2 index fb14eaa..220f124 100644 --- a/brc2 +++ b/brc2 @@ -54,6 +54,21 @@ anki() { fi } +acat() { + shopt -s nullglob + hrcat /m/md/alerts/new/* /m/md/alerts/cur/* + shopt -u nullglob +} +aclear() { + shopt -s nullglob + files=(/m/md/alerts/new/* /m/md/alerts/cur/*) + if (( ${#files[@]} )); then + rm -f ${files[@]} + fi + shopt -u nullglob + system-status _ +} + ap() { # pushd in case current directory has an ansible.cfg file pushd /a/xans >/dev/null @@ -78,6 +93,20 @@ astudio() { /a/opt/android-studio/bin/studio.sh "$@" &r; } +bindpush() { + lipush || return 1 + for h in li l2; do + sl $h <<'EOF' || return 1 +set -e +conflink +f=/var/lib/bind/db.b8.nz +ser stop bind9 +s rm -fv $f.jnl +s install -m 644 -o bind -g bind /p/c/machine_specific/linode/bind-initial/db.b8.nz $f +ser restart bind9 +EOF + done +} bbk() { # btrbk wrapper c / @@ -123,16 +152,17 @@ lipush() { local p a p=(/a/bin /a/exe /a/h /a/c /p/c/machine_specific/linode{,.hosts} /a/opt/{emacs,emacs-debianstable,mu}) a="-ahviSAXPH --specials --devices --delete --relative --exclude-from=/p/c/li-rsync-excludes" - rsync $@ $a ${p[@]} root@l2.b8.nz:/ - rsync $@ $a ${p[@]} /p/c/machine_specific/li root@iankelling.org:/ - rsync $@ -ahviSAXPH root@iankelling.org:/a/h/proposed-comments/ /a/h/proposed-comments + ret=0 + m rsync $@ $a ${p[@]} /p/c/machine_specific/l2 root@l2.b8.nz:/ || ret=$? + m rsync $@ $a ${p[@]} /p/c/machine_specific/li root@li.b8.nz:/ || ret=$? + m rsync $@ -ahviSAXPH root@iankelling.org:/a/h/proposed-comments/ /a/h/proposed-comments || ret=$? + return $ret } lipushnoe() { # noe = noemacs. for running faster. rsync $@ --delete-excluded -ahviSAXPH --specials --devices --delete --relative \ --exclude-from=/p/c/li-rsync-excludes /a/bin /a/exe /a/h /a/c /p/c/machine_specific/li root@li:/ } - #### begin bitcoin related things btc() { local f=/etc/bitcoin/bitcoin.conf @@ -233,6 +263,33 @@ debian_pick_mirror () { sudo sed -ri "/http.us.debian.org/ s@( *[^ #]+ +)[^ ]+([^#]+).*@\1$url\2# http.us.debian.org@" /etc/apt/sources.list sudo apt-get update } +dig() { + command dig +nostats +nocmd "$@" +} +# Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed. +digsort() { + local sec + sec= + dig +nordflag "$@" | sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' | while read -r l; do + if [[ $l == [^\;]* ]]; then + sec+="$l"$'\n' + else + if [[ $sec ]]; then + printf "%s" "$sec" | sort + sec= + fi + printf "%s\n" "$l" + fi + done +} +# note: only the soa master nameserver will respond with +# ra "recursive answer" flag. That difference is meaningless afaik. +# Same thing happens with gnu nameservers. +digme() { + digsort "$@" @ns1.iankelling.org | tee /tmp/digme + diff -u /tmp/digme <(digsort "$@" @ns2.iankelling.org) +} + dup() { local ran_d @@ -802,11 +859,33 @@ spend() { } # ssh, copy my universal config over if needed. + +# By default .bashrc is sourced for ALL ssh commands. This is wonky. +# Normally, this file is not sourced when a script is run, but we can +# override that by having #!/bin/bash -l. I want the same thing for ssh +# commands. when a local script runs an ssh command, bashrc should not be +# sourced, unless we use a modified command. +# +# So, in my bashrc, test for conditions of noninteractive ssh and return +# if so. And we don't keep the rest of the code in .bashrc, because +# even though we return, we parse the whole file which can cause errors +# as we develop it. +# +# To test for an overriding condition: bash builtin vars and env show no +# difference in ssh vs local, except shell level which is not +# reliable. one option is to use an environment variable. env variables +# sent across ssh are strictly limited. We could override an obscure +# unused LC_var, like telephone, but I don't want to run into some edge +# case where that messes things up. I choose to set SendEnv and +# AcceptEnv ssh config vars to allow the environment variable BRC to +# propagate across ssh, and for hosts I don't control, I start an inner +# shell with it set, which doubles up as a way to have a nondefault +# bashrc. sl() { # inspired from https://github.com/Russell91/sshrc - local args info_date info_t type now tmp old sshinfo + local args info_date info_t type now tmp tmp2 old sshinfo cmd haveinfo dorsync info_sec declare -a args tmpa now=$(date +%s) @@ -835,53 +914,59 @@ sl() { esac done remote="$1"; shift - old=false + if [[ ! $remote ]]; then + echo $0: error hostname required >&2 + return 1 + fi + dorsync=false + haveinfo=false tmpa=(/p/sshinfo/???????????"$remote") sshinfo=${tmpa[0]} if [[ -e $sshinfo ]]; then + haveinfo=true + fi + if $haveinfo; then tmp=${sshinfo[0]##*/} - info_date=${tmp::11} - type=${info_date: -1} - info_date=${info_date::10} - info_sec=$(date -d @$info_date +%s) - # debug - #e $(( $(stat -c%Y /b/ds/brc) - $(date -d @$info_date +%s) )) - if (( $(stat -c%Y /b/ds/brc) > info_sec || $(stat -c%Y /b/ds/.bashrc) > info_sec )); then - old=true + tmp2=${tmp::11} + type=${tmp2: -1} + if [[ $type == b ]]; then + info_sec=${tmp::10} + if (( $(stat -c%Y /b/ds/brc) > info_sec || $(stat -c%Y /b/ds/.bashrc) > info_sec )); then + dorsync=true + rm -f $sshinfo + fi fi else + dorsync=true # use this weird yes thing to ensure we know ssh succeeded - tmp=$(command ssh "${args[@]}" "$remote" "if test -e /p/sshinfo; then echo yes; fi") || return + tmp=$(command ssh "${args[@]}" "$remote" "if test -e /a/bin/ds/.bashrc -a -L .bashrc; then echo yes; fi") || return if [[ $tmp == yes ]]; then type=a else type=b - old=true fi fi + if $dorsync || ! $haveinfo; then + sshinfo=/p/sshinfo/$now$type"$remote" + touch $sshinfo + chmod 666 $sshinfo + fi if [[ $type == b ]]; then - if $old; then - RSYNC_RSH="ssh ${args[*]}" rsync -rptL /b/ds/.iank "$remote": - rm -f $sshinfo - sshinfo=/p/sshinfo/$now$type"$remote" - touch $sshinfo - chmod 666 $sshinfo + if $dorsync; then + RSYNC_RSH="ssh ${args[*]}" rsync -rptL /b/ds/sl/.iank "$remote": fi if (( ${#@} )); then # Theres a couple ways to do this. im not sure whats best, # but relying on bash 4.4+ escape quoting seems most reliable. - - command ssh -t "${args[@]}" "$remote" "INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc -c ${@@Q}" - # this way is bad - # command ssh -t "${args[@]}" "$remote" "printf \"%s; exit\" \"$*\" >.iank/brc2 - #INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc" - + command ssh "${args[@]}" "$remote" \ + BRC=t bash -c '"\"\$@\""' bash ${@@Q} else - command ssh -t "${args[@]}" "$remote" "INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc" + # -t gives us an interactive shell for normal ssh. -l makes us use the rcfile when piping commands. + command ssh -t "${args[@]}" "$remote" BRC=t INPUTRC=.iank/.inputrc bash --rcfile .iank/.bashrc -l fi else - BASH_LOGIN_SHELL=true command ssh "$remote" "$@" + BRC=t command ssh "$remote" "$@" fi } sss() { # ssh solo @@ -899,7 +984,7 @@ ssk() { } # plain limited ssh ssh() { - BASH_LOGIN_SHELL=true command ssh "$@" + BRC=t command ssh "$@" } diff --git a/conflink b/conflink index d72f98c..0ae24c3 100755 --- a/conflink +++ b/conflink @@ -25,11 +25,13 @@ s() { sudo "$@"; } lnf() { /a/exe/lnf "$@"; } now=$(date +%s) -f=~/.local/conflink -fast=false -if [[ -e $f ]] && (( $(stat -c %Y $f) > $now - 60*60*24 )); then - fast=true -fi + +# error prone +#f=~/.local/conflink +# fast=false +# if [[ -e $f ]] && (( $(stat -c %Y $f) > $now - 60*60*24 )); then +# fast=true +# fi if [[ $1 == -f ]]; then # f for fast fast=true shift diff --git a/distro-end b/distro-end index 7f2f12c..3178522 100755 --- a/distro-end +++ b/distro-end @@ -561,7 +561,10 @@ User=znc WantedBy=multi-user.target EOF ser daemon-reload - m sgo znc + # avoid restarting if possible, reconnecting to irc is annoying. + if [[ $(ser is-active znc) != active ]]; then + m sgo znc + fi ###### stop znc setup ##### end diff --git a/rootsshsync b/rootsshsync index 1a39d46..668f8a3 100755 --- a/rootsshsync +++ b/rootsshsync @@ -33,8 +33,8 @@ if [[ $SUDO_USER ]]; then fi user_ssh_dir=$(eval echo ~$user)/.ssh -if [[ ! -s $user_ssh_dir/config ]]; then - echo missing $user_ssh_dir/config. bad sign. bailing >&2 +if [[ ! -s $user_ssh_dir/authorized_keys ]]; then + echo missing $user_ssh_dir/authorized_keys. bad sign. bailing >&2 exit 1 fi diff --git a/sl/.iank/.bashrc b/sl/.iank/.bashrc new file mode 120000 index 0000000..4cd008b --- /dev/null +++ b/sl/.iank/.bashrc @@ -0,0 +1 @@ +../../.bashrc \ No newline at end of file diff --git a/sl/.iank/.inputrc b/sl/.iank/.inputrc new file mode 120000 index 0000000..22cd52b --- /dev/null +++ b/sl/.iank/.inputrc @@ -0,0 +1 @@ +../../.inputrc \ No newline at end of file diff --git a/sl/.iank/brc b/sl/.iank/brc new file mode 120000 index 0000000..7a3ce73 --- /dev/null +++ b/sl/.iank/brc @@ -0,0 +1 @@ +../../brc \ No newline at end of file diff --git a/.iank/ll-function b/sl/.iank/ll-function similarity index 100% rename from .iank/ll-function rename to sl/.iank/ll-function diff --git a/ssh-emacs-setup b/ssh-emacs-setup index ee1dd08..80976f0 100755 --- a/ssh-emacs-setup +++ b/ssh-emacs-setup @@ -22,8 +22,8 @@ set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR cd $(dirname $0) -teeu /etc/ssh/ssh_config 'SendEnv INSIDE_EMACS BASH_LOGIN_SHELL COLUMNS' -teeu /etc/ssh/sshd_config 'AcceptEnv INSIDE_EMACS BASH_LOGIN_SHELL COLUMNS' +teeu /etc/ssh/ssh_config 'SendEnv INSIDE_EMACS BRC COLUMNS' +teeu /etc/ssh/sshd_config 'AcceptEnv INSIDE_EMACS BRC COLUMNS' # get rid of useless motd stuff sed -i --follow-symlinks 's/^\s*PrintLastLog .*/PrintLastLog no/' /etc/ssh/sshd_config rm -f /etc/update-motd.d/10-help-text /etc/update-motd.d/00-header diff --git a/system-status b/system-status index ddb5b73..b3c22e4 100755 --- a/system-status +++ b/system-status @@ -53,12 +53,22 @@ write-status() { chars+=("q $qlen") fi + begin=false cd /b/ds if ! make -q ~/.local/distro-begin || [[ $(<~/.local/distro-begin) != 0 ]]; then - chars+=("DISTRO-BEGIN!") + begin=true fi + end=false if ! make -q ~/.local/distro-end || [[ $(<~/.local/distro-end) != 0 ]]; then + end=true + fi + + if $begin && $end; then + chars+=("DISTRO!") + elif $begin; then + chars+=("DISTRO-BEGIN!") + elif $end; then chars+=("DISTRO-END!") fi @@ -93,6 +103,10 @@ write-status() { for d in /a/bin/distro-setup /p/c; do cd $d + if [[ ! -e .git ]]; then + # some hosts i dont push all of /p/c + continue + fi untracked=$(git ls-files -o --exclude-standard) if [[ $untracked && $(find $untracked -mmin $fminplus -type f -print -quit) ]]; then v conflink: untracked in $d @@ -160,7 +174,7 @@ write-status() { fi done if (( maxtime < now - 60*60 )); then - chars+=("OLD-SNAPSHOT!") + chars+=("OLD-SNAP!") snapshotmsg="/o snapshot older than 1 hour" fi lo -1 old-snapshot $snapshotmsg -- 2.30.2