Skip to content

Instantly share code, notes, and snippets.

@benyanke
Last active December 18, 2016 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benyanke/feff2d9f9153a5fe18574ff6623f27ec to your computer and use it in GitHub Desktop.
Save benyanke/feff2d9f9153a5fe18574ff6623f27ec to your computer and use it in GitHub Desktop.
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# 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, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
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
### Custom Bashrc info
# Temp
alias syncdb="/home/benyanke/git/dbProject/sync.sh"
# Start nautilus in current dir
alias n="echo Starting Nautilus in $(pwd) ; nautilus $(pwd) &>/dev/null"
# Matrix fun bash thing
alias matrix="cmatrix"
alias hacker="hollywood"
# Network manage
# alias netwatch="speedometer -r enp0s25"
alias netwatch="speedometer -r "
# Docker
alias docker="sudo docker"
# function back() {
# &>/dev/null &
# }
function wgetall() {
wget $1 -r -nd --delete-after
}
## PYTHON
alias pip="sudo -H pip"
alias vvs="virtualenv venv"
alias vva="source venv/bin/activate"
alias vvd="deactivate"
alias netdownup="sudo service networking restart; sudo service network-manager restart"
alias netdownupc="netdownup && isup"
alias netdownupce="netdownup; isup && exit"
alias fixnet="isupnow || netdownup; isup && exit"
alias idownup="sudo service networking restart && sudo service network-manager restart"
alias idownupc="idownup ; isup"
alias idownupce="idownup ; isup ; exit"
alias isupe="isup; exit"
function isup() {
until isupnow
do
sleep 0.25;
done
}
function isupnow() {
case "$(curl -s --max-time 2 -I https://google.com | sed 's/^[^ ]* *\([0-9]\).*/\1/; 1q')" in
[23]) printf "\n\n## HTTP connectivity is up ##\n\n"; return 0;;
5) printf "The web proxy won't let us through"; return 1;;
*) printf "."; return 1;;
esac
}
alias chrome="google-chrome"
alias o="xdg-open"
alias open="xdg-open"
# GIT functions
alias gitfixrepo="git reset --hard origin/master && git pull"
alias gitp="git pull"
alias gitg="git log --oneline --decorate --all --graph"
cd() {
# Seconds between alerts: raise this
# number if you want to get fewer alerts
# Default: 10 min
alertDelay=300
# Alert text: this is displayed when you
# CD into a git repo
alertText="\n##################################################\n This is a git repo. Don't forget to 'git pull'\n##################################################\n"
######
## Nothing to change beyond here
######
if [ "$1" == "" ]; then
# Run CD command
builtin cd
else
# Run CD command
builtin cd "$1"
fi
# Check if current dir is repo
git status >/dev/null 2>&1
if [ $? == 0 ]; then
# Make sure there's at least one commit before continuing
git rev-parse HEAD &> /dev/null
if [ $? != 0 ]; then
return
fi
# If current dir is a git repo,
# then set up tools for alert
repoRoot="$(git rev-parse --show-toplevel)"
# This is a repository identifer
# used to track when alerts are
# displayed for each repo.
repoId="$(git rev-list --parents HEAD | tail -1)"
# This is the file containing the
# Unix timestamp of the last alert
statusFile="/tmp/git-cd-alerter/$repoId"
# Get the unix timestamp
timestamp=$(date +%s)
if [ -s $statusFile ]; then
fileTime=$(cat $statusFile);
timeDiff=`expr $timestamp - $fileTime`
if [ "$timeDiff" -gt "$alertDelay" ]; then
echo -e $alertText;
echo $timestamp > /tmp/git-cd-alerter/$repoId
fi
else
echo -e $alertText;
mkdir -p /tmp/git-cd-alerter/
echo $timestamp > /tmp/git-cd-alerter/$repoId
fi
else
# If not, exit function now
return
fi
}
alias gitroot='cd $(git rev-parse --show-cdup) >/dev/null 2>&1'
gitcom () {
git add . &&
git commit -m "$1" &&
git push
}
mkdircd() {
mkdir $1;
cd $1;
}
alias aptu="sudo apt update && sudo apt install -f -y && sudo apt upgrade -y && sudo apt dist-upgrade -y && sudo apt autoremove -y"
alias aptue="aptu && exit; echo there was an error"
apti() {
if [ -n "$1" ]; then
sudo apt update &&
sudo apt install -y $1
else
echo "Please list program(s) to install";
fi
}
# restart display manager
alias restartdm="sudo service lightdm restart"
alias restartaudio="pulseaudio -k && sudo alsa force-reload"
# VPN Control
alias startvpn="sudo nohup /home/benyanke/scripts/vpn/startVpn.sh &"
# PATH modifications
export PATH=$PATH:$HOME/bin
export PATH=$PATH:/opt/bin
export PATH=$PATH:~/git/bin
export PATH=$PATH:~/NextCloud/config-sync-files/bin-sync/bin
alias nethogs="sudo nethogs"
# Server Stuff
# "reload webserver"
alias relwebsv="sudo service apache2 reload"
# Font Stuff
alias fontsreload="sudo fc-cache -fv; fc-cache -fv"
# General stuff
alias bashreload="source ~/.bashrc && echo Bash config reloaded"
function nanobk() {
echo "You are making a copy of $1 before you open it. Press enter to continue."
read nul
cp $1 $1.bak
nano $1
}
# Mount and unmount network drives
alias mountnas="sudo ~/scripts/networkshares/mountNasDrives.sh"
alias unmountnas="sudo ~/scripts/networkshares/unmountNasDrives.sh"
# Clear DNS Cache
alias flushdns="sudo /etc/init.d/dns-clean && echo DNS cache flushed"
alias edithosts="sudo nano /etc/hosts && echo Hosts file edited && flushdns"
alias bwtest="iperf3 -c iperf.scottlinux.com; wait 1; iperf3 -c iperf.scottlinux.com -R"
# Get IPs associated with this site
### Work to dynamically list all interfaces. Will add later
function myip()
{
echo ""
ip addr | awk '
/^[0-9]+:/ {
sub(/:/,"",$2); iface=$2 }
/^[[:space:]]*inet / {
split($2, a, "/")
print " " iface" : "a[1]
}'
echo ""
extIp=$(dig +short myip.opendns.com @resolver1.opendns.com)
loc=$(curl -sb -H https://ipcheck.benyanke.com/whereami.php)
echo " WAN : $extIp"
echo " Location : $loc"
echo ""
}
function myip-original()
{
extIp=$(dig +short myip.opendns.com @resolver1.opendns.com)
printf "Wireless IP: "
MY_IP=$(/sbin/ifconfig wlp4s0 | awk '/inet/ { print $2 } ' |
sed -e s/addr://)
echo ${MY_IP:-"Not connected"}
printf "Wired IP: "
MY_IP=$(/sbin/ifconfig enp0s25 | awk '/inet/ { print $2 } ' |
sed -e s/addr://)
echo ${MY_IP:-"Not connected"}
echo ""
echo "WAN IP: $extIp"
}
# Syntax: "repeat [X] [command]"
function repeat()
{
local i max
max=$1; shift;
for ((i=1; i <= max ; i++)); do # --> C-like syntax
eval "$@";
done
}
function repeatu() {
eval $@
while [ $? != 0 ]; do
eval $@
sleep 0.5
done
}
# Make some of the file manipulation programs verbose
alias mv="mv -v"
alias rm="rm -vi"
alias cp="cp -v"
# Shortcuts for various ls options. All of these add colors, ignore files
# ending with '~', and add '/' to the end of folders. Shortcuts ending
# with 'a' show hidden files as well, except the redundant '.' and '..'.
alias ls="ls -pB --color"
alias la="ls -pA --color"
alias ll="ls -phl --color" # long-form list
alias lla="ls -phlA --color"
alias lz="ls -pshS --color" # sort by file size
alias lza="ls -pshSA --color"
alias lt="ls -pghotr --color" # long-form, sort by modification date
alias lta="ls -pghotrA --color"
alias lx="ls -pghoX --color" # sort by file extention
alias lxa="ls -pghoXA --color"
alias lr="ls -ARpsh --color" # recursive list (watch out! It will fill your screen!)
# Prints disk usage in human readable form
alias d="du -sh"
# Removes transparency when displaying an image
alias show="display -flatten"
# Clear the screen of your clutter
alias c="clear"
alias cl="clear;ls;pwd"
# GREP Motifications
alias grep="grep --color"
alias grepp="grep -P --color"
# Json tools (pipe stuff to these)
alias json="python -m json.tool"
alias jsonf="python -m json.tool"
# Edit shortcuts for config files
alias sshconfig="${EDITOR:-nano} ~/.ssh/config"
alias bashrc="${EDITOR:-nano} +120 ~/.bashrc && source ~/.bashrc && echo Bash config edited and reloaded."
# SSH helper
alias sshclear="rm ~/.ssh/multiplex/* -f && echo SSH connection cache cleared"
alias sshlist="echo Currently open ssh connections && echo && l ~/.ssh/multiplex/"
function sshl() {
clear;
echo "SSH Host listing:";
awk '
$1 == "Host" {
host = $2;
printf "\n%s", host;
next;
}
$1 == "HostName" {
$1 = "";
sub( /^[[:space:]]*/, "" );
printf " : %s", $0;
}
$1 == "Port" {
$1 = "";
sub( /^[[:space:]]*/, "" );
printf " : %s", $0;
}
' ~/.ssh/config
echo ""
}
# sshuttle helpers
# set default SSH server:
# user@hostname or a host in ~/.ssh/config
# tunnel all traffic including DNS
# alias tnl="sshuttle --dns -vr $TNL_SERVER 0/0"
alias tnlold="sshuttle --dns -vr $TNL_SERVER 0/0"
function tnl() {
if [ -z "$1" ]; then
TNL_SERVER=SSH-HOSTNAME-HERE
else
TNL_SERVER=$1;
fi
sudo sshuttle --dns -vr $TNL_SERVER 0/0
# sshuttle --dns -vr $TNL_SERVER 0/0
}
# returns a list of IP addresses from given domain(s).
#
# Examples:
# dns2ip google.com
# dns2ip netflix.com movies.netflix.com
#
function dns2ip() {
dig +short $* | sed "/[^0-9\.]/d" # use sed to remove non-IPv4 line e.g. alias
}
# tunnel specified domain(s) only.
#
# Examples:
# tnlonly google.com
# tnlonly netflix.com movies.netflix.com
#
function tnlonly() {
sshuttle -vr $TNL_SERVER `dns2ip $*`;
}
# tunnel all traffic including DNS, except the specified domain(s).
#
# Examples:
# tnlbut youku.com
# tnlbut youku.com weibo.com
#
function tnlbut() {
sshuttle --dns -vr $TNL_SERVER `dns2ip $* | sed "s/^/-x/"` 0/0; # use sed to append '-x' prefix
}
# vpn to a ssh server.
#
# Examples:
# vpnto my_office_server # host in ~/.ssh/config
# vpnto user@123.123.123.123
#
function vpnto() {
# sshuttle -HNvr $1;
sudo sshuttle -HNvr $1;
}
# source ~/.autoenv/activate.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment