Skip to content

Instantly share code, notes, and snippets.

@carlospolop

carlospolop/ps1.bash

Last active Oct 24, 2019
Embed
What would you like to do?
Prompt with CPU&RAM info, jobstatus, last command, user, hostname, current path & several useful aliases
# System-wide .bashrc file for interactive bash(1) shells.
# To enable the settings / commands in this file for login shells as well,
# this file has to be sourced in /etc/profile.
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, overwrite the one in /etc/profile)
# but only if not SUDOing and have SUDO_PS1 set; then assume smart user.
if ! [ -n "${SUDO_USER}" -a -n "${SUDO_PS1}" ]; then
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
# PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
# ;;
#*)
# ;;
#esac
# enable bash completion in interactive shells
#if ! shopt -oq posix; then
# if [ -f /usr/share/bash-completion/bash_completion ]; then
# . /usr/share/bash-completion/bash_completion
# elif [ -f /etc/bash_completion ]; then
# . /etc/bash_completion
# fi
#fi
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: command not found\n" "$1" >&2
return 127
fi
}
fi
#Based on https://gist.github.com/loudambiance/a41b42a4295bce6e7304
##########################################################
#Please edit "User Configuration" section before using #
##########################################################
#=========================================================
#Terminal Color Codes
#=========================================================
WHITE='\[\033[1;37m\]'
LIGHTGRAY='\[\033[1;37m\]'
GRAY='\[\033[1;30m\]'
BLACK='\[\033[1;30m\]'
RED='\[\033[1;31m\]'
LIGHTRED='\[\033[1;31m\]'
GREEN='\[\033[1;32m\]'
LIGHTGREEN='\[\033[1;32m\]'
BROWN='\[\033[1;33m\]' #Orange
YELLOW='\[\033[1;33m\]'
BLUE='\[\033[1;34m\]'
LIGHTBLUE='\[\033[1;34m\]'
PURPLE='\[\033[1;35m\]'
PINK='\[\033[1;35m\]' #Light Purple
CYAN='\[\033[1;36m\]'
LIGHTCYAN='\[\033[1;36m\]'
DEFAULT='\[\033[0m\]'
#=========================================================
# User Configuration
#=========================================================
# Colors
cLINES=$GRAY #Lines and Arrow
cBRACKETS=$GRAY # Brackets around each data item
cERROR=$LIGHTRED # Error block when previous command did not return 0
cCPURAM=$LIGHTGRAY # The current time
cMPX1=$YELLOW # Color for terminal multiplexer threshold 1
cMPX2=$RED # Color for terminal multiplexer threshold 2
cBGJ1=$YELLOW # Color for background job threshold 1
cBGJ2=$RED # Color for background job threshold 2
cSTJ1=$YELLOW # Color for background job threshold 1
cSTJ2=$RED # Color for background job threshold 2
cSSH=$PINK # Color for brackets if session is an SSH session
cUSR=$LIGHTBLUE # Color of user
cUHS=$GRAY # Color of the user and hostname separator, probably '@'
cHST=$LIGHTGREEN # Color of hostname
cRWN=$RED # Color of root warning
cPWD=$BLUE # Color of current directory
cCMD=$DEFAULT # Color of the command you type
# Enable block
eNL=1 # Have a newline between previous command output and new prompt
eERR=1 # Previous command return status tracker
eMPX=1 # Terminal multiplexer tracker enabled
eSSH=1 # Track if session is SSH
eBGJ=1 # Track background jobs
eSTJ=1 # Track stopped jobs
eUSH=1 # Show user and host
ePWD=1 # Show current directory
# Block settins
MPXT1="0" # Terminal multiplexer threshold 1 value
MPXT2="2" # Terminal multiplexer threshold 2 value
BGJT1="0" # Background job threshold 1 value
BGJT2="2" # Background job threshold 2 value
STJT1="0" # Stopped job threshold 1 value
STJT2="2" # Stopped job threshold 2 value
UHS="${cBRACKETS}@${cLINES}"
SEP="${cBRACKETS}|${cLINES}"
function promptcmd()
{
PREVRET=$?
#=========================================================
#check if user is in ssh session
#=========================================================
if [[ $SSH_CLIENT ]] || [[ $SSH2_CLIENT ]]; then
lSSH_FLAG=1
else
lSSH_FLAG=0
fi
#=========================================================
# Start a new prompt
#=========================================================
PS1=""
#=========================================================
# First Dynamic Block - Previous Command Error
#=========================================================
if [ $PREVRET -ne 0 ] ; then
PS1="${PS1}${cBRACKETS}[${cERROR}:(${cBRACKETS}]${cLINES}\342\224\200"
fi
#================================================================
# Second dynamic block - CPU Usage | RAM Usage | FileSystem Space
#================================================================
CPU="$(grep 'cpu ' /proc/stat | awk '{print ($2+$4)*100/($2+$4+$5)}' | cut -d "." -f1)"
CPUc="$(echo -e '\u1d9c')"
if [ "$CPU" -gt "60" ]; then CPU_B="1"; else unset CPU_B 2>/dev/null; fi
RAM="$(free | grep Mem | awk '{print $3/$2 * 100.0}' | cut -d '.' -f1)"
RAMr="$(echo -e '\u1d3f')"
if [ "$RAM" -gt "60" ]; then RAM_B="1"; else unset RAM_B 2>/dev/null; fi
FS="$(df / | tail -n 1 | grep -Po "\d+%" | cut -d "%" -f1)"
FSs="$(echo -e '\u02E2')"
if [ "$FS" -gt "75" ]; then FS_B="1"; else unset FS_B 2>/dev/null; fi
if [ "$CPU_B" ] || [ "$RAM_B" ] || [ "$FS_B" ]; then
PS1="${PS1}${cBRACKETS}["
if [ "$CPU_B" ]; then
PS1="${PS1}${cCPURAM}$CPUc$CPU%"
if [ "$RAM_B" ] || [ "$FS_B" ]; then
PS1="${PS1}${SEP}"
fi
fi
if [ "$RAM_B" ]; then
PS1="${PS1}${cCPURAM}$RAMr$RAM%"
if [ "$FS_B" ]; then
PS1="${PS1}${SEP}"
fi
fi
if [ "$FS_B" ]; then
PS1="${PS1}${cCPURAM}$FSs$FS%"
fi
PS1="${PS1}${cBRACKETS}]${cLINES}\342\224\200"
fi
#=========================================================
# Detached Screen Sessions
#=========================================================
hTMUX=0
hSCREEN=0
MPXC=0
hash tmux --help 2>/dev/null || hTMUX=1
hash screen --version 2>/dev/null || hSCREEN=1
if [ $hTMUX -eq 0 ] && [ $hSCREEN -eq 0 ] ; then
MPXC=$(echo "$(screen -ls | grep -c -i detach) + $(tmux ls 2>/dev/null | grep -c -i -v attach)" | bc)
elif [ $hTMUX -eq 0 ] && [ $hSCREEN -eq 1 ] ; then
MPXC=$(tmux ls 2>/dev/null | grep -c -i -v attach)
elif [ $hTMUX -eq 1 ] && [ $hSCREEN -eq 0 ] ; then
MPXC=$(screen -ls | grep -c -i detach)
fi
if [[ $MPXC -gt $MPXT2 ]] ; then
PS1="${PS1}${cBRACKETS}[${cMPX2}\342\230\220:${MPXC}${cBRACKETS}]${cLINES}\342\224\200"
elif [[ $MPXC -gt $MPXT1 ]] ; then
PS1="${PS1}${cBRACKETS}[${cMPX1}\342\230\220:${MPXC}${cBRACKETS}]${cLINES}\342\224\200"
fi
#=========================================================
# Backgrounded running jobs
#=========================================================
BGJC=$(jobs -r | wc -l )
if [ $BGJC -gt $BGJT2 ] ; then
PS1="${PS1}${cBRACKETS}[${cBGJ2}&:${BGJC}${cBRACKETS}]${cLINES}\342\224\200"
elif [ $BGJC -gt $BGJT1 ] ; then
PS1="${PS1}${cBRACKETS}[${cBGJ1}&:${BGJC}${cBRACKETS}]${cLINES}\342\224\200"
fi
#=========================================================
# Stopped Jobs
#=========================================================
STJC=$(jobs -s | wc -l )
if [ $STJC -gt $STJT2 ] ; then
PS1="${PS1}${cBRACKETS}[${cSTJ2}\342\234\227:${STJC}${cBRACKETS}]${cLINES}\342\224\200"
elif [ $STJC -gt $STJT1 ] ; then
PS1="${PS1}${cBRACKETS}[${cSTJ1}\342\234\227:${STJC}${cBRACKETS}]${cLINES}\342\224\200"
fi
#=========================================================
# Second Static block - User@host
#=========================================================
# set color for brackets if user is in ssh session
if [ $lSSH_FLAG -eq 1 ] ; then
sesClr="$cSSH"
else
sesClr="$cBRACKETS"
fi
# change color to red if root
if [ $EUID -eq 0 ] ; then
PS1="${PS1}${sesClr}[${cRWN}\u${cUHS}${UHS}"
else
PS1="${PS1}${sesClr}[${cUSR}\u${cUHS}${UHS}"
fi
PS1="${PS1}${cHST}\h${sesClr}]${cLINES}\342\224\200"
#=========================================================
# Third Static Block - Current Directory
#=========================================================
PS1="${PS1}[${cPWD}\w${cBRACKETS}]"
#=========================================================
# Second Line
#=========================================================
if [ $EUID -eq 0 ] ; then
fnl="${cRWN}#${cLINES}"
else
fnl="${cUSR}\$${cLINES}"
fi
# Check number of "[", show 2 or 1 line prompt
if [ "$(echo $PS1 | grep -o '\[' | wc -l)" -gt "25" ]; then
PS1="${cLINES}\342\224\214\342\224\200${PS1}\n${cLINES}\342\224\224\342\224\200\342\224\200${fnl} ${cCMD}"
else
PS1="${PS1}${fnl} ${cCMD}"
fi
}
function load_prompt () {
# Get PIDs
local parent_process=$(cat /proc/$PPID/cmdline | cut -d \. -f 1)
local my_process=$(cat /proc/$$/cmdline | cut -d \. -f 1)
if [[ $parent_process == script* ]]; then
PROMPT_COMMAND=""
PS1="\t - \# - \u@\H { \w }\$ "
elif [[ $parent_process == emacs* || $parent_process == xemacs* ]]; then
PROMPT_COMMAND=""
PS1="\u@\h { \w }\$ "
else
export DAY=$(date +%A)
PROMPT_COMMAND=promptcmd
fi
export PS1 PROMPT_COMMAND
}
load_prompt 2>/dev/null
### ALIASES ###
if [ "`which grc`" ]; then
for cmd in g++ gas head make ld ping6 tail traceroute6 $( ls /usr/share/grc/ ); do
cmd="${cmd##*conf.}"
if ! [ "${cmd}" = "jobs" ] && ! [ "${cmd}" = "tcpdump" ]; then
type "${cmd}" > /dev/null 2>&1 && alias "${cmd}"="$( which grc ) --colour=auto ${cmd}"
fi
done
fi
if [ "`which rlwrap`" ]; then
alias "nc"="$( which rlwrap ) nc"
alias "telnet"="$( which rlwrap ) telnet"
fi
#https://blog.ropnop.com/docker-for-pentesters/
function dockershellhereubuntu() {
dirname=${PWD##*/}
docker run --rm -it --entrypoint=/bin/bash -v `pwd`:/${dirname} -w /${dirname} "ubuntu:latest"
}
function dockershellhere() {
dirname=${PWD##*/}
docker run --rm -it --entrypoint=/bin/bash -v `pwd`:/${dirname} -w /${dirname} "$@"
}
function dockershellshhere() {
dirname=${PWD##*/}
docker run --rm -it --entrypoint=/bin/sh -v `pwd`:/${dirname} -w /${dirname} "$@"
}
function nginxhere() {
docker run --rm -it -p 80:80 -p 443:443 -v "${PWD}:/srv/data" rflathers/nginxserve
}
function webdavhere() {
docker run --rm -it -p 80:80 -v "${PWD}:/srv/data/share" rflathers/webdav
}
function metasploit() {
docker run --rm -it -v "${HOME}/.msf4:/home/msf/.msf4" metasploitframework/metasploit-framework ./msfconsole "$@"
}
function metasploitports() {
docker run --rm -it -v "${HOME}/.msf4:/home/msf/.msf4" -p 8443-8500:8443-8500 metasploitframework/metasploit-framework ./msfconsole "$@"
}
function msfvenomhere() {
docker run --rm -it -v "${HOME}/.msf4:/home/msf/.msf4" -v "${PWD}:/data" metasploitframework/metasploit-framework ./msfvenom "$@"
}
alias pythonhttphere="python3 -m http.server $@"
alias reqdump='echo "IT WILL ISTEN IN PORT 80"; docker run --rm -it -p 80:3000 rflathers/reqdump'
alias postfiledumphere='echo "IT WILL ISTEN IN PORT 80"; docker run --rm -it -p80:3000 -v "${PWD}:/data" rflathers/postfiledump'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment