bashrc

Some parts of my bashrc file that I use to alias simple keystrokes into very powerful commands. Some I have borrowed from examples online. I use a mix of special functions and aliases to help increase my speed when trying to figure out an issue.

ENV

Setup basic user env information to help inform commonly used programs, paths, etc.

export SVN_EDITOR=vim
export PATH=/nfs/user_home/scripts:/nfs/user_home/bin:$PATH

Sign in

When I sign into a server I immediately like to see a report of basic system info/stats, and also know that my .bashrc file was correctly loaded

#this is at the very end of my ~/.bashrc file
cv() { cat /etc/redhat-release ;}
function machine()
{
 echo -e "\nMachine information:" ; uname -a; cv;
 echo -e "\nUsers logged on:" ; w -h
 echo -e "\nCurrent date :" ; date
 echo -e "\nMachine status :" ; uptime
 echo -e "\nMemory status :" ; free
 echo -e "\nFilesystem status :"; df -hl
}
echo "Welcome to $HOSTNAME, $USER"
echo "$(machine)"
echo "All Settings are a go! DO NOT PANIC!"

Set terminal colors

I have putty set to default white background with black letters, I find it much easier to read than the defaults, the color settings below are mostly for fun and show.

#Color
#Set variables for foreground colors
fgRed=$(tput setaf 2) ; fgGreen=$(tput setaf 2) ; fgBlue='\e[0;34m' ;
fgMagenta='\e[1;35m' ; fgYellow='\e[0;33m' ; fgCyan=$(tput setaf 5) ;
fgWhite='\e[1;37m' ; fgBlack='\e[0;90m' ; fgText='\e[0;90m' ;
#Set variables for background colors
bgRed='\e[1;101m' ; bgGreen='\e[0;32m' ; bgBlue='\e[0;34m' ;
bgMagenta='\e[0;35m' ; bgYellow='\e[1;33m' ; bgCyan='\e[1;36m' ;
bgWhite='\e[0m' ; bgBlack=$(tput setab 4) ;

#Set variables for font weight and text decoration
B=$(tput bold) ; U=$(tput smul) ; C=$(tput sgr0) ; X=$(tput smso);
#NOTE: ${C} clears the current formatting

if [[ $USER = "root" ]]; then
   PS1="\[\e[1;35m\]\A\[\e[m\] \[\e[1;34m\]\[email protected]\h >\[\e[m\] \[\e[0;31m\]"
else
   PS1="\[\033\$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\]|;)\"; else echo \"\[\033[01;31m\]|;(\"; fi) $(if [[ ${EUID} == 0 ]]; then echo '\[\033[1;35m\]\A\[\e[m\] \[\e[1;34m\]\[email protected]\h'; else echo '\[\033[1;35m\]\A\[\e[m\] \[\e[1;34m\]\[email protected]\h'; fi)\[\033[01;34m\] \w >\[\e[\033[0m\] "
   #add ${C} to line above to end background color

fi

|;) 08:20 [email protected] ~ > #if the last command returns anything other than exit(0) the green smiley face will turn into a red frowny face.

|;( 08:20 [email protected] ~ >

Test Mail

Test that sendmail is working on the server

testmail() { echo "test from $HOSTNAME"| mail -s "TEST MAIL"  [email protected] ;}

PANIC

Shut off the NIC if one believes the server to be compromised.

PANIC() {
 echo "You said to PANIC"
 echo "Sending alert"
 echo "Something bad has occured and $USER is intervening on $HOSTNAME \n \ 
 $USER is shutting off network access"| mail -s "$HOSTNAME has been taken \ 
 offline" [email protected]
 echo "Shutting off network access"
 ifdown -a

}

Try

try a command, and alert if failed

yell() { echo "$0: $*" >&2; echo "[email protected] failed with exit code $?"| \ 
       mail -s "TEST FAIL" [email protected] ;}
die() { yell "$*"; exit 111; }
try() { "[email protected]" || die "cannot $*"; }

 

Extract

Tired of remembering if you should gunzip or tar -xvzf a file, this takes away all of that confusion. I did not come up with this one, I found it here.

extract() {
    if [ -z "$1" ]; then
       # display usage if no parameters given
       echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
    else
       if [ -f $1 ] ; then
          NAME=${1%.*}
          mkdir $NAME && cd $NAME
          case $1 in
              *.tar.bz2) tar xvjf ../$1 ;;
              *.tar.gz) tar xvzf ../$1 ;;
              *.tar.xz) tar xvJf ../$1 ;;
              *.lzma) unlzma ../$1 ;;
              *.bz2) bunzip2 ../$1 ;;
              *.rar) unrar x -ad ../$1 ;;
              *.gz) gunzip ../$1 ;;
              *.tar) tar xvf ../$1 ;;
              *.tbz2) tar xvjf ../$1 ;;
              *.tgz) tar xvzf ../$1 ;;
              *.zip) unzip ../$1 ;;
              *.Z) uncompress ../$1 ;;
              *.7z) 7z x ../$1 ;;
              *.xz) unxz ../$1 ;;
              *.exe) cabextract ../$1 ;;
              *) echo "extract: '$1' - unknown archive method" ;;
               esac
        else
            echo "$1 - file does not exist"
        fi
  fi
}

 

alias list

# User specific aliases
mcd() { mkdir -pv $1 && cd $1 ;}
cls() { cd "$1"; ls; }
backup() { cp "$1"{,.bak};}
#keyboard mistypes
alias sl='ls'
alias ll='ls -lha'
alias kk=ll
alias xs='cd'
alias vf='cd'
#sudo access
alias sv='sudo vim'
alias sd='sudo'
alias do='sudo'
alias suu='sudo su -'
alias root=suu
#look back
alias c='clear'
alias h='history'
alias histg='history | grep'
alias hg=histg
#processes and files
alias ps?='ps aux | grep'
alias psf='ps fax'
alias findp='sudo find . -type f -mmin +720 -print'
alias finddel='sudo find . -type f -mmin +720 -delete'
alias cdl='cd "[email protected]" && ls -al'
alias cdh='cd ~'
alias ..='cd ..'
alias ...='cd ../../'
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias mkdir='mkdir -pv'
alias now='date +"%T"'
alias nowtime=now
alias nowdate='date +"%d-%m-%Y"'
#networking
alias ports='netstat -tulanp'
alias iptlist='sudo /sbin/iptables -L -n -v --line-numbers'
alias iptlistin='sudo /sbin/iptables -L INPUT -n -v --line-numbers'
alias iptlistout='sudo /sbin/iptables -L OUTPUT -n -v --line-numbers'
alias iptlistfw='sudo /sbin/iptables -L FORWARD -n -v --line-numbers'
alias firewall=iptlist
alias listen="sudo lsof -P -i -n"
alias lso80='sudo lsof -i :80'
alias lso443='sudo lsof -i :443'
alias lso25='sudo lsof -i :25'
alias lso3306='sudo lsof -i :3306'
alias lso5432='sudo lsof -i :5432'
alias cmount='mount | column -t'
#other
alias install='sudo yum install'
alias update='sudo yum update'
alias updatey='sudo yum -y update'
alias meminfo='free -m -l -t'
alias bashrc='vim ~/.bashrc && source ~/.bashrc'
alias get_alias='cat ~/.bashrc | grep alias'

alias fuck='shutdown -h now'
alias fucker='reboot'
alias busy='cat /dev/urandom | hexdump -C | grep "ca fe"'
alias genpasswd="strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 15 | tr -d '\n'; echo"
alias tree="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'"