#########################################################################################################
### ###
### Functies: ###
### ###
#########################################################################################################
# exit function to use in scripts , Usage: die "REASON"
function die() { echo "Error: ${1}" ; exit 1; }
# sudo handigheid, if no args do sudo su -
function sudo() {
command="$@"
if [ -z "$command" ]; then
command sudo su -
else
command sudo "$@"
fi
}
# find handigheid, if no args do find su -
function find() {
command="$@"
if [ -z "$command" ]; then
command find .
else
command find "$@"
fi
}
# kill firefox the hard way, in case of a crash
function killfox() { pkill firefox-bin || ps -ef |grep firefox |grep -v grep | awk '{print $2}' | while read line; do kill -9 $line; done }
# fix metacity in case of a crash
function fix() { metacity --replace & }
# show tree of the current directory
function tree() {
{ [ ${#} = 0 ] && find . -type d | sed -e 1d -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|-/'; }
{ [ ${#} != 0 ] && find ${@} -type d | sed -e 1d -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|-/'; }
}
# top 10 of most used commands
function top10() {
echo "top 10 commands:"
history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -nr | head
}
# wargames
function game() {
echo -e "Greetings, Professor Falken.\n\n"
read -p "Would you like to play a game? " ANSWER
if [ "${ANSWER}" = 'Hello joshua' ] || [ "${ANSWER}" = 'Hello joshua' ] || [ "${ANSWER}" = 'Hello Joshua' ] || [ "${ANSWER}" = 'hello joshua' ]
then
echo -e "A strange game.\n\nThe only winning move is not to play.\n"
sleep 2
fi
echo -e "\nHow about a nice game of chess?\n"
return 0
}
# koffie tijd funshizzle to clear screen
function koffie() { cat /dev/urandom | hexdump -C | grep --color=auto "ca fe"; }
# generate some random strings (handt for passwords etc)
function random() {
while :
do
echo -e "\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM" | column -t
done
}
# depth determine the world dept by checking online
function depth() { watch -n 5 "wget -q http://www.brillig.com/debt_clock -O - | grep debtiv.gif | sed -e 's/.*ALT=\"//' -e 's/\".*//' -e 's/ //g'"; }
# Run a command, redirecting output to a file, Usage: vimcmd orig newfile && vim newfile
function vimcmd() { "${1}" > "${2}" && vim "${2}"; }
# copy a key to a machine
function sshkey() { ssh-copy-id -i "${HOME}/.ssh/id_rsa.pub" $U@"${1}" ; }
# To show Apt Log History from syslogging
function apt-history() {
case "$1" in
install)
cat /var/log/dpkg.log | grep 'install '
;;
upgrade|remove)
cat /var/log/dpkg.log | grep $1
;;
rollback)
cat /var/log/dpkg.log | grep upgrade | \
grep "$2" -A10000000 | \
grep "$3" -B10000000 | \
awk '{print $4"="$5}'
;;
*)
cat /var/log/dpkg.log
;;
esac
}
# apt autokey get all missing keys
function apt-autokey() {
sudo apt-get update 2> /tmp/keymissing
for key in "$( grep "NO_PUBKEY" /tmp/keymissing |sed 's/.*NO_PUBKEY //' )"
do
echo -e "\nProcessing key: $key"
gpg --keyserver pool.sks-keyservers.net --recv $key && gpg --export --armor $key | sudo apt-key add -
done
}
# An apt-get wrapper function which will run the command via sudo, but will run it normally if you're only downloading source files.
function apt-get() { [ "$1" = source ] && (command apt-get "$@";true) || sudo apt-get "$@"; }
# convert arabic to roman numerals
function arabic2roman() {
echo $1 | sed -e 's/1...$/M&/;s/2...$/MM&/;s/3...$/MMM&/;s/4...$/MMMM&/
s/6..$/DC&/;s/7..$/DCC&/;s/8..$/DCCC&/;s/9..$/CM&/
s/1..$/C&/;s/2..$/CC&/;s/3..$/CCC&/;s/4..$/CD&/;s/5..$/D&/
s/6.$/LX&/;s/7.$/LXX&/;s/8.$/LXXX&/;s/9.$/XC&/
s/1.$/X&/;s/2.$/XX&/;s/3.$/XXX&/;s/4.$/XL&/;s/5.$/L&/
s/1$/I/;s/2$/II/;s/3$/III/;s/4$/IV/;s/5$/V/
s/6$/VI/;s/7$/VII/;s/8$/VIII/;s/9$/IX/
s/[0-9]//g'
}
#########################################################################
# MOUNT FILESTORES #
#########################################################################
# mount encfs
function mount-encfs() {
[ ${#} = 2 ] && [ -d ${1} ] || { echo "Usage: mount-encfs <crypted> <mount-dir>" ; return 0; }
{ encfs "${1}" "${2}"; } || { echo "Failed to mount ${1} to ${2}!"; return 0; }
}
function umount-encfs() {
[ ${#} = 1 ] || { echo "Usage: umount-encfs <mount>" ; return 0; }
{ [ -d "${1}" ] && fusermount -zu "${1}"; } || { echo "mount ${1} not found!" ; return 0; }
}
# CIFS mount
function mount-cifs() {
[ ${#} = 2 ] || { echo: "Usage: mount-cifs <remote share> <local mountpoint>" ; return 0; }
sudo mount -t cifs -o username="${USER}",uid="${UID}",gid="${GROUPS}" "${1}" "${2}"
}
# CIFS unmount
function unmount-cifs() {
[ ${#} = 1 ] || { echo: "Usage: unmount-cifs <local mountpoint>" ; return 0; }
sudo umount "${1}"
}
#########################################################################################################
### ###
### toolbox ###
### ###
#########################################################################################################
#########################################################################
# LOCK UNLOCK DESKTOP #
#########################################################################
# turn the display off and will stay off until this command is run again or cancelled, even after key/cursor is touched
function cust-display-off() {
{ clear ; echo "CTRL+C to cancel!"; sleep 2; }
LF=/tmp/screen-lock
if [ -f ${LF} ]; then
rm ${LF}
else
touch ${LF}
sleep .5
while [ -f ${LF} ]; do
xset dpms force off
sleep 2
done
fi
}
# lock desktop
function cust-lock() {
keychain --clear
fusermount -zu /media/cryptbin
gnome-screensaver-command -l
}
# unlock desktop
function cust-unlock() {
[ -d ${HOME}/.keychain/$HOSTNAME-sh ] || { echo -e "Loading keychain\n"; }
keychain --nogui --dir ${HOME}/.keychain "$( find ${HOME}/.ssh | \
grep rsa$ )"
source ${HOME}/.keychain/$HOSTNAME-sh
}
# start na reboot
function cust-start() {
find ${HOME}/.ssh -type f -exec chmod 600 {} \; && find ${HOME}/.ssh -type d -exec chmod 700 {} \;
keychain --nogui --dir ${HOME}/.keychain "$( find ${HOME}/.ssh | grep rsa$ )"
source ${HOME}.keychain/$HOSTNAME-sh
source ${HOME}/.bash_settings
}
#########################################################################
# System meuk #
#########################################################################
# reboot
function cust-reboot() {
sudo apt-get update && sudo apt-get upgrade;
for FUCK in $( ls /dev/pts | grep '[0-9]' )
do
sudo echo "WARNING MESSAGE! - We are going to reboot!" > /dev/pts/${FUCK}
done;
sleep 5;
sudo reboot;
}
# shutdown
function cust-shutdown() {
sudo apt-get update && sudo apt-get upgrade;
for FUCK in $( ls /dev/pts | grep '[0-9]' )
do
sudo echo "WARNING MESSAGE! - We are going to shut down!" > /dev/pts/${FUCK}
done;
sleep 5;
sudo shutdown -h now;
}
# restart or stop X
function cust-X-kill() { sudo /etc/init.d/gdm stop; }
function cust-X-hup() { sudo /etc/init.d/gdm restart; }
#########################################################################
# PASSWORD MANAGER #
#########################################################################
# open pass manager
function cust-pass() { yapet ${HOME}/.pass.pet; }
#########################################################################
# KEYCHAIN #
#########################################################################
# load keychain
function cust-key-load() { { [ -d ${HOME}/.ssh ] && keychain --nogui --dir ${HOME}/.ssh "$( find ${HOME}/.ssh |grep rsa$ )"; } || cust-unlock; }
# source keychain settings
function cust-key-source() { source ${HOME}/.keychain/$HOSTNAME-sh; }
# clear keychain
function cust-key-clear() { keychain --clear; }
# load extra key
function cust-key-add() {
[ ${#} = 1 ] || { echo "Usage: cust-key-add <ssh-key>" ; return 0; }
[ -f "${1}" ] || { echo "Key ${1} not found!"; }
keychain --nogui --dir ${HOME}/.keychain "${1}";
}
#########################################################################
# NIEUWE ALIASES, FUNCTIES, SETTINGS EN HOSTS #
#########################################################################
# edit aliassen file
function cust-edit-alias() { vim ${HOME}/.bash_aliases && source ${HOME}/.bash_aliases; }
function cust-edit-function() { vim ${HOME}/.bash_functions && source ${HOME}/.bash_functions; }
function cust-edit-setting() { vim ${HOME}/.bash_settings && source ${HOME}/.bash_settings; }
function cust-edit-host() { sudo vim /etc/hosts && sudo rndc flush; }
# reload all settings, load keychain
function cust-reload() {
echo "----> Rebuilding bash environment!" ; sleep 1
source ${HOME}/.bash_aliases
source ${HOME}/.bash_functions
source ${HOME}/.bash_settings
echo "----> Done"
echo "----> Now reloading keychain!"
cust-key-load
echo "----> Done"
}
function cust-resource() {
source ${HOME}/.bash_aliases
source ${HOME}/.bash_functions
source ${HOME}/.bash_settings
echo "Done!"
}
#########################################################################
# TOOLS #
#########################################################################
# bepaal leeftijd aan de hand van geboortejaar:
function cust-age() { YR=${1}; AGE=$(( `date +%Y` - ${YR} )); echo "Het geboortejaar is ${YR}, De leeftijd is ${AGE}"; if [ ${AGE} -lt 18 ] || [ ${AGE} -gt 40 ]; then echo "No we shouldn't"; else echo "Yes you can"; fi; }
# take screenshot
function cust-screenshot() { sleep 5 ; gnome-screenshot -a @> /dev/null & }
# kill application after sleep
function cust-xkill() { sleep 5 ; xkill & }
# Substitutes underscores for blanks in all the filenames in a directory
function cust-ontspatie() {
ONE=1 ; number=0 ; FOUND=0
for filename in * ; do
echo "$filename" | grep -q " "
if [ $? -eq $FOUND ] ; then
fname=$filename
n="$( echo $fname | sed -e "s/ /_/g" )"
mv "$fname" "$n"
let "number += 1"
fi
done
if [ "$number" -eq "$ONE" ] ; then
echo "$number file renamed."
else
echo "$number files renamed."
fi
}
# nieuw script
function cust-script() {
if [ ${#} != 1 ] || [ -e "${1}" ] ; then
echo -e "Usage: cust-script <filename>\n\
to create a new script from template"
return 1
else
( cd ${HOME} && cp ${HOME}/.bash-template "${1}" && vim "${1}" );
fi;
}
# create box around argument:
function cust-box() { t="$1xxxx";c=${2:-#}; echo ${t//?/$c}; echo "$c $1 $c"; echo ${t//?/$c}; }
# Generates list of random numbers
function cust-random () {
echo -e "\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
$RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM" | column -t
}
# Print a cron formatted time for 2 minutes in #
function cust-crontest() { date -d '+2 minutes' +'%M %k %d %m *'; }
#########################################################################
# STRINGS BEWERKEN #
#########################################################################
# center text in console with simple pipe like
function cust-center() {
l="$(cat -)"
s=$(echo -e "$l"| wc -L)
echo "$l" | while read l
do
j=$(((s-${#l})/2))
echo "$(while ((--j>0)); do printf " ";done;)$l"
done
} # ; ls --color=none / | cust-center
# right-align text in console using pipe like ( command | right )
function cust-right() {
l="$(cat -)"
[ -n "$1" ] && s=$1 || s=$(echo -e "$l"| wc -L)
echo "$l" | while read l
do
j=$(((s-${#l})))
echo "$(while ((j-->0)); do printf " ";done;)$l"
done
} # ; ls --color=none / | cust-right 150
# generate string AZ
function cust-gen-AZ() {
if [ ${#} = 1 ]; then
{ PASS="$( < /dev/urandom tr -dc "A-Za-z" | head -c "${1}" )" && echo "${PASS}"; }
else
{ echo "Usage cust-gen-AZ <bytes>" ; return 0; }
fi
}
# generate string09
function cust-gen-09() {
if [ ${#} = 1 ]; then
{ PASS="$( < /dev/urandom tr -dc "0-9" | head -c "${1}" )" && echo "${PASS}"; }
else
{ echo "Usage cust-gen-09 <bytes>" ; return 0; }
fi
}
# generate string az09
function cust-gen-az09() {
if [ ${#} = 1 ]; then
{ PASS="$( < /dev/urandom tr -dc "A-Za-z0-9" | head -c "${1}" )" && echo "${PASS}"; }
else
{ echo "Usage cust-gen-09 <bytes>" ; return 0; }
fi
}
# generate string all chars
function cust-gen-PASS() {
if [ ${#} = 1 ]; then
PASS="$( < /dev/urandom tr -dc 'A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\_\+\=\-'| head -c ${1} )" echo "${PASS}"
else
{ echo "Usage cust-gen-09 <bytes>" ; return 0; }
fi
}
# roll a single die of "$1" sides, just once
function cust-dice() {
if [ ${#} != 1 ] || [ -n "$( echo "${1}" | tr -d '[0-9]' )" ] ; then
{ echo 'Usage: cust-dice <number of dice> (max 4)' ; return 0; }
else
for num in $( seq ${1} )
do
echo "Dice $num = $(( $RANDOM % 6 +1 ))"
done
fi
}
# Convert all alphabetic characters to lowercase
function cust-tolower() {
if [ -n "${1}" ]; then
echo $1 | tr '[:upper:]' '[:lower:]'
else
cat - | tr '[:upper:]' '[:lower:]'
fi
}
# Convert all alphabetic characters converted to uppercase
function cust-toupper() {
if [ -n "${1}" ]; then
echo ${1} | tr '[:lower:]' '[:upper:]'
else
cat - | tr '[:lower:]' '[:upper:]'
fi
}
# remove duplicate lines in a file (without resorting)
function cust-duplines() { awk '!x[$0]++' "${1}"; }
# tail
function cust-tail() {
[ ${#} = 2 ] || { echo "Usage cust-tail 1000 <logfile>"; return 1; }
tail -n "${1}" -f "${2}"
}
# ruler that stretches across the terminal
function cust-ruler() {
for s in '....^....|' '1234567890'
do
w="${#s}"
str="$( for (( i=1; $i<=$(( ($COLUMNS + $w) / $w )) ; i=$i+1 )); do echo -n $s; done )"
str="$(echo $str | cut -c -$COLUMNS)"
echo $str
done
}
# Ask
function cust-ask() {
echo -n "$@" '[y/n] ' ; read ans
case "$ans" in
y*|Y*) return 0 ;;
*) return 1 ;;
esac
}
# Stopwatch
function cust-stopwatch() {
BEGIN=$(date +%s)
while true; do
NOW=$(date +%s)
DIFF=$(($NOW - $BEGIN))
MINS=$(($DIFF / 60))
SECS=$(($DIFF % 60))
echo -ne "Time elapsed: $MINS:`printf %02d $SECS`\r"
sleep .1
done
}
#########################################################################
# NETWERK CONNECTIVITEIT #
#########################################################################
# pingtool
function cust-ping() {
if [ ${#} = 1 ]; then
{ ping -q -c 1 -w 2 ${1} 2>&1 1> /dev/null && echo "ip ${1} is pingable"; } || { echo "ip ${1} is not pingable"; }
else
{ echo "Usage cust-ping <IP>" ; return 0; }
fi
}
# linkup
function cust-linkup() {
for IP in $( ifconfig -a | grep -A1 ^[a-z] | grep 'inet addr:'| awk '{print $2 }' | sed -e 's/addr://' )
do
cust-ping ${IP}
done
GW="$(route -n | awk '{print $2}' | tr -d "[A-Za-z]" |grep -Ev '(^$|0.0.0.0)' |xargs echo)" || { echo "Failed to get local gateway from route table" ; return 1; }
GOOGLE="8.8.8.8"
for IP in ${GW} ${GOOGLE}
do
cust-ping ${IP}
done
}
# ping subnet
function cust-ping-subnet() {
# determine subnet
NET="$( ifconfig | grep 'Bcast:' | cut -d ":" -f 2 | cut -d "." -f 1,2,3 )"
# ping all subnet hosts /24
HOSTS="$( nmap ${NET}.0/24 -sP | grep 'report' | awk '{ print $1 }' |xargs echo )"
# for loopje met mooie resultaatjes:
for HOST in ${HOSTS}
do
NAME="$( host ${NET}.${HOST} | grep 'domain' | awk '{ print $NF }' | cut -d '.' -f 1,2,3 )"
echo "${NET}.${HOST}.:${NAME}"
ONLINE[`expr ${#ONLINE[@]} + 1`]=${HOST}
done
echo
echo "Computers online: ${#ONLINE[*]}"
}
# debug http
function cust-debug_http() { curl $@ -o /dev/null -w "dns: %{time_namelookup} connect: %{time_connect} pretransfer: %{time_pretransfer} starttransfer: %{time_starttransfer} total: %{time_total}\n" ; }
# Lists unique IPs currently connected
function cust-doscheck() { netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n; }
# show extern ip
function cust-getip() { lynx -dump http://whatismyip.org/; }
# doe een portscan
function cust-nmap() { nmap "{1}" ; }
# show Url information
function cust-url-info() {
doms="${@}"
if [ "${#}" -eq 0 ]; then
{ echo -e "No domain given\nTry cust-url-info domain.com domain2.org anyotherdomain.net" ; return 0; }
fi
for i in ${doms} ; do
_ip="$(host $i|grep 'has address'|awk '{print $4}' )"
if [ "${_ip}" == "" ]; then
{ echo -e "\nERROR: $i DNS error or not a valid domain\n" ; continue; }
fi
ip="$( echo ${_ip[*]} | tr " " "|" )"
echo -e "\nInformation for domain: ${i} [ ${ip} ]\nQuerying individual IPs"
for j in ${_ip[*]}; do
echo -e "\n${j} results:"
whois $j |egrep -w 'OrgName:|City:|Country:|OriginAS:|NetRange:'
done
done
}
# cleanly list available wireless networks (using iwlist)
function wscan() { iwlist wlan0 scan | \
sed -ne 's#^[[:space:]]*\(Quality=\|Encryption key:\|ESSID:\)#\1#p' |\
sed -e 's#^[[:space:]]*\(Mode:.*\)$#\1\n#p'; }
#########################################################################
# Overview aliassen en functies #
#########################################################################
function cust-overview() {
echo -e "\n\
-------------------------------------------------------------------------------------------
FUNCTIONS:\n\
-------------------------------------------------------------------------------------------
$( cat ${HOME}/.bash_functions |grep -E '(^#|^function)' | sed -e 's/().*$//g' )\n
-------------------------------------------------------------------------------------------
ALIASES:\n\
-------------------------------------------------------------------------------------------
$( alias | sed 's/alias/#/' | tr "=" "\n" | sed -e "s/\'//g" )\n
-------------------------------------------------------------------------------------------"
}
#########################################################################
# BACKUP EN BEWERKING #
#########################################################################
# Easy extract
function cust-extract() {
if [ -f ${1} ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $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 ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
}
# cust backup
function cust-backup() {
if [ -d ./old ] ; then
find -maxdepth 1 -exec cp -rv {} ./old/{}.$(date +'%Y%m%d') \;
else
{ mkdir ./old && find -maxdepth 1 -exec cp -rv {} ./old/{}.$(date +'%Y%m%d') \;; }
fi
}
# lsof grep
function cust-lsofgrep() {
if [ "${#}" -lt 1 ] || [ "${#}" -gt 1 ]; then
echo -e "grep lsof \n\
usage: losfg [port/program/whatever]"
else
lsof | grep -i "${1}" | less
fi
}
# ps grep
function cust-psgrep() {
if [ "${#}" -lt 1 ] || [ "${#}" -gt 1 ]; then
echo -e "grep running processes\n\
usage: psg [process]"
else
ps -ef | grep -v grep | grep -i "${1}" | less
fi
}
#########################################################################
# INTERNETZ #
#########################################################################
# Site down for everyone or just me?
function cust-downforme() {
if [ ${#} = 0 ]
then
echo -e "usage: cust-downforme website_url"
else
JUSTYOUARRAY="$(lynx -dump http://downforeveryoneorjustme.com/${1} | grep -o "It's just you")"
if [ ${#JUSTYOUARRAY} != 0 ]
then
echo -e "It's just you. \n${1} is up."
else
echo -e "It's not just you! \n${1} looks down from here."
fi
fi
}
# Search IMDB.COM
function imdb() { firefox "http://www.imdb.com/find?s=all&q="${@}"&x=0&y=0" & }
# send to pastebin
function cust-2pastebin() {
# first things first: check for curl
[ -x "$(which curl)" ] || { echo "this script depends on curl" ; return 0; }
# check for stdin, if so, get it :)
if [ "$( tty )" == 'not a tty' ] && [ ${#} = 1 ]
then
INPUT="$(</dev/stdin)"
else
{ echo "Usage: echo plop | cust-2pastebin <name>" ; return 0; }
fi
# check for empty input
[ -z ${INPUT} ] && { echo "Usage: echo plop | ${0}"; return 0; }
# set empty var:
NAME="${1}"
EMAIL=""
TYPE="bash"
API="http://pastebin.com/api_public.php"
QUERYSTRING="paste_private=0&paste_code=${INPUT}&paste_name=${NAME}&paste_email=${EMAIL}&paste_format=${TYPE}"
#post data to pastebin.com API
curl -d "${QUERYSTRING}" "${API}"
echo ""
}
# download torrent
function cust-torrent() { ( cd ~/Downloads/ && curl -O "${1}"); }
#########################################################################
# INTERNETZ # Youtube stuff #
#########################################################################
# stream YouTube videos directly to your media player
function cust-youtube2mplayer() {
video_id=$(curl -s ${1} | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p");
mplayer -fs $(echo "http://youtube.com/get_video.php?$video_id");
}
# convert to ogg
function cust-youtube2ogg(){ ffmpeg -i "${1}" -vn "${1}".ogg; }
# play YouTube's first match directly
function cust-ytplay() {
args="$*";mplayer -fs $(youtube-dl -g "http://www.youtube.com$(lynx --source \
"http://www.youtube.com/results?search_query=${args// /+}&aq=f"|grep -m1 '<a id=.*watch?v=.*title'|\
cut -d\" -f4)")
}
# define a word - USAGE: define dog
function cust-define() {
local LNG=$(echo $LANG | cut -d '_' -f 1)
local CHARSET=$(echo $LANG | cut -d '.' -f 2)
lynx -accept_all_cookies -dump -hiddenlinks=ignore -nonumbers -assume_charset="$CHARSET" -display_charset="$CHARSET" \
"http://www.google.com/search?hl=${LNG}&q=define%3A+${1}&btnG=Google+Search" | grep -m 5 -C 2 -A 5 -w "*" > /tmp/define
if [ ! -s /tmp/define ]; then
echo "Sorry, google doesn't know this one..."
rm -f /tmp/define
return 1
else
cat /tmp/define | grep -v Search
echo ""
fi
rm -f /tmp/define
return 0
}
# detect language of a string
function cust-detectlang() { curl -s "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=$@" | sed 's/{"responseData": {"language":"\([^"]*\)".*/\1\n/'; }
# translate a word using Google
# usage: translate <phrase> <output-language>
function cust-translate() {
[ ${#} = 2 ] || { echo "usage: translate <phrase> <output-language>" ; return 0; }
wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=%7C${2:-en}" | sed 's/.*{"translatedText":"\([^"]*\)".*/\1\n/'
}
# look in Webster
function cust-webster() { curl dict://dict.org/d:${1}:web1913; }
# look in WordNet
function cust-wordnet() { curl dict://dict.org/d:${1}:wn; }
# Using PIPEs, Execute a command, convert output to .png file, upload file to imgur.com, then returning the address of the .png
function cust-imgur() {
convert label:@- png:-|curl -F "image=@-" -F "key=1913b4ac473c692372d108209958fd15" http://api.imgur.com/2/upload.xml| \
grep -E -o "<original>(.)*</original>" | grep -E -o "http://i.imgur.com/[^<]*"
}
# Query Wikipedia via console over DNS
function cust-wikipedia() {
[ ${#} != 0 ] || { echo "usage: cust-wikipedia <string>" ; return 0; }
STRING="$( echo "${@}" | sed -e 's/ /_/g' )"
dig +short txt ${STRING}.wp.dg.cx
}
# Auto send an attachment from CLI
function cust-mail-file() {
[ ${#} != 2 ] || { echo "usage: cust-mail-file <file> <rcpt>" ; return 0; }
echo "File auto-sent from $(hostname -f) by $(whoami)." | mutt -s "See Attached File" -a "${1}" "${2}"
}
# Create an easy to pronounce shortened URL from CLI
function cust-shorturl() {
[ ${#} != 1 ] || { echo "usage: cust-shorturl\"Long URL\"" ; return 0; }
curl -s "http://shoutkey.com/new?url=${1}" | sed -n 's/\<h1\>/\&/p' | sed 's/<[^>]*>//g;/</N;//b'
}
# Transmission with blocklists
function cust-torrentbl() {
pkill transmission-gtk
BLOCKLISTDIR="$HOME/.config/transmission/blocklists"
BASEURL="http://list.iblocklist.com/?list="
ENDURL="&fileformat=p2p&archiveformat=gz"
FILES=( bt_level1 bt_level2 bt_level3 us bt_dshield jcjfaxgyyshvdbceroxf ijfqtofzixtwayqovmxn bt_templist dufcxgnbjsdwmwctgfuj )
cd $BLOCKLISTDIR && /bin/rm *.*
for i in "${FILES[@]}"
do
curl -L $BASEURL"$i"$ENDURL -o $BLOCKLISTDIR/$i.gz
gunzip -f $BLOCKLISTDIR/$i.gz
mv $BLOCKLISTDIR/$i $BLOCKLISTDIR/$i.txt
done
transmission-gtk &
cd $BLOCKLISTDIR && /bin/rm bt_level1.txt bt_level2.txt bt_level3.txt us.txt bt_dshield.txt jcjfaxgyyshvdbceroxf.txt ijfqtofzixtwayqovmxn.txt bt_templist.txt dufcxgnbjsdwmwctgfuj.txt
}
# show just first 3 octets of IP address and runs nmap just as original function.
function cust-subnet() {
if [ -n "$1" ]; then
net="$1"
else
net=$(cat /etc/resolv.conf | grep 'nameserver' | cut -c12-26 | awk -F '.' '{print $1"."$2"."$3".0/24"}')
fi
echo "testing $net for online hosts"
nmap -sP $net | awk '/Host/ && /up/ { print $0; }'
echo "done"
}
#########################################################################
# Good bash tips for everyone #
#########################################################################
function bashtips() {
cat <<EOF
DIRECTORIES
-----------
~- Previous working directory
pushd tmp Push tmp && cd tmp
popd Pop && cd
GLOBBING AND OUTPUT SUBSTITUTION
--------------------------------
ls a[b-dx]e Globs abe, ace, ade, axe
ls a{c,bl}e Globs ace, able
\$(ls) \`ls\` (but nestable!)
HISTORY MANIPULATION
--------------------
!! Last command
!?foo Last command containing \`foo'
^foo^bar^ Last command containing \`foo', but substitute \`bar'
!!:0 Last command word
!!:^ Last command's first argument
!\$ Last command's last argument
!!:* Last command's arguments
!!:x-y Arguments x to y of last command
C-s search forwards in history
C-r search backwards in history
LINE EDITING
------------
M-d kill to end of word
C-w kill to beginning of word
C-k kill to end of line
C-u kill to beginning of line
M-r revert all modifications to current line
C-] search forwards in line
M-C-] search backwards in line
C-t transpose characters
M-t transpose words
M-u uppercase word
M-l lowercase word
M-c capitalize word
COMPLETION
----------
M-/ complete filename
M-~ complete user name
M-@ complete host name
M-\$ complete variable name
M-! complete command name
M-^ complete history
EOF
}