|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- # see if which can find the command in the PATH
- # defined before other aliases so they don't get
- # added if the command they use isn't installed
- cmds_exist () {
- for cmd in ${@}; do
- if ! PATH="/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" which ${1} &>/dev/null; then
- return 1
- fi
- done
- return 0
- }
-
- if cmds_exist sudo; then
- if cmds_exist apt-fast; then
- alias apt-fast="sudo apt-fast"
- if [ -z $APT_CMD ]; then APT_CMD="apt-fast"; fi
- fi
- if cmds_exist apt; then
- alias apt="sudo apt"
- if [ -z $APT_CMD ]; then APT_CMD="apt"; fi
- fi
- if cmds_exist apt-get; then
- alias apt-get="sudo apt-get"
- if [ -z $APT_CMD ]; then APT_CMD="apt-get"; fi
- fi
-
- alias dist-upgrade="${APT_CMD} update -qq && ${APT_CMD} dist-upgrade"
- alias upgrade="${APT_CMD} update -qq && ${APT_CMD} upgrade --with-new-pkgs"
-
- if cmds_exist add-apt-repository; then
- alias add-apt-repository="sudo add-apt-repository"
- fi
-
- if cmds_exist apt-key; then
- alias apt-recv-key="sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys"
- fi
- alias dpkg="sudo dpkg"
- alias cleanoldkernels="dpkg --list | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'$(uname -r)'/q;p' | xargs ${APT_CMD} purge"
-
- function apt-get-update-repo () {
- options="-o Dir::Etc::sourcelist=sources.list.d/${1}.list -o Dir::Etc::sourceparts='-' -o APT::Get::List-Cleanup='0
- '"
- ${APT_CMD} ${options} update
- }
-
- alias service="sudo /usr/sbin/service"
-
- alias iwconfig="sudo /sbin/iwconfig"
- alias iw="sudo /sbin/iw"
- alias ip="sudo /sbin/ip"
- alias iptables="sudo /sbin/iptables"
- alias ip6tables="sudo /sbin/ip6tables"
-
- if cmds_exist tcpdump; then
- alias tcpdump="sudo /usr/sbin/tcpdump"
- fi
-
- if cmds_exist iftop; then
- alias iftop="sudo /usr/sbin/iftop"
- fi
-
- if cmds_exist nethogs; then
- alias nethogs="sudo /usr/sbin/nethogs"
- fi
-
- if cmds_exist iotop; then
- alias iotop="sudo /usr/sbin/iotop"
- fi
-
- if cmds_exist unbound-control; then
- alias unbound-control="sudo /usr/sbin/unbound-control"
- fi
-
- alias fsck="sudo /sbin/fsck"
-
- if cmds_exist lxc-start; then
- alias lxc-start="sudo /usr/bin/lxc-start"
- fi
- if cmds_exist lxc-stop; then
- alias lxc-stop="sudo /usr/bin/lxc-stop"
- fi
- if cmds_exist lxc-attach; then
- alias lxc-attach="sudo /usr/bin/lxc-attach"
- fi
- if cmds_exist lxc-ls; then
- alias lxc-ls="sudo /usr/bin/lxc-ls"
- fi
-
- if [ -f /opt/borgbackup/bin/borg ]; then
- alias overstock-borg="sudo BORG_REPO=/mnt/c_isilon/home/nbohman/borgbackup BORG_PASSPHRASE='aey4zah4eish9weekaizega7ooph9joo9eep3otoh4uev9eez4oof9Ko7ga3zah' /opt/borgbackup/bin/borg"
- fi
- fi
-
- if cmds_exist grc; then
- alias mtr="grc mtr --curses"
- alias ping="grc ping"
- alias dig="grc dig"
- fi
-
- if cmds_exist pigz; then
- alias gzip="pigz"
- fi
-
- if cmds_exist sfs; then
- alias organize_downloads="sfs ~/.config/sfs/Dowloads.yaml ~/Downloads"
- fi
-
- alias xz="xz -T0"
-
- if cmds_exist zfs; then
- function zfs_snapshots_by_size () {
- root_dataset=${1}
- zfs list -r -t snapshot -o name,used,creation ${root_dataset} | sort -k 2,2h
- }
- fi
-
- if cmds_exist lsblk; then
- alias lsblk="lsblk -o 'NAME,UUID,PARTUUID,SIZE,TYPE,SCHED,FSTYPE,LABEL,MOUNTPOINT'"
- fi
-
- if cmds_exist git; then
- alias git-reset-hard="git fetch origin; git reset --hard origin/master; git clean -f"
- fi
-
- alias ssh-remove-host-key="ssh-keygen -f "/home/natrinicle/.ssh/known_hosts" -R"
- alias ssh-stop-multiplex="ssh -O stop"
- alias extract-jpg-from-dngs="for file in *.dng; do exiftool -b -JpgFromRaw $file > $file.jpg; done"
-
- if cmds_exist pwgen; then
- alias genpass="pwgen -1yBn"
- fi
-
- if cmds_exist dig; then
- alias dig-short="dig +short"
- fi
-
- alias load="uptime | grep -Eo '([0-9]+\.[0-9]+,? ?){3}'"
-
- alias pip="/usr/bin/env pip"
- alias pip3="/usr/bin/env pip3"
-
- scp_home_rc () {
- scp ${@:2} .bashrc ${1}:~
- scp ${@:2} .bash_aliases ${1}:~
- scp ${@:2} .vimrc ${1}:~
- }
-
- # Add a path to $PATH if it's not already in there
- # pathmunge /path/to/add
- pathmunge () {
- if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
- if [ "$2" = "after" ] ; then
- PATH=$PATH:$1
- else
- PATH=$1:$PATH
- fi
- fi
- }
-
- if cmds_exist ipmitool ; then
- export IPMI_USER="ADMIN"
- export IPMI_PASS="ADMIN"
-
- # Set boot device on next boot to pxe
- ipmipxe() {
- ipmitool -I lanplus -H ${1} -U ADMIN -P ADMIN chassis bootdev pxe
- }
- # Reset a hung bmc
- ipmiresetbmc() {
- ipmitool -I lanplus -H ${1} -U ADMIN -P ADMIN mc reset cold
- }
- # Power cycle a server from it's bmc
- ipmipowercycle() {
- ipmitool -I lanplus -H ${1} -U ADMIN -P ADMIN chassis power cycle
- }
- # Ipmi wrapper with username/password for any other commands
- ipmi() {
- # ${@:2} pulls in all argv from 2 to the end of the line
- ipmitool -I lanplus -H ${1} -U ${IPMI_USER} -P ${IPMI_PASS} ${@:2}
- }
- fi
-
- if cmds_exist pv dd; then
- dd-progress() {
- sudo dd if=${1} ${@:3} | pv -tpreb | sudo dd of=${2} ${@:3}
- }
- fi
-
- if [ -d /sys/devices/platform/applesmc.768 ]; then
- fanspeed() {
- # Pulled from http://ubuntuforums.org/showthread.php?t=1645913&p=10246215#post10246215
- # Requires applesmc-dkms package and modprobe applesmc
- # Included in kernels >=2.6.27
- service mbpfan stop
- echo 1 | sudo tee /sys/devices/platform/applesmc.768/fan1_manual > /dev/null
- echo 1 | sudo tee /sys/devices/platform/applesmc.768/fan2_manual > /dev/null
- echo $1 | sudo tee /sys/devices/platform/applesmc.768/fan1_output > /dev/null
- echo $1 | sudo tee /sys/devices/platform/applesmc.768/fan2_output > /dev/null
- }
- fi
-
- extract () {
- if [ -f $1 ] ; then
- case $1 in
- *.tar.bz2) tar xjf $1 ;;
- *.tar.gz) tar xzf $1 ;;
- *.bz2) bunzip2 $1 ;;
- *.rar) unrar e $1 ;;
- *.gz) gunzip $1 ;;
- *.tar) tar xf $1 ;;
- *.tbz2) tar xjf $1 ;;
- *.tgz) tar xzf $1 ;;
- *.zip) unzip $1 ;;
- *.Z) uncompress $1 ;;
- *.7z) 7z x $1 ;;
- *) echo "'$1' cannot be extracted via extract()" ;;
- esac
- else
- echo "'$1' is not a valid file"
- fi
- }
-
- if cmds_exist ldapsearch; then
- export LDAP_USER="nate.bohman"
-
- ldapmultisearch() {
- if [ -z $LDAP_USER ]; then
- read -p "Username: " LDAP_USER
- export LDAP_USER
- fi
- read -s -p "${LDAP_USER}'s Password: " LDAP_PASSWORD
- echo ""
- for uid in "${@}"; do
- ldapsearch -x -ZZ -D "${LDAP_USER}@purestorage.com" -b "ou=PSUsers,dc=purestorage,dc=com" -h ldap.purestorage.com -w ${LDAP_PASSWORD} uid=${uid}
- if [ ${?} != 0 ]; then
- echo "ldapsearch had an error"
- unset LDAP_PASSWORD
- break
- fi
- done
- export LDAP_PASSWORD
- }
-
- ldapfindteam() {
- LDAP_SEARCH="$(ldapmultisearch ${1})"
- MANAGER_DN=$(echo "${LDAP_SEARCH}" | grep -Po "(?<=^manager: ).*$")
- MANAGER_NAME=$(echo "${LDAP_SEARCH}" | grep -Po "(?<=^name: ).*$")
- MANAGER_TITLE=$(echo "${LDAP_SEARCH}" | grep -Po "(?<=^title: ).*$")
- TEAM_UIDS=$(ldapsearch -x -ZZ -D "${LDAP_USER}@purestorage.com" -b "ou=PSUsers,dc=purestorage,dc=com" -h ldap.purestorage.com -w ${LDAP_PASSWORD} "manager=${MANAGER_DN}" | grep -Po "(?<=^uid: ).*")
- echo "Manager: ${MANAGER_NAME} - ${MANAGER_TITLE}"
- for character in $(echo "Manager: ${MANAGER_NAME} - ${MANAGER_TITLE}" | wc -c); do echo -e "-"; done
- ldapmultisearch ${TEAM_UIDS} | grep -Po "(?<=^name: ).*$"
- }
- fi
-
- if cmds_exist curl; then
- test_redirect() {
- if [ $(echo $2 | cut -d":" -f1) == "https" ]; then
- port=443
- else
- port=80
- fi
- domain=$(echo $2 | cut -d"/" -f3)
- # echo "IP $1"
- # echo "Port $port"
- # echo "Domain $domain"
- # echo "URL $2"
- echo "Testing $2 against $1:$port"
-
- curl --resolve $domain:$port:$1 $2 --progress-bar --insecure -sL -w "%{http_code} %{url_effective}\\n" -o /dev/null
- }
- fi
-
- psgrep() {
- ps auxw | grep "$(echo ${@} | sed 's/^\(.\)/[\1]/')"
- }
- ps_forest_pid () {
- ps --forest -o user,pid,pcpu,pmem,vsz,rss,tty,stat,start,time,cmd -g $(ps -o sid= -p ${1})
- }
- psmem() {
- # Print the number of processes matching name given, memory per process, and total memory usage
- ps -C $1 -O rss | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =",count; print "Memory usage per process =",sum/1024/count, "MB"; print "Total memory usage =", sum/1024, "MB" ;};'
- }
- lsof_proc_open_cons () {
- lsof -c ${1} -a -i${2}
- }
- memused() {
- # Get ram total, free, and used without cached/buffers then calculate the percentage used to the nearest whole number
- mem_total=$(cat /proc/meminfo | egrep "^MemTotal:" | egrep -o "[0-9]+")
- mem_free=$(echo $(cat /proc/meminfo | egrep "^MemFree:" | egrep -o "[0-9]+") + $(cat /proc/meminfo | egrep "^Cached:" | egrep -o "[0-9]+") | bc)
- mem_used=$(echo ${mem_total} - ${mem_free} | bc)
- percent_used=$(awk "BEGIN { pc=${mem_used}/${mem_total}*100; i=int(pc); print (pc-i<0.5)?i:i+1 }")
-
- # Output as used/total (percent%)
- echo -e "${mem_used}/${mem_total} (${percent_used}%)"
- }
- swapused() {
- # Get swap total, free, and used then calculate the percentage used to the nearest whole number
- swap_total=$(cat /proc/meminfo | egrep "^SwapTotal:" | egrep -o "[0-9]+")
- swap_free=$(cat /proc/meminfo | egrep "^SwapFree:" | egrep -o "[0-9]+")
- swap_used=$(echo ${swap_total} - ${swap_free} | bc)
- percent_used=$(awk "BEGIN { pc=${swap_used}/${swap_total}*100; i=int(pc); print (pc-i<0.5)?i:i+1 }")
-
- # Output as used/total (percent%)
- echo -e "${swap_used}/${swap_total} (${percent_used}%)"
- }
-
- suspend_process_soft() {
- for pid in $(psgrep $1 | awk '{print $2}'); do
- kill -TSTP ${pid}
- done
- }
-
- suspend_process_hard() {
- for pid in $(psgrep $1 | awk '{print $2}'); do
- kill -STOP ${pid}
- done
- }
-
- unsuspend_process() {
- for pid in $(psgrep $1 | grep -P " Tl? " | awk '{print $2}'); do
- kill -CONT ${pid}
- done
- }
-
- zram_stat() {
- for i in /sys/block/zram*; do
- if [ -f $i/compr_data_size ] && [ -f $i/orig_data_size ]; then
- compr=$(< $i/compr_data_size)
- orig=$(< $i/orig_data_size)
- ratio=0
- if [ $compr -gt 0 ]; then
- ratio=$(echo "scale=2; $orig*100/$compr" | bc -q)
- fi
- echo -e "/dev/${i/*\/}:\t$ratio% ($orig -> $compr)"
- fi
- done
- }
-
- ksm_stat() {
- # offered = pages_sharing + pages_shared + pages_unshared + pages_volatile;
- # saved = pages_sharing - pages_shared;
- # savings (ksm_ratios) = saved / offered;
-
- page_size=$(getconf PAGE_SIZE)
-
- pages_shared=$(cat /sys/kernel/mm/ksm/pages_shared)
- pages_sharing=$(cat /sys/kernel/mm/ksm/pages_sharing)
- pages_unshared=$(cat /sys/kernel/mm/ksm/pages_unshared)
- pages_volatile=$(cat /sys/kernel/mm/ksm/pages_volatile)
-
- offered=$(echo "${pages_sharing} + ${pages_shared} + ${pages_unshared} + ${pages_volatile}" | bc -l)
- offered_bytes=$(echo "${offered} * ${page_size}" | bc -l)
- offered_human="$(echo "${offered_bytes}" | numfmt --to=iec-i)B"
- saved=$(echo "${pages_sharing} - ${pages_shared}" | bc -l)
- saved_bytes=$(echo "${saved} * ${page_size}" | bc -l)
- saved_human="$(echo "${saved_bytes}" | numfmt --to=iec-i)B"
- saved_percent="0.00"
-
- if [ ${offered} -gt 0 ]; then
- saved_percent=$(python -c "print(round(float($(echo "${saved} / ${offered} * 100" | bc -l)), 2))")
- fi
-
- echo -e "${saved_human}/${offered_human} (${saved_percent}%)"
- }
-
- if cmds_exist dkms; then
- if cmds_exist sudo; then
- DKMS_CMD="sudo dkms"
- else
- DKMS_CMD="dkms"
- fi
-
- dkms-buildall() {
- $DKMS_CMD status | sed s/,//g | awk '{print "-m",$1,"-v",$2}' | while read line; do ls /var/lib/initramfs-tools | xargs -n 1 $DKMS_CMD install $line -k; done
- }
- fi
-
- if cmds_exist sudo; then
- function check_open_files () {
- alert_percent=${1:-0.75}
- for running_pid in $(sudo ps aux | awk '{print $2}' | tail -n+2); do
- if sudo test -d /proc/${running_pid}/fd && sudo test -f /proc/${running_pid}/limits; then
- open_files=$(sudo ls -l /proc/${running_pid}/fd | wc -l)
- open_limit=$(sudo grep "open files" /proc/${running_pid}/limits | awk '{print $4}')
- alert_limit=$(echo "((${open_limit} * ${alert_percent}) + 0.5) / 1" | bc)
- if [ ${open_files} -gt ${alert_limit} ]; then
- echo "PID ${running_pid} has ${open_files} open files with a limit of ${open_limit} max"
- fi
- fi
- done
- }
- fi
-
- if cmds_exist sudo wg; then
- alias wg="sudo /usr/bin/wg"
- fi
- if cmds_exist sudo wg-quick; then
- alias wg-quick="sudo /usr/bin/wg-quick"
- fi
-
- if cmds_exist nc; then
- function port_open_check () {
- nc -zv ${@}
- }
- fi
-
- if cmds_exist youtube-dl; then
- alias youtube-dl-best="youtube-dl -f bestvideo+bestaudio"
- fi
-
- function dedup_text () {
- # Can use inline or with a filename
- # echo -e "Test\nTest\nTest\nTest2\nTest" | dedup_text
- # dedup_text /var/log/badly_duplicated_log > /var/log/deduplicated_log
-
- # Stolen from https://stackoverflow.com/questions/1444406/how-can-i-delete-duplicate-lines-in-a-file-in-unix
- # seen is an associative-array that Awk will pass every line of the file to.
- # If a line isn't in the array then seen[$0] will evaluate to false.
- # The ! is a logical NOT operator and will invert the false to true.
- # Awk will print the lines where the expression evaluates to true.
- # The ++ increments seen so that seen[$0] == 1 after the first time a
- # line is found and then seen[$0] == 2, and so on.
- # Awk evaluates everything but 0 and "" (empty string) to true.
- # If a duplicate line is placed in seen then !seen[$0] will evaluate to
- # false and the line will not be written to the output.
- awk '!seen[$0]++'
- }
-
- function escape_string () {
- # string=${@}
- # echo ${string//\\/\\\\}
- printf "%q" "${@}"
- }
-
- function round () {
- round_float=${1}
- round_places=${2:-2}
-
- python -c "print(round(float(${round_float}), ${round_places}))"
- }
-
- function percent_change () {
- old_number=${1}
- new_number=${2}
-
- if [ $(echo "${new_number} < ${old_number}" | bc -l) -eq 1 ]; then
- percent_decrease=$(round $(echo "((${old_number} - ${new_number}) / ${old_number}) * 100" | bc -l) 2)
- echo "${old_number} -> ${new_number} is a ${percent_decrease}% decrease"
- else
- percent_increase=$(round $(echo "((${new_number} - ${old_number}) / ${old_number}) * 100" | bc -l) 2)
- echo "${old_number} -> ${new_number} is a ${percent_increase}% increase"
- fi
- }
-
- # add thefuck function
- #eval $(thefuck --alias)
- function fuck () {
- TF_PREVIOUS=$(fc -ln -1);
- TF_CMD=$(
- TF_ALIAS=fuck
- TF_SHELL_ALIASES=$(alias)
- PYTHONIOENCODING=utf-8
- thefuck $TF_PREVIOUS THEFUCK_ARGUMENT_PLACEHOLDER $@
- ) && eval $TF_CMD;
- history -s $TF_CMD;
- }
-
- if cmds_exist ffmpeg; then
- convert_video_to_webm () {
- filename=${1}
- filename_wo_ext=$(echo ${filename} | rev | cut -d"." -f2- | rev)
-
- # First pass
- ffmpeg -i ${filename} -c:v libvpx-vp9 -pass 1 -b:v 0 -crf 21 -threads 8 -speed 4 -tile-columns 6 -frame-parallel 1 -an -f webm -y /dev/null
-
- # Second pass
- ffmpeg -i ${filename} -c:v libvpx-vp9 -pass 2 -b:v 0 -crf 21 -threads 8 -speed 2 -tile-columns 6 -frame-parallel 1 -auto-alt-ref 1 -lag-in-frames 25 -c:a libopus -b:a 96k -f webm ${filename_wo_ext}.webm
- }
-
- stabilize_video () {
- filename=${1}
- filename_wo_ext=$(echo ${filename} | rev | cut -d"." -f2- | rev)
- filename_ext=$(echo ${filename} | rev | cut -d"." -f1 | rev)
- shakiness=${2:-5}
- accuracy=${3:-15}
- smoothing=${4:-30}
-
- # First pass
- ffmpeg -i ${filename} -vf vidstabdetect=shakiness=${shakiness}:accuracy=${accuracy} -f null -
-
- # Second pass
- ffmpeg -i ${filename} -vf vidstabtransform=smoothing=${smoothing}:input="transforms.trf" ${filename_wo_ext}.stabilized.${filename_ext}
- }
- if cmds_exist flac; then
- convert_flac_to_mp3 () {
- filename=${1}
- filename_wo_ext=$(echo ${filename} | rev | cut -d"." -f2- | rev)
-
- flac --silent --decode --stdout "${filename}" | ffmpeg -i - -map 0:a:0 -b:a 320k -v 0 -f mp3 "${filename_wo_ext}.mp3"
- }
- fi
- fi
-
- # Nuitka converts Python source into C
- if cmds_exist nuitka; then
- # Convert and compile all .py files in current directory to modules
- function nuitka_compile_all_as_modules() {
- for py_file in *.py; do
- # Ignore things like __init__.py that tend to break
- if echo ${py_file} | grep -qv "^__"; then
- nuitka --module ${py_file} --recurse-none --show-progress
- fi
- done
- }
-
- # Undo Nuitka module conversion and compilation
- function nuitka_remove_all_compiled_modules() {
- rm -r *.build
- rm *.so
- rm *.pyi
- }
- fi
|