2022-11-05 20:57:02 +01:00
|
|
|
# Mullvad VPN checker.
|
|
|
|
function mullvad() {
|
|
|
|
echo ""
|
|
|
|
python3 <(cat <<EOF
|
|
|
|
|
|
|
|
from rich import print
|
|
|
|
from rich.panel import Panel
|
|
|
|
import json, urllib.request
|
|
|
|
|
|
|
|
with urllib.request.urlopen("https://am.i.mullvad.net/json") as url:
|
|
|
|
data = json.load(url)
|
|
|
|
|
|
|
|
print(
|
|
|
|
Panel.fit(
|
|
|
|
"[white]{0}\n{1}[/white]\n[cyan]{2}[/cyan]\n[blue]{3}[/blue]".format(
|
|
|
|
data["ip"], data["city"], data["mullvad_exit_ip_hostname"], data["mullvad_server_type"]
|
|
|
|
), title="Mullvad connection", style="bold green"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
# Snakify filenames.
|
|
|
|
function snakify () {
|
|
|
|
emulate -L zsh
|
|
|
|
local filename=$(basename -- "$1")
|
|
|
|
local extension="${filename##*.}"
|
|
|
|
local filename="${filename%.*}"
|
|
|
|
local new_filename=$(echo ${filename// /_} | tr '-' '_' | tr -cd '[:alnum:]._-')
|
|
|
|
local new_basename=$(echo "$new_filename.$extension")
|
|
|
|
mv -i "$1" "$new_basename"
|
|
|
|
mv -i "$new_basename" "${new_basename:l}"
|
2022-09-13 15:37:45 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Print timestamp as %Y-%m-%d %H:%M:%S.
|
|
|
|
function tstamp() {
|
2022-09-13 15:37:45 +02:00
|
|
|
emulate -L zsh
|
|
|
|
date '+%Y-%m-%d %H:%M:%S'
|
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Lifted from strcat.de
|
2022-09-13 15:37:45 +02:00
|
|
|
# Find history events by search pattern and list them by date.
|
2022-11-05 20:57:02 +01:00
|
|
|
function whatwhen () {
|
2022-09-13 15:37:45 +02:00
|
|
|
emulate -L zsh
|
|
|
|
local usage help ident format_l format_s first_char remain first last
|
|
|
|
usage='USAGE: whatwhen [options] <searchstring> <search range>'
|
|
|
|
help='Use `whatwhen -h'\'' for further explanations.'
|
|
|
|
ident=${(l,${#${:-Usage: }},, ,)}
|
|
|
|
format_l="${ident}%s\t\t\t%s\n"
|
|
|
|
format_s="${format_l//(\\t)##/\\t}"
|
|
|
|
# Make the first char of the word to search for case
|
|
|
|
# insensitive; e.g. [aA]
|
|
|
|
first_char=[${(L)1[1]}${(U)1[1]}]
|
|
|
|
remain=${1[2,-1]}
|
|
|
|
# Default search range is `-100'.
|
|
|
|
first=${2:-\-100}
|
|
|
|
# Optional, just used for `<first> <last>' given.
|
|
|
|
last=$3
|
|
|
|
case $1 in
|
|
|
|
("")
|
|
|
|
printf '%s\n\n' 'ERROR: No search string specified. Aborting.'
|
|
|
|
printf '%s\n%s\n\n' ${usage} ${help} && return 1
|
|
|
|
;;
|
|
|
|
(-h)
|
|
|
|
printf '%s\n\n' ${usage}
|
|
|
|
print 'OPTIONS:'
|
|
|
|
printf $format_l '-h' 'show help text'
|
|
|
|
print '\f'
|
|
|
|
print 'SEARCH RANGE:'
|
|
|
|
printf $format_l "'0'" 'the whole history,'
|
|
|
|
printf $format_l '-<n>' 'offset to the current history number; (default: -100)'
|
|
|
|
printf $format_s '<[-]first> [<last>]' 'just searching within a give range'
|
|
|
|
printf '\n%s\n' 'EXAMPLES:'
|
|
|
|
printf ${format_l/(\\t)/} 'whatwhen grml' '# Range is set to -100 by default.'
|
|
|
|
printf $format_l 'whatwhen zsh -250'
|
|
|
|
printf $format_l 'whatwhen foo 1 99'
|
|
|
|
;;
|
|
|
|
(\?)
|
|
|
|
printf '%s\n%s\n\n' ${usage} ${help} && return 1
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
# -l list results on stout rather than invoking $EDITOR.
|
|
|
|
# -i Print dates as in YYYY-MM-DD.
|
|
|
|
# -m Search for a - quoted - pattern within the history.
|
|
|
|
fc -li -m "*${first_char}${remain}*" $first $last
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Lifted from the extract plugin for oh-my-zsh.
|
|
|
|
# Extract archive into current directory.
|
|
|
|
function extract() {
|
2022-09-13 15:37:45 +02:00
|
|
|
setopt localoptions noautopushd
|
|
|
|
|
|
|
|
if (( $# == 0 )); then
|
|
|
|
cat >&2 <<'EOF'
|
|
|
|
Usage: extract [-option] [file ...]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-r, --remove Remove archive after unpacking.
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
|
|
|
|
local remove_archive=1
|
|
|
|
if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
|
|
|
|
remove_archive=0
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
|
|
|
local pwd="$PWD"
|
|
|
|
while (( $# > 0 )); do
|
|
|
|
if [[ ! -f "$1" ]]; then
|
|
|
|
echo "extract: '$1' is not a valid file" >&2
|
|
|
|
shift
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
local success=0
|
|
|
|
local extract_dir="${1:t:r}"
|
|
|
|
local file="$1" full_path="${1:A}"
|
|
|
|
case "${file:l}" in
|
|
|
|
(*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$file" | tar xv } || tar zxvf "$file" ;;
|
|
|
|
(*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$file" ;;
|
|
|
|
(*.tar.xz|*.txz)
|
|
|
|
tar --xz --help &> /dev/null \
|
|
|
|
&& tar --xz -xvf "$file" \
|
|
|
|
|| xzcat "$file" | tar xvf - ;;
|
|
|
|
(*.tar.zma|*.tlz)
|
|
|
|
tar --lzma --help &> /dev/null \
|
|
|
|
&& tar --lzma -xvf "$file" \
|
|
|
|
|| lzcat "$file" | tar xvf - ;;
|
|
|
|
(*.tar.zst|*.tzst)
|
|
|
|
tar --zstd --help &> /dev/null \
|
|
|
|
&& tar --zstd -xvf "$file" \
|
|
|
|
|| zstdcat "$file" | tar xvf - ;;
|
|
|
|
(*.tar) tar xvf "$file" ;;
|
|
|
|
(*.tar.lz) (( $+commands[lzip] )) && tar xvf "$file" ;;
|
|
|
|
(*.tar.lz4) lz4 -c -d "$file" | tar xvf - ;;
|
|
|
|
(*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$file" ;;
|
|
|
|
(*.gz) (( $+commands[pigz] )) && pigz -dk "$file" || gunzip -k "$file" ;;
|
|
|
|
(*.bz2) bunzip2 "$file" ;;
|
|
|
|
(*.xz) unxz "$file" ;;
|
|
|
|
(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$file" ;;
|
|
|
|
(*.lz4) lz4 -d "$file" ;;
|
|
|
|
(*.lzma) unlzma "$file" ;;
|
|
|
|
(*.z) uncompress "$file" ;;
|
|
|
|
(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$file" -d "$extract_dir" ;;
|
|
|
|
(*.rar) unrar x -ad "$file" ;;
|
|
|
|
(*.rpm)
|
|
|
|
command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
|
|
|
|
&& rpm2cpio "$full_path" | cpio --quiet -id ;;
|
|
|
|
(*.7z) 7za x "$file" ;;
|
|
|
|
(*.deb)
|
|
|
|
command mkdir -p "$extract_dir/control" "$extract_dir/data"
|
|
|
|
builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
|
|
|
|
builtin cd -q control; extract ../control.tar.*
|
|
|
|
builtin cd -q ../data; extract ../data.tar.*
|
|
|
|
builtin cd -q ..; command rm *.tar.* debian-binary ;;
|
|
|
|
(*.zst) unzstd "$file" ;;
|
|
|
|
(*.cab) cabextract -d "$extract_dir" "$file" ;;
|
|
|
|
(*.cpio) cpio -idmvF "$file" ;;
|
|
|
|
(*)
|
|
|
|
echo "extract: '$file' cannot be extracted" >&2
|
|
|
|
success=1 ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
(( success = success > 0 ? success : $? ))
|
|
|
|
(( success == 0 && remove_archive == 0 )) && rm "$full_path"
|
|
|
|
shift
|
|
|
|
|
|
|
|
# Go back to original working directory in case we ran cd previously
|
|
|
|
builtin cd -q "$pwd"
|
|
|
|
done
|
|
|
|
}
|
2022-11-05 20:57:02 +01:00
|
|
|
alias x=extract
|
2022-09-13 15:37:45 +02:00
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Create a tarball from given directory.
|
|
|
|
function create-archive() {
|
|
|
|
local archive_name
|
|
|
|
archive_name="$1.tar.gz"
|
|
|
|
archive_name=${archive_name/\//}
|
|
|
|
tar cvfz "$archive_name" "$1"
|
|
|
|
echo "Created archive $archive_name"
|
2022-09-13 15:37:45 +02:00
|
|
|
}
|
|
|
|
compdef _directories create-archive
|
|
|
|
|
|
|
|
# Create a /overview/ of all available function()'s; the description for
|
|
|
|
# each funtion() *must* be the first line above the string `function'!
|
|
|
|
# Otherwise it wont work.
|
2022-11-05 20:57:02 +01:00
|
|
|
# Display all function()'s with their descriptions.
|
|
|
|
function funlist() {
|
|
|
|
grep -B 1 "^function" $HOME/.zshrc.d/functions.zsh | \
|
|
|
|
grep -v "^\-\-$" | \
|
|
|
|
awk '{ if(/^#/) { gsub(/^#[:space:]*/, ""); ht=$0 }; getline; gsub(/ ?\(\)/, ":"); printf("-> %-20s %s\n", $2, ht); }' | \
|
|
|
|
sort -u -k 3
|
2022-09-13 15:37:45 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Show directory stack and ask for a dir to switch to.
|
2022-09-13 15:37:45 +02:00
|
|
|
function dstack() {
|
|
|
|
emulate -L zsh
|
|
|
|
autoload -U colors
|
|
|
|
local color=$fg_bold[blue]
|
|
|
|
integer i=0
|
|
|
|
dirs -p | while read dir
|
|
|
|
do
|
2022-11-05 20:57:02 +01:00
|
|
|
local num="${$(printf "%-4d " $i)/ /.}"
|
|
|
|
printf " %s $color%s$reset_color\n" $num $dir
|
|
|
|
(( i++ ))
|
2022-09-13 15:37:45 +02:00
|
|
|
done
|
|
|
|
integer dir=-1
|
|
|
|
read -r 'dir?Jump to directory: ' || return
|
|
|
|
(( dir == -1 )) && return
|
|
|
|
if (( dir < 0 || dir >= i ))
|
|
|
|
then
|
2022-11-05 20:57:02 +01:00
|
|
|
echo d: no such directory stack entry: $dir
|
|
|
|
return 1
|
2022-09-13 15:37:45 +02:00
|
|
|
fi
|
|
|
|
cd ~$dir
|
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# zremovecomp() remove *.zwc files.
|
2022-09-13 15:37:45 +02:00
|
|
|
function zremovecomp() {
|
|
|
|
local i
|
|
|
|
for i in ${HOME}/*.zwc(N); do
|
|
|
|
printf "Removing $i\n"
|
|
|
|
command rm -f $i
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# grep(1)'ing $HISTFILE.
|
|
|
|
function histgrep () { fc -fl -m "*(#i)$1*" 1 | grep -i --color $1 }
|
2022-09-13 15:37:45 +02:00
|
|
|
|
|
|
|
# A nicer output of cal(1)
|
2022-11-05 20:57:02 +01:00
|
|
|
# Colorize the output of cal(1).
|
2022-09-13 15:37:45 +02:00
|
|
|
function calendar() {
|
2022-11-05 20:57:02 +01:00
|
|
|
if [[ ! -f /usr/bin/cal ]] ; then
|
|
|
|
echo "Please install cal before trying to use it!"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [[ "$#" = "0" ]] ; then
|
|
|
|
/usr/bin/cal | egrep -C 40 --color "\<$(date +%e| tr -d ' ')\>"
|
|
|
|
else
|
|
|
|
/usr/bin/cal $@ | egrep -C 40 --color "\<($(date +%B)|$(date +%e | tr -d ' '))\>"
|
|
|
|
fi
|
2022-09-13 15:37:45 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Invoke this every time you change .zshrc to recompile it.
|
|
|
|
function src() {
|
|
|
|
autoload -U zrecompile
|
|
|
|
[ -f ~/.zshrc ] && zrecompile -p ~/.zshrc
|
|
|
|
[ -f ~/.zcompdump ] && zrecompile -p ~/.zcompdump
|
|
|
|
[ -f ~/.zcompdump ] && zrecompile -p ~/.zcompdump
|
|
|
|
[ -f ~/.zshrc.zwc.old ] && command rm -f ~/.zshrc.zwc.old
|
|
|
|
[ -f ~/.zcompdump.zwc.old ] && command rm -f ~/.zcompdump.zwc.old
|
|
|
|
source ~/.zshrc
|
2022-09-13 15:37:45 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Do an ls after cd.
|
|
|
|
function cd() { builtin cd "$@" && ls; }
|
2022-09-13 15:37:45 +02:00
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Create new directory and enter it.
|
|
|
|
function mkd() { mkdir -p "$@" && cd "$_"; }
|
2022-09-13 15:37:45 +02:00
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Display pids of commands.
|
|
|
|
function pids() { pgrep -a "$@"; }
|
2022-09-13 15:37:45 +02:00
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# zshall manpage.
|
|
|
|
function manzsh() { /usr/bin/man zshall | most +/"$1" ; }
|
2022-09-13 15:37:45 +02:00
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Restart zsh.
|
|
|
|
function restart() { exec $SHELL $SHELL_ARGS "$@"; }
|
2022-09-13 15:37:45 +02:00
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# cd to ~, clear screen, and restart zsh.
|
|
|
|
function rsrc() { cd && clear && restart; }
|
2022-09-13 15:37:45 +02:00
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Parse HackerNews feed (https://hnrss.org/newest).
|
|
|
|
function hn() {
|
2022-09-13 15:37:45 +02:00
|
|
|
python3 <(cat <<EOF
|
|
|
|
|
|
|
|
import feedparser
|
|
|
|
HNFeed = feedparser.parse("https://hnrss.org/newest")
|
|
|
|
|
|
|
|
class bcolors:
|
|
|
|
HEADER = '\033[95m'
|
|
|
|
ENDC = '\033[0m'
|
|
|
|
|
|
|
|
for item in HNFeed.entries:
|
|
|
|
print(f"{bcolors.HEADER}%s{bcolors.ENDC}" % item.title)
|
|
|
|
print(item.links[0].href)
|
|
|
|
print("")
|
|
|
|
|
|
|
|
EOF
|
|
|
|
) | bat -p
|
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Fetch open source license.
|
|
|
|
function license() {
|
2022-09-13 15:37:45 +02:00
|
|
|
local base_url
|
|
|
|
base_url="https://api.github.com/licenses"
|
|
|
|
|
|
|
|
local headers
|
|
|
|
headers="Accept: application/vnd.github.drax-preview+json"
|
|
|
|
|
|
|
|
local res
|
|
|
|
if (( $# == 0 )); then
|
|
|
|
res=$(curl --silent --header $headers $base_url)
|
|
|
|
|
|
|
|
echo "Available licenses:"
|
|
|
|
echo
|
|
|
|
echo "$res" | jq ".[].key" | tr -d '"'
|
|
|
|
fi
|
|
|
|
|
|
|
|
local license="$argv[1]"
|
|
|
|
res=$(curl --silent --header $headers $base_url/$license | jq ."body")
|
|
|
|
echo -e $res | tr -d '"'
|
|
|
|
}
|
|
|
|
|
2022-11-05 20:57:02 +01:00
|
|
|
# Use nmap from podman to check for open SSH ports on local net.
|
2022-09-13 15:37:45 +02:00
|
|
|
if test -x "$(command -v podman)"; then
|
2022-11-05 20:57:02 +01:00
|
|
|
function nmap_ssh() {
|
2022-09-13 15:37:45 +02:00
|
|
|
if sudo podman image exists localhost/nmap; then
|
|
|
|
sudo podman run -it --rm \
|
|
|
|
--cap-add=NET_RAW \
|
|
|
|
--cap-add=NET_ADMIN \
|
|
|
|
--network host \
|
|
|
|
--name nmap \
|
|
|
|
nmap -sV -p 22 -open 10.0.0.0/24
|
|
|
|
else
|
|
|
|
echo "localhost/nmap image does not exist"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
fi
|