Last active
December 15, 2023 21:26
-
-
Save Wagahai/3ee960f210252488322f24a31d15b6af to your computer and use it in GitHub Desktop.
generic-bash-functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Most recent version is at https://gist.githubusercontent.com/Wagahai/3ee960f210252488322f24a31d15b6af/raw/generic-bash-functions.sh | |
# Last updated: 2023-12-15 | |
# Include this file to have access to the various functions it provides | |
# The script allows for a custom prefix before the command | |
# ie: it-commandname | |
cmdprefix="it" | |
enablecolors=true | |
# If not running interactively, don't do anything | |
case $- in | |
*i*) ;; | |
*) return;; | |
esac | |
# Test if we are being sourced or run manually | |
(return 0 2>/dev/null) | |
if [ $? -eq 2 ]; then | |
echo "This script is meant to be sourced, exiting" | |
exit 1 | |
fi | |
# This is what adjusts the command names | |
if [ -z "${functionloaded}" ]; then | |
#echo "Functions script loaded." | |
functionloaded=1 | |
source <(sed -e "s/FUNCTIONPREFIX/${cmdprefix}/g" -e "s!BASHSOURCE!${BASH_SOURCE}!g" ${BASH_SOURCE}) | |
return | |
fi | |
if [ ! -z "${enablecolors}" ]; then | |
declare -A colorarray | |
for colorinfo in 31:red 32:green 33:brown 34:blue 35:purple 36:cyan 37:lightgrey 1:bold 0:reset; do | |
colorinfoarr=(${colorinfo//:/ }) | |
colornum=${colorinfoarr[0]}; | |
colorname=${colorinfoarr[1]}; | |
colorarray[${colorinfoarr[1]}]=`echo -en "\e[${colornum}m"` | |
done | |
fi | |
#Sample usage: echo ${colorarray[red]}RED${colorarray[reset]} yes? | |
# This function handles re-sourcing of this script if needed | |
FUNCTIONPREFIX-re-source-functions() { | |
echo "Re-loading functions" | |
unset functionloaded | |
source BASHSOURCE | |
} | |
## SSL certificate related functions | |
FUNCTIONPREFIX-ssl-showinfo-remote() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} url" | |
else | |
echo | openssl s_client -showcerts -servername $1 -connect $1:443 2>/dev/null | openssl x509 -inform pem -noout -text | |
fi | |
} | |
FUNCTIONPREFIX-ssl-showinfo-csrinfo() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} filename.csr" | |
else | |
openssl req -text -noout -verify -in $1 2>/dev/null|grep Subject|grep CN | |
fi | |
} | |
FUNCTIONPREFIX-ssl-showinfo-crtinfo() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} filename.crt" | |
else | |
openssl x509 -in $1 -text -noout |grep -5 "Not Before"|egrep '(Issuer|Not Before|Not After|Subject:|Public-key)' | |
fi | |
} | |
FUNCTIONPREFIX-ssl-comparekey-and-cert() { | |
if [ -z "$2" ]; then | |
echo "Usage: ${FUNCNAME} filename.key filename.crt" | |
else | |
openssl pkey -in $1 -pubout -outform pem | sha256sum | |
openssl x509 -in $2 -pubkey -noout -outform pem | sha256sum | |
fi | |
} | |
FUNCTIONPREFIX-ssl-remove-key-passphrase() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} filename.key" | |
else | |
mv $1 ${1}-with-pass | |
openssl rsa -in ${1}-with-pass -out $1 | |
fi | |
} | |
FUNCTIONPREFIX-ssl-download-remote() { | |
if [ -z "$2" ]; then | |
echo "Usage: ${FUNCNAME} website:port filename (note: do not include https..)" | |
else | |
portinfo=(${1//:/ }) | |
</dev/null openssl s_client -connect ${portinfo[0]}:${portinfo[1]} -servername ${portinfo[0]} | openssl x509 > $2 | |
fi | |
} | |
## STORAGE FUNCTIONS | |
FUNCTIONPREFIX-storage-grow-disk() { | |
[ $(id -u) -eq 0 ]||{ echo "You must be root to use this."; return; } | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} /mounted/path" | |
echo "ie: ${FUNCNAME} /opt" | |
echo "NOTE: The (virtual) hardware disk size must be changed before using this" | |
else | |
mounteddevice=$1 | |
[ -d "${mounteddevice}" ]|| { echo "Device path not found ($mounteddevice), aborting";return; } | |
# Resize differently depending on LVM or RAW DISK | |
echo "Disk usage pre-adjustment" | |
df -HlT "${mounteddevice}" | |
storageselect=( $(findmnt -nrT ${mounteddevice}) ) | |
case ${storageselect[1]} in | |
/dev/mapper*) echo "Found that ${mounteddevice} is LVM" | |
source <(lvs --noheadings --nameprefixes --segments ${storageselect[1]} -o +lv_size,devices) | |
finddevice=(${LVM2_DEVICES//\(/ }) | |
echo "Running partprobe ${finddevice[0]}" | |
partprobe ${finddevice[0]} | |
echo "Running pvresize /dev/${storageinfo[0]}" | |
pvresize ${finddevice[0]} | |
echo "Running lvresize -l100%VG ${mounteddevice}" | |
lvresize -l100%VG ${storageselect[1]};; | |
/dev/s*) echo "Found that ${mounteddevice} is a raw disk (not LVM)" | |
echo "Running partprobe /dev/${storageinfo[0]}" | |
partprobe ${storageselect[1]};; | |
*) echo "Disk was not identified as LVM or standard disk, aborting..";exit 1;; | |
esac | |
echo "Found ${storageselect[2]} on volume" | |
case ${storageselect[2]} in | |
xfs) xfs_growfs ${mounteddevice};; | |
ext3) resize2fs ${storageselect[1]};; | |
ext4) resize2fs ${storageselect[1]};; | |
*) echo "Not familiar with filetype (${storageselect[1]}), could not resize";; | |
esac | |
echo "Disk usage post-adjustment" | |
df -HlT "${mounteddevice}" | |
fi | |
} | |
FUNCTIONPREFIX-storage-remove-device() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} device (without /dev/path, just \"sda\")" | |
else | |
echo "1" > /sys/block/$1/device/delete | |
fi | |
} | |
## Various network items | |
FUNCTIONPREFIX-check-port() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} IP:PORT" | |
else | |
IFS=':' read -r -a portcheck <<< "$1" | |
timeout 5 echo > /dev/tcp/${portcheck[0]}/${portcheck[1]}&&echo "Port ${portcheck[1]} open on ${portcheck[0]}"||echo "Port ${portcheck[1]} closed on ${portcheck[0]}" | |
fi | |
} | |
FUNCTIONPREFIX-download-withcurl() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} URL (recommend using quotes around the URL)" | |
else | |
curl -JLO "$1" | |
fi | |
} | |
FUNCTIONPREFIX-download-withwget() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} URL (recommend using quotes around the URL)" | |
else | |
wget --content-disposition "$1" | |
fi | |
} | |
FUNCTIONPREFIX-download-and-sourcewithcurl() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} URL (recommend using quotes around the URL)" | |
else | |
bash <(curl -s "$1") | |
fi | |
} | |
FUNCTIONPREFIX-download-and-sourcewithwget() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} URL (recommend using quotes around the URL)" | |
else | |
bash <(wget -qO- "$1") | |
fi | |
} | |
# Misc local functions | |
FUNCTIONPREFIX-java-find-home() { | |
dirname $(dirname $(readlink -f $(which javac))) | |
} | |
FUNCTIONPREFIX-java-set-home() { | |
JAVA_HOME=`dirname $(dirname $(readlink -f $(which javac)))` | |
PATH=$JAVA_HOME/bin:$PATH | |
export JAVA_HOME | |
export PATH | |
echo "JAVA_HOME is now set to ${JAVA_HOME} and adjusted the PATH" | |
} | |
FUNCTIONPREFIX-ssh-agent-and-add-key() { | |
eval `ssh-agent` | |
echo "SSH_AUTH_SOCK is set to $SSH_AUTH_SOCK" | |
echo "Adding default key" | |
ssh-add | |
} | |
# String and other operations | |
#Handy when looking at disk q trying to find the associated number | |
FUNCTIONPREFIX-letter-to-number() { | |
letter=$1 | |
alphabet=(0 a b c d e f g h i j k l m n o p q r s t u v w x y z) | |
for i in "${!alphabet[@]}"; do | |
if [[ "${alphabet[$i]}" = "${letter}" ]]; then | |
echo "${i}" | |
#arraypos=${i} | |
#let "arraypos++" | |
#echo "$arraypos"; | |
fi | |
done | |
} | |
FUNCTIONPREFIX-file-convert-to-lowercase() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} filename" | |
else | |
myfile=$1 | |
[ -w "${myfile}" ]|| { echo "File path not writable ($myfile), aborting";return; } | |
cp "${myfile}" "/tmp/${myfile}.tmp" | |
> ${myfile} | |
tr '[:upper:]' '[:lower:]' < "/tmp/${myfile}.tmp" >> ${myfile} | |
rm "/tmp/${myfile}.tmp" | |
fi | |
} | |
FUNCTIONPREFIX-file-convert-to-uppercase() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} filename" | |
else | |
myfile=$1 | |
[ -w "${myfile}" ]|| { echo "File path not writable ($myfile), aborting";return; } | |
cp "${myfile}" "/tmp/${myfile}.tmp" | |
> ${myfile} | |
tr '[:lower:]' '[:upper:]' < "/tmp/${myfile}.tmp" >> ${myfile} | |
rm "/tmp/${myfile}.tmp" | |
fi | |
} | |
FUNCTIONPREFIX-file-add-line-numbers() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} filename" | |
else | |
myfile=$1 | |
[ -w "${myfile}" ]|| { echo "File path not writable ($myfile), aborting";return; } | |
cp "${myfile}" "/tmp/${myfile}.tmp" | |
> ${myfile} | |
awk '{ print FNR " " $0 }' < "/tmp/${myfile}.tmp" >> ${myfile} | |
rm "/tmp/${myfile}.tmp" | |
fi | |
} | |
FUNCTIONPREFIX-file-sort() { | |
myfile=$1 | |
[ -z "${myfile}" ]&& { echo "Usage: ${FUNCNAME} filename";return; } | |
[ -w "${myfile}" ]|| { echo "File path not writable ($myfile), aborting";return; } | |
cp "${myfile}" "/tmp/${myfile}.tmp";> ${myfile};sort < "/tmp/${myfile}.tmp" >> ${myfile} | |
} | |
# DOCKER/PODMAN | |
FUNCTIONPREFIX-docker-run-shell-in-active-container() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} containerID" | |
else | |
MYCMD=(${FUNCNAME//-/ });${MYCMD[1]} exec -it $1 ${2:-bash} | |
fi | |
} | |
FUNCTIONPREFIX-podman-run-shell-in-active-container() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} containerID" | |
else | |
MYCMD=(${FUNCNAME//-/ });${MYCMD[1]} exec -it $1 ${2:-bash} | |
fi | |
} | |
FUNCTIONPREFIX-docker-quickrun-shell-in-container-then-remove() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} container" | |
else | |
MYCMD=(${FUNCNAME//-/ });${MYCMD[1]} exec -it $1 /bin/bash | |
fi | |
} | |
FUNCTIONPREFIX-podman-quickrun-shell-in-container-then-remove() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} container" | |
else | |
MYCMD=(${FUNCNAME//-/ });${MYCMD[1]} exec -it $1 /bin/bash | |
fi | |
} | |
FUNCTIONPREFIX-dns-lookup-rDNS-for-class-C() { | |
if [ -z "$1" ]; then | |
echo "Usage: ${FUNCNAME} X.X.X" | |
else | |
IPPFX=$1 | |
for i in `seq 1 255` ; do LIST="$LIST ${IPPFX}.$i" ; done | |
for i in $LIST ; do | |
ENTRY="`host $i`" | |
[ $? -ne 0 ] && continue | |
ENTRY=`echo "$ENTRY" | sed -e 's/.* //' -e 's/\.$//'` | |
echo -e "$i\t$ENTRY" | |
done | |
fi | |
} | |
#docker run -it geerlingguy/docker-"${1:-ubuntu1604}"-ansible /bin/bash | |
FUNCTIONPREFIX-server-info() { | |
echo "${colorarray[reset]}" | |
echo "${colorarray[red]}Hostname:${colorarray[reset]} `hostname -s`" | |
echo "${colorarray[red]}Virtualization type:${colorarray[reset]} `systemd-detect-virt`" | |
echo "${colorarray[red]}IPs:${colorarray[reset]} `hostname -I`" | |
awk -v red=${colorarray[red]} -v reset=${colorarray[reset]} -F': ' '/model name/ {a=$2;count++} END{print red "CPU: " reset count " vCPU cores running "a}' /proc/cpuinfo | |
awk '$3=="kB"{if ($2>1024^2){$2=$2/1024^2;$3="GB";} else if ($2>1024){$2=$2/1024;$3="MB";}} /MemAvailable/;/MemTotal/;/Swap/' /proc/meminfo | column -t|sed -e "s/^/${colorarray[red]}/" -e "s/ /${colorarray[reset]} /" | |
echo -n "${colorarray[red]}Last reboot:${colorarray[reset]} " | |
uptime -p|cut -d " " -f2- | xargs | |
echo "" | |
} | |
alias FUNCTIONPREFIX-calculator='echo "Use CTRL-D to exit";echo "";bc -l' | |
alias FUNCTIONPREFIX-git-config-set-global-name="git config --global user.name" | |
alias FUNCTIONPREFIX-git-config-set-global-email="git config --global user.email" | |
alias FUNCTIONPREFIX-git-config-set-global-sslverify-false="git config --global http.sslVerify false" | |
alias FUNCTIONPREFIX-git-config-set-global-sslverify-true="git config --global http.sslVerify true" | |
alias FUNCTIONPREFIX-git-config-set-THIS-sslverify-false="git config http.sslVerify false" | |
alias FUNCTIONPREFIX-git-config-set-THIS-sslverify-true="git config http.sslVerify true" | |
alias FUNCTIONPREFIX-process-list-extended="ps -axf --header -o user,pid,ppid,start_time,%cpu,rssize=Resident,vsize=Virtual,cmd" | |
alias FUNCTIONPREFIX-show-url-headers-withcurl="curl -D- -o/dev/null" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment