bin: script updates

This commit is contained in:
xeruf 2024-02-20 19:54:51 +01:00
parent 9829b58ee7
commit b6d8ad24f2
18 changed files with 81 additions and 33 deletions

View File

@ -11,7 +11,6 @@
# video thumbnails via mtn, pdf pages from pdftoppm
# - text files are displayed through bat
# Usually automatically requests elevation through sudo when needed
# TODO .raw,.iso,.qcow2
set -o pipefail
@ -42,15 +41,22 @@ fileinfo() {
tput smso
$elevate file --exclude elf -E "$arg"
tput rmso
$elevate ssh-keygen -l -f "$arg" 2>/dev/null || true
size=$(stat --format=%s "$arg")
# Check if SSH key (<10KB then read)
if test "$size" -lt 10000
then $elevate ssh-keygen -l -f "$arg" 2>/dev/null
fi
# I think this check is here to avoid scrolling text interpreted as video
#probe="$($elevate ffprobe "$arg" 2>&1)"
#echo $probe | grep -v -e '00:00:00.04' -e 'ansi' &&
{ ! $inspect && $elevate ffprobe -hide_banner "$arg" 2>&1 | grep -E "bitrate: .{3,5} " | sed 's/, start:[^,]\+,/,/'; } ||
$elevate stat --format "%U:%G %A %s $(
# Print media infos over file infos when <2G
if ! { test "$size" -lt 2000000000 && $elevate ffprobe -hide_banner "$arg" 2>&1 | grep -E "bitrate: .{3,5} " | sed 's/, start:[^,]\+,/,/'; } || $inspect
then $elevate stat --format "%U:%G %A %s $(
size="$($elevate unzip -l "$arg" 2>/dev/null | tail -1)" &&
echo "(uncompressed $(echo $size | cut -d' ' -f1 | numfmt --to=iec-i --suffix=B))"
) - birth %.10w mod %.10y" "$arg" | numfmt --field=3 --to=iec-i --padding=6 --suffix=B
fi
done
tput sgr0
}
@ -68,7 +74,7 @@ for arg; do
fi
continue
fi
# amount of columns in a grid
# amount of items to display per line in a grid for two lines max
grid=$(expr $(tput cols) / \( 25 - \( $# / 2 \) \& $# \< 30 \| 5 \))
tmpfile="$prefix/$(basename -- "$arg")_$(dd "if=$arg" bs=512 count=10 2>/dev/null | md5sum | tr -d ' ' || true)"
mkdir -p "$prefix"
@ -93,8 +99,11 @@ for arg; do
echo Converting "$arg"
convert -flatten "$arg" png:"$tmpfile"
timg+=("$tmpfile");;
# TODO .raw .img
(*/x-iso*|*/x-qemu-disk*) fdisk -l "$arg";;
(*\ video/*)
suffix=_thumbs.jpg
! $inspect &&
# TODO sometimes duration mismatch for short videos
test $(printf "%.0f" $(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$arg")) -gt 3 &&
mtn -q -i -t -W -r$(expr 5 - $# \& $# \< 4 \| 1) -D6 -b 0.6 -c $grid -w $(expr $(tput cols) '*' 20) \
@ -162,7 +171,7 @@ if test "$timg"; then
if which timg >/dev/null
then $elevate timg $(test "$timga" && echo "-V") --rotate=exif -g $(tput cols)x$(expr $(tput lines) / 2) \
$(test $# -gt 1 &&
echo "-t0.2 --auto-crop --center $(test $# -lt 20 && echo "--title") --grid=$((grid < $# ? grid : $#))x2") \
echo "-t0.2 --auto-crop --center $(test $# -lt 20 && echo "--title") --grid=$(((grid < $# ? grid : $#) / 2))x") \
"${timg[@]}" "${timga[@]}" 2>/dev/null || true
else for img in "${timg[@]}"
do catimg -H $(expr $(tput lines) / 2) $img

View File

@ -2,7 +2,6 @@
# Rename a file with a backup suffix or revert it
# Add -a to act on multiple files,
# otherwise the second arg is used as suffix rather than the default "bak".
# TODO no sudo for symlinks
if test "$1" = "-a"; then
shift
for arg; do "$0" "$arg"; done

View File

@ -0,0 +1,2 @@
#!/bin/sh -e
bluetoothctl devices

View File

@ -78,7 +78,7 @@ highlight "c :: clean electron caches"
highlight "o :: optimize space extensively"
if [[ $1 =~ "o" ]]; then
sudo find $XDG_CACHE_HOME /var/cache /var/log /var/tmp -mindepth 1 -maxdepth 2 -atime +2 -exec rm -r {} + -prune
sudo find /root/.cache $XDG_CACHE_HOME /var/cache /var/log /var/tmp -mindepth 1 -maxdepth 2 -atime +2 -exec rm -r {} + -prune
if test -f "/var/log/apt/history.log"; then
aptclean_cur=$(cat "/var/log/apt/history.log" | wc -l)

View File

@ -1,6 +1,7 @@
#!/bin/sh -e
# Flatten folder hierarchy
# Args: depth
# Flatten folder hierarchies
# Moves subfolders up one layer, replacing the slash by underscore unless the parent folder has the same name.
# Args: depth of leaf folders to move up
depth=$(expr 1 \& $# \| 2)
case $1 in ([0-9]|[0-9][0-9]) depth=$1; shift;; esac
find "$@" -mindepth $depth -maxdepth $depth -depth -type d | while read folder; do

View File

@ -7,10 +7,12 @@
# - host
# - target directory name (and further arguments to clone)
# In an existing repo, first arg is omitted
# TODO: transform https://codeberg.org/forgejo-contrib/forgejo-helm -> forgejo-helm forgejo-contrib "" codeberg.org
if test -d ".git" # TODO search upwards
then repo="$(basename "$PWD")"
git remote set-url origin "$(git-repo "${3:-github.com}" "$repo" "${2:-$(git config --get user.name)}" "${@:4}")"
else test $# -eq 0 && echo "Usage: $0 [repo (omit if in repo)] [upstream owner] [own user] [url]" && exit 2
git remote remove upstream 2>/dev/null || true
else test $# -eq 0 && echo "Usage: $0 <repo (omit if in repo)> <upstream owner> [own user] [host]" && exit 2
repo="$1" && shift
if test "$#" -eq 0
then git-get "$repo"
@ -24,7 +26,6 @@ else test $# -eq 0 && echo "Usage: $0 [repo (omit if in repo)] [upstream owner]
cd "$(basename "$(expr "$4" \| "$repo")")"
fi
user="${1:-$repo}"
git remote remove upstream 2>/dev/null || true
git remote add upstream "$(git-repo "${3:-github.com}" "$repo" "$user")"
git remote -v
git-upstream "$user"
#git remote add upstream "$(git-repo "${3:-github.com}" "$repo" "$user")"
exec $SHELL

View File

@ -1,9 +1,6 @@
#!/bin/sh
# set given repository as upstream or add as a new remote
# ARGS:
# - user/org
# - repo
# - remote name
# Set given repository as upstream or add as a new remote
test $# -lt 1 && echo "$0 <user> [repo] [remote-name]" && exit 1
if test "$3"
then
name="$3"
@ -12,5 +9,5 @@ else
git remote get-url upstream >/dev/null 2>&1 && name="$1" || name="upstream"
fi
origin="$(git remote get-url origin)"
git remote add -f $name "$(echo $origin | cut -d':' -f1):$1/${2:-$(echo $origin | cut -d'/' -f2)}"
git remote add -f "$name" "$(echo $origin | cut -d':' -f1):$1/${2:-$(echo $origin | cut -d'/' -f2)}"
git remote -v

View File

@ -28,6 +28,8 @@ cmd="$1"
case "$cmd" in
(fwupdmgr|hunt|rdoc|gh|chordpro|bat|pdfjam|reflector|topgrade|r128gain|7z|kubectl|diffr|docker|jrnl|difft|wiked-diff|qpdf|ninja)
unbuffer "$@" --help | sed 's|^[^ ].*:|\0|' | $paginate;;
(sqlcmd) sqlcmd -?;;
(exa) "$0" eza;;
(caddy|stretchly|go|flutter)
shift
"$cmd" help "$@" | $paginate;;

View File

@ -1,10 +1,10 @@
#!/bin/sh
# Find terms in jrnl and turn them into tags
# Find terms in jrnl files and turn them into tags
# Check with jrnl --tags
if test $# -eq 0
then $0 sleep nap health tech read dev phone Zinc run bike tour laptop computer PC CB piano faith journal
else
for arg
do rpl "\(^\|[^@]\)\b$arg\b" "\1@$arg" $JOURNAL/*.txt
do rpl "\(^\|[^@]\)\b$arg\b" "\1@$arg" $JOURNAL/*.txt 2>/dev/null
done
fi

8
.local/bin/scripts/k8sviz Executable file
View File

@ -0,0 +1,8 @@
#!/bin/sh -e
ns=${1:-stackspin}
case "$1" in
("") /opt/k8sviz/k8sviz.sh --help;;
(-*) /opt/k8sviz/k8sviz.sh "$@";;
(*) set -x
/opt/k8sviz/k8sviz.sh --kubeconfig $KUBECONFIG --namespace "$ns" -t png -o "$(date +%y%m%d)_$ns.png";;
esac

View File

@ -1,7 +1,6 @@
#!/bin/sh
#!/bin/bash
# Mount a partition by label or device identifier automatically
# Includes interactive selection if no argument is given
# TODO losetup for raw and qcow2, including lvm handling
set -eo pipefail
_help="$0 <device> [mountpoint] [options...]"
case $1 in
@ -30,11 +29,13 @@ esac
# FILE as loopback device
if test -f "$arg"
then set -e
loopdevice="$(sudo losetup -f)"
sudo losetup -P -v "$loopdevice" "$arg"
$0 "$loopdevice"
then loopdevice="$(sudo losetup -f)"
if sudo losetup -P -v "$loopdevice" "$arg"
then $0 "$loopdevice" ||
{ sudo vgchange -ay &&
sudo pvs | grep "$loopdevice" | awk '{print $2}' | xargs -I% find /dev/% -mindepth 1 -exec sudo sh -c 'dir=/mnt/$(echo "{}" | sed "s|/dev/||;s|/|-|g") && mkdir -vp "$dir" && mount {} "$dir"' \; ; }
exit $?
fi
fi
# FSTAB: BY LABEL

View File

@ -16,7 +16,9 @@ do
# Clean empty files
$elevate find -name '.thumbnails' -printf 'Pre-cleaning %p\n' -exec rm -r {} +
$elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 3) \( -name '.stfolder' -o -name '.*keep' -o -name '*.py' -o -name 'nodelay.txt' -o -name '.git' -o -name 'tmp' -o -name '.nomedia' -o -name '__init__.py' -o -name '*ignore' -o -name 'instalee' \) \( -type d -o -type f \) -prune -o -empty -printf 'Removing empty %p\n' #-exec rm -d {} +
$elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 3) \
\( -name '.stfolder' -o -name '.*keep' -o -name '*.py' -o -name 'nodelay.txt' -o -name '.git' -o -name 'tmp' -o -name '.nomedia' -o -name '__init__.py' -o -name '*ignore' -o -name 'instalee' \) \
\( -type d -o -type f \) -prune -o -empty -printf 'Removing empty %p\n' -exec rm -d {} +
# $elevate find -H "$f" -maxdepth $(expr 1 \& "$f" = "/" \| 5 \& $# \> 0 \| 4) -type d -empty -name .stfolder -exec rm -div {} \;
test $# -eq 0 && exit $?
if test -e "$f"; then

View File

@ -4,6 +4,6 @@
export chars=$(expr $(tput cols) - 60)
case "$1" in (+*) size=${1#+}G; depth=2; shift;; esac
case "$1" in ([0-9]) depth=$1; shift;; esac
(du --max-depth "${depth:-1}" -xhat ${size:-500M} "$@" | sort -h | grep -v "^0") |
(du --max-depth "${depth:-1}" -xhat ${size:-50M} "$@" | sort -h | grep -v "^0") |
while read line; do echo "$(tput smso)$line$(tput rmso) $(pacman -Qqo $(echo $line | awk '{print $2}') 2>/dev/null | paste -s -d',' | sed "s/\(.\{${chars}\}\).*/\1.../")"; done || #| column -t ||
(du --max-depth "${depth:-1}" -xha "$@" | sort -h | tail)

View File

@ -3,7 +3,8 @@
# Trims everything beyond the given page number
test "$1" = "-q" && quiet=$1 && shift
case "$1" in ([0-9]) page=$1; shift;; esac
test ! -r "$1" && echo "Usage: sign [page] <document.pdf> [hoffset (-160) [voffset (-310) [scale [signature-image]]]]" && exit 1
test $# -lt 2 && echo "Usage: sign [page] <document.pdf> [hoffset (-160) [voffset (-310) [scale [signature-image]]]]"
test ! -r "$1" && exit 1
tmp_base=/tmp/sign
mkdir -p $tmp_base

View File

@ -7,3 +7,4 @@
# Any other arguments (usually pathnames) are passed on to the tree command
case "$1" in ([0-9]) depth=$1; shift;; esac
tree --dirsfirst --du -h -C -L ${depth:-3} -I node_modules "$@" | ${PAGER:-less} -rF
# TODO consider exa -T -L X

View File

@ -1,3 +1,3 @@
#!/bin/sh -e
test -f "$1" || cd "$XDG_DATA_HOME/openvpn"
tmux new-session -s "$@" "sudo openvpn $@"
tmux new-session -s "$@" "sudo openvpn $@ || read"

View File

@ -0,0 +1,3 @@
#!/bin/sh -x
# wg-quick reload interface
wg syncconf "$@" <(wg-quick strip "$@")

View File

@ -0,0 +1,21 @@
#!/bin/bash -e
# Source: https://serverfault.com/questions/285256/how-to-unban-an-ip-properly-with-fail2ban/1023005#1023005
test $# -eq 0 && sudo fail2ban-client status && exit 0
echo "Jails where $1 is locked up:"
JAILS=`fail2ban-client status | grep "Jail list" | sed -E 's/^[^:]+:[ \t]+//' | sed 's/,//g'`
for JAIL in $JAILS
do
currentjail=`fail2ban-client status $JAIL | grep -B 8 $1 | grep Status | awk '{printf $5}'`
if [[ ${#currentjail} -gt a ]] ; then
echo $currentjail
fi
done
echo
echo "To unban $1 use the following commands:"
for JAIL in $JAILS
do
currentjail=`fail2ban-client status $JAIL | grep -B 8 $1 | grep Status | awk '{printf $5}'`
if [[ ${#currentjail} -gt a ]] ; then
echo "fail2ban-client set $currentjail unbanip $1"
fi
done