137 lines
3.5 KiB
Bash
137 lines
3.5 KiB
Bash
# Utility functions
|
|
|
|
function copylastcmd() {
|
|
echo -n $(fc -ln -1) | wl-copy
|
|
}
|
|
|
|
function getkernelpkg() {
|
|
if [[ ${OS_ID} == "arch" ]]; then
|
|
pacman -Qqo /usr/lib/modules/$(uname -r)/vmlinuz
|
|
else
|
|
echo "Unsupported OS" >&2
|
|
fi
|
|
}
|
|
|
|
function random-xkcd() {
|
|
blocklist=(
|
|
"404"
|
|
"631" # "Anatomy Text", don't want that coming up in public thanks
|
|
)
|
|
link="https://xkcd.com/404/"
|
|
while (( $blocklist[(Ie)$(basename $link)] )); do
|
|
link=$(curl https://c.xkcd.com/random/comic/ -v |& grep location | cut -d' ' -f3 | sed 's/\r//')
|
|
done
|
|
html=$(curl -L ${link/http:/https:} 2>/dev/null)
|
|
name=$(echo $html | xmllint --html --xpath 'string(//html/body/div[@id="middleContainer"]/div[@id="ctitle"])' - 2>/dev/null)
|
|
title=$(echo $html | xmllint --html --xpath 'string(//html/body/div[@id="middleContainer"]/div[@id="comic"]/img/@title)' - 2>/dev/null)
|
|
echo $link
|
|
echo -e "\033[1;34m$name\033[0m"
|
|
echo -e "\033[1;33m$title\033[0m"
|
|
curl $(echo $html | (echo "https:"$(xmllint --html --xpath 'string(//html/body/div[@id="middleContainer"]/div[@id="comic"]/img/@src)' - 2>/dev/null))) 2>/dev/null | feh -
|
|
}
|
|
|
|
if [[ $(hostnamectl chassis) == "container" ]]; then
|
|
DEFAULT_WINDOW_NAME="Container Connection: [ $os_icon $hostname Console ]"
|
|
elif [[ -z "${SSH_CONNECTION+x}" ]]; then
|
|
DEFAULT_WINDOW_NAME="$os_icon $hostname Console"
|
|
else
|
|
DEFAULT_WINDOW_NAME="Remote Connection: [ $os_icon $hostname Console ]"
|
|
fi
|
|
|
|
function _rename_window() {
|
|
echo -ne "\033]0;$@\007"
|
|
}
|
|
|
|
function _reset_window_name() {
|
|
_rename_window "$DEFAULT_WINDOW_NAME"
|
|
}
|
|
|
|
function rename_window() {
|
|
_rename_window "$@"
|
|
DEFAULT_WINDOW_NAME="$@"
|
|
}
|
|
|
|
_rename_window "$DEFAULT_WINDOW_NAME"
|
|
|
|
function ssh-clean() {
|
|
# Clean up ssh connection sockets
|
|
for i in $(find ~/.ssh/cm/ -type s); do
|
|
fname=$(basename $i)
|
|
conn=$(echo $fname | cut -d- -f2-)
|
|
user=$(echo $conn | cut -d@ -f1)
|
|
host=$(echo $conn | cut -d@ -f2 | cut -d: -f1)
|
|
echo "Closing connection to $user@$host"
|
|
ssh -O exit -S $i -q _ 2>/dev/null
|
|
done
|
|
}
|
|
|
|
function lsssh() {
|
|
# List active ssh connections
|
|
for i in $(find ~/.ssh/cm/ -type s); do
|
|
fname=$(basename $i)
|
|
conn=$(echo $fname | cut -d- -f2-)
|
|
user=$(echo $conn | cut -d@ -f1)
|
|
host=$(echo $conn | cut -d@ -f2 | cut -d: -f1)
|
|
echo "$user@$host"
|
|
done
|
|
}
|
|
|
|
function bwunlock() {
|
|
# Unlock Bitwarden vault and export session key
|
|
local bwfd
|
|
local bwpasswd
|
|
local secretstore
|
|
local bwcmd
|
|
local skipkeyring
|
|
|
|
# Use default Bitwarden account
|
|
secretstore="bw"
|
|
bwcmd="bw"
|
|
skipkeyring=0
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
--personal)
|
|
# Use personal Bitwarden account
|
|
secretstore="bw-personal"
|
|
bwcmd="env BITWARDENCLI_APPDATA_DIR=$HOME/.config/bw-cli-personal bw"
|
|
;;
|
|
--debug)
|
|
# Enable debug output
|
|
set -x
|
|
;;
|
|
--ask)
|
|
# Prompt for Bitwarden password
|
|
skipkeyring=1
|
|
;;
|
|
*)
|
|
echo "Unknown option: $1" >&2
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
# Attempt to read Bitwarden password from keyring
|
|
if ! (( skipkeyring )); then
|
|
bwpasswd=$(secret-tool lookup service $secretstore 2>/dev/null)
|
|
fi
|
|
if [[ -z "${bwpasswd}" ]]; then
|
|
# If password is not found in keyring, prompt for it
|
|
read -s -p "Enter Bitwarden password: " bwpasswd
|
|
echo
|
|
# Store Bitwarden password in keyring
|
|
if ! (( skipkeyring )); then
|
|
secret-tool store --label="Bitwarden" service $secretstore
|
|
fi
|
|
fi
|
|
# Unlock Bitwarden vault and export session key
|
|
export BW_SESSION=$(eval $bwcmd unlock --raw "'$bwpasswd'" 2>/dev/tty)
|
|
if [[ -z "${BW_SESSION}" ]]; then
|
|
echo "Failed to unlock Bitwarden vault" >&2
|
|
return 1
|
|
else
|
|
echo "Bitwarden vault unlocked"
|
|
fi
|
|
|
|
}
|