Skip to content

Instantly share code, notes, and snippets.

@Saruspete
Saruspete / ocr_comments.lua
Last active September 7, 2024 00:13
A lua script for nextcloud files_script to run OCR with tesseract and put the output as comment
local fileObj = get_input_files()[1]
if (fileObj == nil or not is_file(fileObj) ) then
abort("No file name was provided")
end
local fileMeta = meta_data(fileObj)
if ( not string.find(fileMeta.mimetype, "image/") ) then
abort("File ".. fileObj.name .. " is not an image (mimetype:" .. fileMeta.mimetype ..")")
end
@Saruspete
Saruspete / xdpdropcafe.c
Created August 9, 2021 17:50
xdp prog to drop unknown ethertype 0xcafe frm Veritas Cluster that increments bond interfaces drop counter
#include <linux/if_ether.h>
#include <linux/bpf.h>
#include <arpa/inet.h>
#define SEC(NAME) __attribute__((section(NAME), used))
SEC("xdpdropcafe")
int xdp_dropcafe(struct xdp_md *xdp) {
void *data_end = (void *)(long)xdp->data_end;
void *data = (void *)(long)xdp->data;
#!/usr/bin/env sh
set -u
export LC_ALL=C
# Reexec'ing myself as root
if [[ "$(id -u)" != "0" ]]; then
# Check with sudo
if ! [[ -r /dev/ipmi0 ]] && ! type -P sudo >/dev/null; then
echo >&2 "You need sudo to reexec this script as root for ipmi access"
@Saruspete
Saruspete / activateWindowByPid.sh
Last active March 15, 2021 15:27
Focus on a window provided a given PID
#!/usr/bin/env bash
set -o nounset -o noclobber
export LC_ALL=C
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"
export PS4=' (${BASH_SOURCE##*/}:$LINENO ${FUNCNAME[0]:-main}) '
function getParent {
typeset pid="$1"
awk '$1 == "PPid:"{ print $2}' /proc/$pid/status
@Saruspete
Saruspete / pre-post-exec.sh
Created March 2, 2021 21:58
bash pre/post exec hooks
# Some placeholders
typeset -i _BASHEXEC_PREEXECTIME=0
typeset _BASHEXEC_PREEXECCMD=""
function bashexec_post {
# Don't display anything if we didn't ran anything
if [[ -n "$_BASHEXEC_PREEXECCMD" ]]; then
echo "Took $((${SECONDS:-$(date +%s)} - $_BASHEXEC_PREEXECTIME ))s to run '$_BASHEXEC_PREEXECCMD'"
fi
LD_LIBRARY_PATH='$LIB' ./elf/ldconfig -p
(gdb) info stack
#0 0x000000000049032b in expand_dynamic_string_token (l=0x0, input=0x7ffe35525c60 "$LIB") at dl-load.c:400
#1 0x0000000000490442 in fillin_rpath (rpath=<optimized out>, rpath@entry=0x7ffe35525c60 "$LIB", result=0x14c65e0, sep=sep@entry=0x4c5f10 ":;", what=what@entry=0x4b1ac9 "LD_LIBRARY_PATH",
where=where@entry=0x0, l=l@entry=0x0) at dl-load.c:465
#2 0x000000000049094f in _dl_init_paths (llp=<optimized out>, source=0x4b1ac9 "LD_LIBRARY_PATH", glibc_hwcaps_prepend=<optimized out>, glibc_hwcaps_mask=<optimized out>) at dl-load.c:825
#3 0x00000000004648e2 in _dl_non_dynamic_init () at dl-support.c:344
#4 0x0000000000465b96 in __libc_init_first (argc=2, argv=0x7ffe35525f58, envp=0x7ffe35525f70) at init-first.c:68
@Saruspete
Saruspete / args2csv.sh
Created February 18, 2021 17:49
Pure bash function to convert a list of args into CSV
function args2csv {
typeset -a fields=("$@")
# Escape existing quotes
fields=("${fields[@]//\"/\"\"}");
# Add " before and after fields
fields=("${fields[@]/#/\"}")
@Saruspete
Saruspete / checkport.sh
Created February 9, 2021 15:47
Check that no process is listening on ephemeral port range
#!/usr/bin/env bash
# Get min/max port range from sysctl
prange="$(sysctl net.ipv4.ip_local_port_range| awk '{print $3,$4}')"
pmin="${prange% *}"
pmax="${prange#* }"
# TODO: ignore ports in sysctl net.ipv4.ip_local_reserved_ports
ss --listen --numeric --tcp --process | awk -v pmin=$pmin -v pmax=$pmax '
@Saruspete
Saruspete / rm_safe.sh
Last active December 14, 2020 11:54
A wrapper over rm to protect critical paths from accidental deletion
# Override RM to protect critical folders. Regex
typeset -gxa MYS_RM_PROTECT=(
"$HOME/[^/]+"
"/root/.+"
)
# TODO: 2 unhandled cases:
# - protected path contains a symlink
# - recursive parent deletion
function rm {
typeset todel= safedir= confirmdir=""
@Saruspete
Saruspete / hashblock.sh
Last active May 12, 2020 00:27
Hash per block for large files
#!/usr/bin/env bash
set -o nounset
set -o noclobber
export LC_ALL=C
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"
readonly MYSELF="$(readlink -f $0)"
readonly MYPATH="${MYSELF%/*}"