flipje

bash_functions

Jan 16th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 42.38 KB | None | 0 0
  1. #########################################################################################################
  2. ###                                                                                                   ###
  3. ###    Functies:                                                                                      ###
  4. ###                                                                                                   ###
  5. #########################################################################################################
  6.  
  7. # exit function to use in scripts , Usage: die "REASON"
  8. function die()                { echo "Error: ${1}" ; exit 1; }
  9.  
  10. # sudo handigheid, if no args do sudo su -
  11. function sudo()               {
  12.                               command="$@"
  13.                               if [ -z "$command" ]; then
  14.                                 command sudo su -
  15.                               else
  16.                                 command sudo "$@"
  17.                               fi
  18.                               }
  19.  
  20. # find handigheid, if no args do find su -
  21. function find()               {
  22.                               command="$@"
  23.                               if [ -z "$command" ]; then
  24.                                 command find .
  25.                               else
  26.                                 command find "$@"
  27.                               fi
  28.                               }
  29.  
  30.  
  31. # kill firefox the hard way, in case of a crash
  32. function killfox()            { pkill firefox-bin || ps -ef |grep firefox |grep -v grep | awk '{print $2}' | while read line; do  kill -9 $line; done }
  33.  
  34. # fix metacity in case of a crash
  35. function fix()                { metacity --replace & }
  36.  
  37. # show tree of the current directory
  38. function tree()               {
  39.                               { [ ${#} = 0 ] && find . -type d | sed -e 1d -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|-/'; }
  40.                               { [ ${#} != 0 ] && find ${@} -type d | sed -e 1d -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|-/'; }
  41.                               }
  42.  
  43. # top 10 of most used commands
  44. function top10()              {
  45.                               echo "top 10 commands:"
  46.                               history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -nr | head
  47.                               }
  48.  
  49. # wargames
  50. function game()           {
  51.                               echo -e "Greetings, Professor Falken.\n\n"
  52.                               read -p "Would you like to play a game?  " ANSWER
  53.                       if [ "${ANSWER}" = 'Hello joshua' ] || [ "${ANSWER}" = 'Hello joshua' ] || [ "${ANSWER}" = 'Hello Joshua' ] || [ "${ANSWER}" = 'hello joshua' ]
  54.                               then
  55.                                 echo -e "A strange game.\n\nThe only winning move is not to play.\n"
  56.                                 sleep 2  
  57.                               fi
  58.                               echo -e "\nHow about a nice game of chess?\n"
  59.                               return 0
  60.                               }
  61.  
  62. # koffie tijd funshizzle to clear screen
  63. function koffie()             { cat /dev/urandom | hexdump -C | grep --color=auto "ca fe"; }
  64.  
  65. # generate some random strings (handt for passwords etc)
  66. function random()             {
  67.                               while :
  68.                               do
  69.                                 echo -e "\n\
  70.                                $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  71.                                $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  72.                                $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  73.                                $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  74.                                $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  75.                                $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  76.                                $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM" | column -t
  77.                               done
  78.                               }
  79.  
  80. # depth determine the world dept by checking online
  81. 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'"; }
  82.  
  83. # Run a command, redirecting output to a file,  Usage: vimcmd orig newfile && vim newfile
  84. function vimcmd()             { "${1}" > "${2}" && vim "${2}"; }
  85.  
  86. # copy a key to a machine
  87. function sshkey()         { ssh-copy-id -i "${HOME}/.ssh/id_rsa.pub" $U@"${1}" ; }
  88.  
  89. # To show Apt Log History from syslogging
  90. function apt-history()        {
  91.                               case "$1" in
  92.                                 install)
  93.                                   cat /var/log/dpkg.log | grep 'install '
  94.                                 ;;
  95.                                 upgrade|remove)
  96.                                   cat /var/log/dpkg.log | grep $1
  97.                                 ;;
  98.                                 rollback)
  99.                                   cat /var/log/dpkg.log | grep upgrade | \
  100.                                   grep "$2" -A10000000 | \
  101.                                   grep "$3" -B10000000 | \
  102.                                   awk '{print $4"="$5}'
  103.                                 ;;
  104.                                 *)
  105.                                   cat /var/log/dpkg.log
  106.                                 ;;
  107.                               esac
  108.                               }
  109.  
  110. # apt autokey get all missing keys
  111. function apt-autokey()        {
  112.                               sudo apt-get update 2> /tmp/keymissing
  113.                               for key in "$( grep "NO_PUBKEY" /tmp/keymissing |sed 's/.*NO_PUBKEY //' )"
  114.                               do
  115.                                 echo -e "\nProcessing key: $key"
  116.                                 gpg --keyserver pool.sks-keyservers.net --recv $key && gpg --export --armor $key | sudo apt-key add -
  117.                               done
  118.                               }
  119.  
  120. # An apt-get wrapper function which will run the command via sudo, but will run it normally if you're only downloading source files.
  121. function apt-get()            { [ "$1" = source ] && (command apt-get "$@";true) || sudo apt-get "$@"; }
  122.  
  123.  
  124. # convert arabic to roman numerals
  125. function arabic2roman()       {
  126.                               echo $1 | sed -e 's/1...$/M&/;s/2...$/MM&/;s/3...$/MMM&/;s/4...$/MMMM&/
  127.                              s/6..$/DC&/;s/7..$/DCC&/;s/8..$/DCCC&/;s/9..$/CM&/
  128.                              s/1..$/C&/;s/2..$/CC&/;s/3..$/CCC&/;s/4..$/CD&/;s/5..$/D&/
  129.                              s/6.$/LX&/;s/7.$/LXX&/;s/8.$/LXXX&/;s/9.$/XC&/
  130.                              s/1.$/X&/;s/2.$/XX&/;s/3.$/XXX&/;s/4.$/XL&/;s/5.$/L&/
  131.                              s/1$/I/;s/2$/II/;s/3$/III/;s/4$/IV/;s/5$/V/
  132.                              s/6$/VI/;s/7$/VII/;s/8$/VIII/;s/9$/IX/
  133.                              s/[0-9]//g'
  134.                               }
  135.  
  136. #########################################################################
  137. # MOUNT FILESTORES                                                      #
  138. #########################################################################
  139.  
  140. # mount encfs
  141. function mount-encfs()        {
  142.                               [ ${#} = 2 ] && [ -d ${1} ] || { echo "Usage: mount-encfs <crypted> <mount-dir>" ; return 0; }
  143.                               { encfs "${1}" "${2}"; } || { echo "Failed to mount ${1} to ${2}!"; return 0; }
  144.                               }
  145.  
  146. function umount-encfs()       {
  147.                               [ ${#} = 1 ] || { echo "Usage: umount-encfs <mount>" ; return 0; }
  148.                               { [ -d "${1}" ] && fusermount -zu "${1}"; } || { echo "mount ${1} not found!" ; return 0; }
  149.                               }
  150.  
  151. # CIFS mount
  152. function mount-cifs()         {
  153.                               [ ${#} = 2 ] || { echo: "Usage: mount-cifs <remote share> <local mountpoint>" ; return 0; }
  154.                               sudo mount -t cifs -o username="${USER}",uid="${UID}",gid="${GROUPS}" "${1}" "${2}"
  155.                               }
  156. # CIFS unmount
  157. function unmount-cifs()       {
  158.                               [ ${#} = 1 ] || { echo: "Usage: unmount-cifs <local mountpoint>" ; return 0; }
  159.                               sudo umount "${1}"
  160.                               }
  161.  
  162.  
  163.  
  164. #########################################################################################################
  165. ###                                                                                                   ###
  166. ###   toolbox                                                                                         ###
  167. ###                                                                                                   ###
  168. #########################################################################################################
  169.  
  170. #########################################################################
  171. # LOCK UNLOCK DESKTOP                                                   #
  172. #########################################################################
  173.  
  174. # turn  the display off and will stay off until this command is run again or cancelled, even after key/cursor is touched
  175. function cust-display-off()   {
  176.                               { clear ; echo "CTRL+C to cancel!"; sleep 2; }
  177.                               LF=/tmp/screen-lock
  178.                               if [ -f ${LF} ]; then
  179.                                 rm ${LF}
  180.                               else
  181.                                 touch ${LF}
  182.                                 sleep .5
  183.                                 while [ -f  ${LF} ]; do
  184.                                   xset dpms force off
  185.                                   sleep 2
  186.                                 done
  187.                               fi
  188.                               }
  189.  
  190. # lock desktop
  191. function cust-lock()          {
  192.                               keychain --clear
  193.                               fusermount -zu /media/cryptbin
  194.                               gnome-screensaver-command -l
  195.                               }
  196.  
  197. # unlock desktop
  198. function cust-unlock()        {
  199.                               [ -d ${HOME}/.keychain/$HOSTNAME-sh ] || {  echo -e "Loading keychain\n"; }
  200.                               keychain --nogui --dir ${HOME}/.keychain "$( find ${HOME}/.ssh | \
  201.                              grep rsa$ )"
  202.                               source ${HOME}/.keychain/$HOSTNAME-sh
  203.                               }
  204.  
  205. # start na reboot
  206. function cust-start()         {
  207.                               find ${HOME}/.ssh -type f -exec chmod 600 {} \; &&  find ${HOME}/.ssh -type d  -exec chmod 700 {} \;
  208.                               keychain --nogui --dir ${HOME}/.keychain "$( find ${HOME}/.ssh | grep rsa$ )"
  209.                               source ${HOME}.keychain/$HOSTNAME-sh
  210.                               source ${HOME}/.bash_settings
  211.                               }
  212.  
  213. #########################################################################
  214. # System meuk                                                           #
  215. #########################################################################
  216.  
  217. # reboot
  218. function cust-reboot()        {
  219.                               sudo apt-get update && sudo apt-get upgrade;
  220.                               for FUCK in $( ls /dev/pts | grep '[0-9]' )
  221.                               do
  222.                                 sudo echo "WARNING MESSAGE! - We are going to reboot!" > /dev/pts/${FUCK}
  223.                               done;
  224.                               sleep 5;
  225.                               sudo reboot;
  226.                               }
  227.  
  228. # shutdown
  229. function cust-shutdown()      {
  230.                               sudo apt-get update && sudo apt-get upgrade;
  231.                               for FUCK in $( ls /dev/pts | grep '[0-9]' )
  232.                               do
  233.                                 sudo echo "WARNING MESSAGE! - We are going to shut down!" > /dev/pts/${FUCK}
  234.                               done;
  235.                               sleep 5;
  236.                               sudo shutdown -h now;
  237.                               }
  238.  
  239. # restart or stop X
  240. function cust-X-kill()        { sudo /etc/init.d/gdm stop; }
  241. function cust-X-hup()         { sudo /etc/init.d/gdm restart; }
  242.  
  243. #########################################################################
  244. # PASSWORD MANAGER                                                      #
  245. #########################################################################
  246.  
  247. # open pass manager
  248. function cust-pass()          { yapet ${HOME}/.pass.pet; }
  249.  
  250. #########################################################################
  251. # KEYCHAIN                                                              #
  252. #########################################################################
  253.  
  254. # load keychain
  255. function cust-key-load()      { { [ -d ${HOME}/.ssh ] && keychain --nogui --dir ${HOME}/.ssh "$( find ${HOME}/.ssh |grep rsa$ )"; } || cust-unlock; }
  256.    
  257. # source keychain settings
  258. function cust-key-source()    { source ${HOME}/.keychain/$HOSTNAME-sh; }
  259.  
  260. # clear keychain
  261. function cust-key-clear()     { keychain --clear; }
  262.  
  263. # load extra key
  264. function cust-key-add()       {
  265.                               [ ${#} = 1 ]  || { echo "Usage: cust-key-add <ssh-key>" ; return 0; }
  266.                               [ -f "${1}" ] || { echo "Key ${1} not found!"; }
  267.                               keychain --nogui --dir ${HOME}/.keychain "${1}";
  268.                               }
  269.  
  270. #########################################################################
  271. # NIEUWE ALIASES, FUNCTIES, SETTINGS EN HOSTS                           #
  272. #########################################################################
  273.  
  274. # edit aliassen file
  275. function cust-edit-alias()    { vim ${HOME}/.bash_aliases && source ${HOME}/.bash_aliases; }
  276. function cust-edit-function() { vim ${HOME}/.bash_functions && source ${HOME}/.bash_functions; }
  277. function cust-edit-setting()  { vim ${HOME}/.bash_settings && source ${HOME}/.bash_settings; }
  278. function cust-edit-host()     { sudo vim /etc/hosts && sudo rndc flush; }
  279.  
  280. # reload all settings, load keychain
  281. function cust-reload()        {
  282.                               echo "----> Rebuilding bash environment!" ; sleep 1
  283.                               source ${HOME}/.bash_aliases
  284.                               source ${HOME}/.bash_functions
  285.                               source ${HOME}/.bash_settings
  286.                               echo "----> Done"
  287.                               echo "----> Now reloading keychain!"
  288.                               cust-key-load
  289.                               echo "----> Done"
  290.                               }
  291.  
  292. function cust-resource()      {
  293.                               source ${HOME}/.bash_aliases
  294.                               source ${HOME}/.bash_functions
  295.                               source ${HOME}/.bash_settings
  296.                               echo "Done!"
  297.                               }
  298.  
  299. #########################################################################
  300. # TOOLS                                                                 #
  301. #########################################################################
  302.  
  303. # bepaal leeftijd aan de hand van geboortejaar:
  304. 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; }
  305.  
  306. # take screenshot
  307. function cust-screenshot()    { sleep 5 ; gnome-screenshot -a @> /dev/null & }
  308.  
  309. # kill application after sleep
  310. function cust-xkill()         { sleep 5 ; xkill & }
  311.  
  312. # Substitutes underscores for blanks in all the  filenames in a directory
  313. function cust-ontspatie()    {
  314.                               ONE=1 ; number=0 ; FOUND=0                  
  315.                               for filename in * ; do
  316.                                 echo "$filename" | grep -q " "        
  317.                                 if [ $? -eq $FOUND ] ; then
  318.                                   fname=$filename                      
  319.                                   n="$( echo $fname | sed -e "s/ /_/g" )"
  320.                                   mv "$fname" "$n"                    
  321.                                   let "number += 1"
  322.                                 fi
  323.                               done
  324.                               if [ "$number" -eq "$ONE" ] ; then
  325.                                 echo "$number file renamed."
  326.                               else
  327.                                 echo "$number files renamed."
  328.                               fi
  329.                               }
  330.  
  331. # nieuw script
  332. function cust-script()        {
  333.                               if [ ${#} != 1 ] || [ -e "${1}" ] ; then
  334.                                 echo -e "Usage: cust-script <filename>\n\
  335.                                         to create a new script from template"
  336.                                 return 1
  337.                               else
  338.                                 ( cd ${HOME} && cp ${HOME}/.bash-template "${1}" && vim "${1}" );
  339.                               fi;
  340.                               }
  341.  
  342. # create box around argument:
  343. function cust-box()           { t="$1xxxx";c=${2:-#}; echo ${t//?/$c}; echo "$c $1 $c"; echo ${t//?/$c}; }
  344.  
  345. # Generates list of random numbers
  346. function cust-random ()       {
  347.                               echo -e "\n\
  348.                              $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  349.                              $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  350.                              $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  351.                              $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM\n\
  352.                              $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM $RANDOM" | column -t
  353.                               }
  354.  
  355.  
  356. # Print a cron formatted time for 2 minutes in   #
  357. function cust-crontest()      { date -d '+2 minutes' +'%M %k %d %m *'; }
  358.  
  359. #########################################################################
  360. # STRINGS BEWERKEN                                                      #
  361. #########################################################################
  362.  
  363. # center text in console with simple pipe like
  364. function cust-center()        {
  365.                               l="$(cat -)"
  366.                               s=$(echo -e "$l"| wc -L)
  367.                               echo "$l" | while read l
  368.                               do
  369.                                 j=$(((s-${#l})/2))
  370.                                 echo "$(while ((--j>0)); do printf " ";done;)$l"
  371.                               done
  372.                               } # ; ls --color=none / | cust-center
  373.  
  374. # right-align text in console using pipe like ( command | right )
  375. function cust-right()         {
  376.                               l="$(cat -)"
  377.                               [ -n "$1" ] && s=$1 || s=$(echo -e "$l"| wc -L)
  378.                               echo "$l" | while read l
  379.                               do
  380.                                 j=$(((s-${#l})))
  381.                                 echo "$(while ((j-->0)); do printf " ";done;)$l"
  382.                               done
  383.                               } # ; ls --color=none / | cust-right 150
  384.  
  385. # generate string AZ
  386. function cust-gen-AZ()        {
  387.                               if [ ${#} = 1 ]; then
  388.                                 { PASS="$( < /dev/urandom tr -dc "A-Za-z" | head -c "${1}" )" && echo "${PASS}"; }
  389.                               else
  390.                                 { echo "Usage cust-gen-AZ <bytes>" ; return 0; }
  391.                               fi
  392.                               }
  393.  
  394. # generate string09
  395. function cust-gen-09()        {
  396.                               if [ ${#} = 1 ]; then
  397.                                 { PASS="$( < /dev/urandom tr -dc "0-9" | head -c "${1}" )" && echo "${PASS}"; }
  398.                               else
  399.                                 { echo "Usage cust-gen-09 <bytes>" ; return 0; }
  400.                               fi
  401.                               }
  402. # generate string az09
  403. function cust-gen-az09()      {
  404.                               if [ ${#} = 1 ]; then
  405.                                 { PASS="$( < /dev/urandom tr -dc "A-Za-z0-9" | head -c "${1}" )" && echo "${PASS}"; }
  406.                               else
  407.                                 { echo "Usage cust-gen-09 <bytes>" ; return 0; }
  408.                               fi
  409.                               }
  410. # generate string all chars
  411. function cust-gen-PASS()      {
  412.                               if [ ${#} = 1 ]; then
  413.                                 PASS="$( < /dev/urandom tr -dc 'A-Za-z0-9\!\@\#\$\%\^\&\*\(\)\_\+\=\-'| head -c ${1} )"                                 echo "${PASS}"
  414.                               else
  415.                                 { echo "Usage cust-gen-09 <bytes>" ; return 0; }
  416.                               fi
  417.                               }
  418.  
  419. # roll a single die of "$1" sides, just once
  420. function cust-dice()          {
  421.                               if [ ${#} != 1 ] || [ -n "$( echo "${1}" | tr -d '[0-9]' )" ] ; then
  422.                                 { echo 'Usage: cust-dice <number of dice> (max 4)' ; return 0; }
  423.                               else
  424.                                 for num in $( seq ${1} )
  425.                                 do                                    
  426.                                   echo "Dice $num = $(( $RANDOM % 6 +1 ))"
  427.                                 done
  428.                               fi
  429.                               }
  430.  
  431. # Convert all alphabetic characters to lowercase
  432. function cust-tolower()       {
  433.                               if [ -n "${1}" ]; then
  434.                                 echo $1 | tr '[:upper:]' '[:lower:]'
  435.                               else
  436.                                 cat - | tr '[:upper:]' '[:lower:]'
  437.                               fi
  438.                               }
  439.  
  440. # Convert all alphabetic characters converted to uppercase
  441. function cust-toupper()       {
  442.                               if [ -n "${1}" ]; then
  443.                                 echo ${1} | tr '[:lower:]' '[:upper:]'
  444.                               else
  445.                                 cat - | tr '[:lower:]' '[:upper:]'
  446.                               fi
  447.                               }
  448.  
  449. # remove duplicate lines in a file (without resorting)
  450. function cust-duplines()     { awk '!x[$0]++' "${1}"; }
  451.  
  452. # tail                              
  453. function cust-tail()         {
  454.                  [ ${#} = 2 ] || { echo "Usage cust-tail 1000 <logfile>"; return 1; }
  455.                              tail -n "${1}" -f "${2}"
  456.                              }
  457.  
  458. # ruler that stretches across the terminal
  459. function cust-ruler()        {
  460.                              for s in '....^....|' '1234567890'
  461.                              do
  462.                                w="${#s}"
  463.                                str="$( for (( i=1; $i<=$(( ($COLUMNS + $w) / $w )) ; i=$i+1 )); do echo -n $s; done )"
  464.                    str="$(echo $str | cut -c -$COLUMNS)"
  465.                    echo $str
  466.                  done
  467.                  }
  468.  
  469. # Ask
  470. function cust-ask()          {
  471.                              echo -n "$@" '[y/n] ' ; read ans
  472.                              case "$ans" in
  473.                                y*|Y*) return 0 ;;
  474.                                *) return 1 ;;
  475.                              esac
  476.                              }
  477.  
  478. # Stopwatch
  479. function cust-stopwatch()    {
  480.                              BEGIN=$(date +%s)
  481.                              while true; do
  482.                                NOW=$(date +%s)
  483.                                DIFF=$(($NOW - $BEGIN))
  484.                                MINS=$(($DIFF / 60))
  485.                                SECS=$(($DIFF % 60))
  486.                                echo -ne "Time elapsed: $MINS:`printf %02d $SECS`\r"
  487.                                sleep .1
  488.                              done
  489.                              }
  490.  
  491.  
  492. #########################################################################
  493. # NETWERK CONNECTIVITEIT                                                #
  494. #########################################################################
  495.  
  496. # pingtool
  497. function cust-ping()          {
  498.                               if [ ${#} = 1 ]; then
  499.                                 { ping -q -c 1 -w 2 ${1} 2>&1 1> /dev/null && echo "ip ${1} is pingable"; } || { echo "ip ${1} is not pingable"; }
  500.                               else
  501.                                 { echo "Usage cust-ping <IP>" ; return 0; }
  502.                               fi
  503.                               }
  504.  
  505. # linkup
  506. function cust-linkup()        {
  507.                               for IP in $( ifconfig -a | grep -A1 ^[a-z] | grep 'inet addr:'| awk '{print $2 }' | sed -e 's/addr://' )
  508.                               do
  509.                                 cust-ping ${IP}
  510.                               done
  511.                               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; }
  512.                               GOOGLE="8.8.8.8"
  513.                               for IP in ${GW} ${GOOGLE}
  514.                               do
  515.                                cust-ping ${IP}
  516.                               done  
  517.                               }
  518. # ping subnet
  519. function cust-ping-subnet()   {
  520.                               # determine subnet
  521.                               NET="$( ifconfig | grep 'Bcast:' | cut -d ":" -f 2 | cut -d "." -f 1,2,3 )"
  522.                               # ping all subnet hosts /24
  523.                               HOSTS="$( nmap ${NET}.0/24 -sP | grep 'report' | awk '{ print $1 }' |xargs echo )"
  524.                               # for loopje met mooie resultaatjes:
  525.                               for HOST in ${HOSTS}
  526.                               do
  527.                                 NAME="$( host ${NET}.${HOST} | grep 'domain' | awk '{ print $NF }' | cut -d '.' -f 1,2,3 )"
  528.                                 echo "${NET}.${HOST}.:${NAME}"
  529.                                 ONLINE[`expr ${#ONLINE[@]} + 1`]=${HOST}
  530.                               done
  531.                               echo
  532.                               echo "Computers online: ${#ONLINE[*]}"
  533.                               }
  534.                          
  535. # debug http
  536. 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" ; }
  537.  
  538. # Lists unique IPs currently connected
  539. function cust-doscheck()      { netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n; }
  540.  
  541. # show extern ip
  542. function cust-getip()         { lynx -dump http://whatismyip.org/; }
  543.  
  544. # doe een portscan
  545. function cust-nmap()          { nmap "{1}" ; }
  546.  
  547. # show Url information
  548. function cust-url-info()      {
  549.                               doms="${@}"
  550.                               if [ "${#}" -eq 0 ]; then
  551.                                 { echo -e "No domain given\nTry cust-url-info domain.com domain2.org anyotherdomain.net" ; return 0; }
  552.                               fi
  553.                               for i in ${doms} ; do
  554.                                 _ip="$(host $i|grep 'has address'|awk '{print $4}' )"
  555.                                 if [ "${_ip}" == "" ]; then
  556.                                   { echo -e "\nERROR: $i DNS error or not a valid domain\n" ; continue; }
  557.                                 fi
  558.                                 ip="$( echo ${_ip[*]} | tr " " "|" )"
  559.                                 echo -e "\nInformation for domain: ${i} [ ${ip} ]\nQuerying individual IPs"
  560.                                 for j in ${_ip[*]}; do
  561.                                   echo -e "\n${j} results:"
  562.                                   whois $j |egrep -w 'OrgName:|City:|Country:|OriginAS:|NetRange:'
  563.                                 done
  564.                               done
  565.                               }
  566.  
  567. # cleanly list available wireless networks (using iwlist)
  568. function wscan()              { iwlist wlan0 scan | \
  569.                                 sed -ne 's#^[[:space:]]*\(Quality=\|Encryption key:\|ESSID:\)#\1#p' |\  
  570.                                 sed -e 's#^[[:space:]]*\(Mode:.*\)$#\1\n#p'; }
  571.  
  572. #########################################################################
  573. # Overview aliassen en functies                                         #
  574. #########################################################################
  575.  
  576. function cust-overview()      {
  577.                               echo -e "\n\
  578. -------------------------------------------------------------------------------------------
  579. FUNCTIONS:\n\
  580. -------------------------------------------------------------------------------------------
  581. $( cat ${HOME}/.bash_functions |grep -E '(^#|^function)' | sed -e 's/().*$//g' )\n
  582. -------------------------------------------------------------------------------------------
  583. ALIASES:\n\
  584. -------------------------------------------------------------------------------------------
  585. $( alias | sed 's/alias/#/' | tr "=" "\n" | sed -e "s/\'//g" )\n
  586. -------------------------------------------------------------------------------------------"
  587.                               }
  588.                              
  589.  
  590. #########################################################################
  591. # BACKUP EN BEWERKING                                                   #
  592. #########################################################################
  593.  
  594. # Easy extract
  595. function cust-extract()      {
  596.                              if [ -f ${1} ] ; then
  597.                                case $1 in
  598.                                  *.tar.bz2)   tar xvjf $1    ;;
  599.                                  *.tar.gz)    tar xvzf $1    ;;
  600.                                  *.bz2)       bunzip2 $1     ;;
  601.                                  *.rar)       rar x $1       ;;
  602.                                  *.gz)        gunzip $1      ;;
  603.                                  *.tar)       tar xvf $1     ;;
  604.                                  *.tbz2)      tar xvjf $1    ;;
  605.                                  *.tgz)       tar xvzf $1    ;;
  606.                                  *.zip)       unzip $1       ;;
  607.                                  *.Z)         uncompress $1  ;;
  608.                                  *.7z)        7z x $1        ;;
  609.                                  *)           echo "don't know how to extract '$1'..." ;;
  610.                                esac
  611.                              else
  612.                                echo "'$1' is not a valid file!"
  613.                              fi
  614.                              }
  615.  
  616. # cust backup
  617. function cust-backup()      {
  618.                             if [ -d ./old ] ; then
  619.                               find -maxdepth 1 -exec cp -rv {} ./old/{}.$(date +'%Y%m%d') \;
  620.                             else                              
  621.                               { mkdir ./old && find -maxdepth 1 -exec cp -rv {} ./old/{}.$(date +'%Y%m%d') \;; }        
  622.                             fi
  623.                             }
  624.  
  625.  
  626. # lsof grep
  627. function cust-lsofgrep()    {
  628.                             if [ "${#}" -lt 1 ] || [ "${#}" -gt 1 ]; then
  629.                               echo -e "grep lsof \n\
  630.                              usage: losfg [port/program/whatever]"
  631.                             else
  632.                               lsof | grep -i "${1}" | less
  633.                             fi
  634.                             }
  635.  
  636. # ps grep
  637. function cust-psgrep()      {
  638.                             if [ "${#}" -lt 1 ] || [ "${#}" -gt 1 ]; then
  639.                               echo -e "grep running processes\n\
  640.                              usage: psg [process]"
  641.                             else
  642.                               ps -ef | grep -v grep | grep -i "${1}" | less
  643.                             fi
  644.                             }
  645.  
  646. #########################################################################
  647. # INTERNETZ                                                             #
  648. #########################################################################
  649.  
  650. # Site down for everyone or just me?
  651. function cust-downforme()  {
  652.                            if [ ${#} = 0 ]
  653.                            then
  654.                              echo -e "usage: cust-downforme website_url"
  655.                            else
  656.                              JUSTYOUARRAY="$(lynx -dump http://downforeveryoneorjustme.com/${1} | grep -o "It's just you")"
  657.                              if [ ${#JUSTYOUARRAY} != 0 ]
  658.                              then
  659.                                echo -e "It's just you. \n${1} is up."
  660.                              else
  661.                                echo -e "It's not just you! \n${1} looks down from here."
  662.                              fi
  663.                            fi
  664.                            }
  665. # Search IMDB.COM
  666. function imdb()            { firefox "http://www.imdb.com/find?s=all&q="${@}"&x=0&y=0" & }
  667.  
  668. # send to pastebin
  669. function cust-2pastebin()  {
  670.                            # first things first: check for curl
  671.                            [ -x "$(which curl)" ] || { echo "this script depends on curl" ; return 0; }
  672.                            # check for stdin, if so, get it :)
  673.                            if [ "$( tty )" == 'not a tty' ] && [ ${#} = 1 ]
  674.                            then
  675.                              INPUT="$(</dev/stdin)"
  676.                            else
  677.                              { echo "Usage: echo plop | cust-2pastebin <name>" ; return 0; }
  678.                            fi
  679.                            # check for empty input
  680.                            [ -z ${INPUT} ] && { echo "Usage: echo plop | ${0}"; return 0; }
  681.  
  682.                            # set empty var:
  683.                            NAME="${1}"
  684.                            EMAIL=""
  685.                            TYPE="bash"
  686.                            API="http://pastebin.com/api_public.php"
  687.                            QUERYSTRING="paste_private=0&paste_code=${INPUT}&paste_name=${NAME}&paste_email=${EMAIL}&paste_format=${TYPE}"
  688.  
  689.                            #post data to pastebin.com API
  690.                            curl -d "${QUERYSTRING}" "${API}"
  691.                            echo ""
  692.                            }
  693.  
  694. # download torrent
  695. function cust-torrent()    { ( cd ~/Downloads/ && curl -O "${1}"); }
  696.  
  697. #########################################################################
  698. # INTERNETZ # Youtube stuff                                             #
  699. #########################################################################
  700.  
  701. # stream YouTube videos directly to your media player
  702. function cust-youtube2mplayer()  {
  703.                            video_id=$(curl -s ${1} | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p");
  704.                            mplayer -fs $(echo "http://youtube.com/get_video.php?$video_id");
  705.                            }
  706. # convert to ogg
  707. function cust-youtube2ogg(){ ffmpeg -i "${1}" -vn "${1}".ogg; }
  708.  
  709. # play YouTube's first match directly
  710. function cust-ytplay()     {
  711.                            args="$*";mplayer -fs $(youtube-dl -g "http://www.youtube.com$(lynx --source \
  712.                           "http://www.youtube.com/results?search_query=${args// /+}&aq=f"|grep -m1 '<a id=.*watch?v=.*title'|\
  713.                           cut -d\" -f4)")
  714.                            }
  715.  
  716. # define a word - USAGE: define dog
  717. function cust-define()     {
  718.                            local LNG=$(echo $LANG | cut -d '_' -f 1)
  719.                            local CHARSET=$(echo $LANG | cut -d '.' -f 2)
  720.                            lynx -accept_all_cookies -dump -hiddenlinks=ignore -nonumbers -assume_charset="$CHARSET" -display_charset="$CHARSET" \
  721.                            "http://www.google.com/search?hl=${LNG}&q=define%3A+${1}&btnG=Google+Search" | grep -m 5 -C 2 -A 5 -w "*" > /tmp/define
  722.                            if [ ! -s /tmp/define ]; then
  723.                              echo "Sorry, google doesn't know this one..."
  724.                              rm -f /tmp/define
  725.                              return 1
  726.                            else
  727.                              cat /tmp/define | grep -v Search
  728.                              echo ""
  729.                            fi
  730.                            rm -f /tmp/define
  731.                            return 0
  732.                            }
  733.  
  734. # detect language of a string
  735. function cust-detectlang() { curl -s "http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=$@" | sed 's/{"responseData": {"language":"\([^"]*\)".*/\1\n/'; }
  736.  
  737. # translate a word using Google
  738. # usage: translate <phrase> <output-language>
  739. function cust-translate()  {
  740.                            [ ${#} = 2 ] || { echo "usage: translate <phrase> <output-language>" ; return 0; }
  741.                            wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=%7C${2:-en}" | sed 's/.*{"translatedText":"\([^"]*\)".*/\1\n/'
  742.                            }
  743.  
  744. # look in Webster
  745. function cust-webster()    { curl dict://dict.org/d:${1}:web1913; }
  746.  
  747. # look in WordNet
  748. function cust-wordnet()    { curl dict://dict.org/d:${1}:wn; }
  749.  
  750. # Using PIPEs, Execute a command, convert output to .png file, upload file to imgur.com, then returning the address of the .png
  751. function cust-imgur()      {
  752.                            convert label:@- png:-|curl -F "image=@-" -F "key=1913b4ac473c692372d108209958fd15" http://api.imgur.com/2/upload.xml| \
  753.                            grep -E -o "<original>(.)*</original>" | grep -E -o "http://i.imgur.com/[^<]*"
  754.                            }
  755.  
  756. # Query Wikipedia via console over DNS
  757. function cust-wikipedia()  {
  758.                            [ ${#} != 0 ] || { echo "usage: cust-wikipedia <string>" ; return 0; }                        
  759.                            STRING="$( echo "${@}" | sed -e 's/ /_/g' )"
  760.                            dig +short txt ${STRING}.wp.dg.cx
  761.                            }
  762.  
  763. # Auto send an attachment from CLI  
  764. function cust-mail-file()  {
  765.                            [ ${#} != 2 ] || { echo "usage: cust-mail-file <file> <rcpt>" ; return 0; }
  766.                            echo "File auto-sent from $(hostname -f) by $(whoami)." | mutt -s "See Attached File" -a "${1}" "${2}"
  767.                            }
  768.  
  769. # Create an easy to pronounce shortened URL from CLI
  770. function cust-shorturl()   {
  771.                            [ ${#} != 1 ] || { echo "usage: cust-shorturl\"Long URL\"" ; return 0; }
  772.                            curl -s "http://shoutkey.com/new?url=${1}" | sed -n 's/\<h1\>/\&/p' | sed 's/<[^>]*>//g;/</N;//b'
  773.                            }
  774.  
  775. # Transmission with blocklists
  776. function cust-torrentbl()  {
  777.                            pkill transmission-gtk
  778.                            BLOCKLISTDIR="$HOME/.config/transmission/blocklists"
  779.                            BASEURL="http://list.iblocklist.com/?list="
  780.                            ENDURL="&fileformat=p2p&archiveformat=gz"
  781.                            FILES=( bt_level1 bt_level2 bt_level3 us bt_dshield jcjfaxgyyshvdbceroxf ijfqtofzixtwayqovmxn bt_templist dufcxgnbjsdwmwctgfuj )
  782.                            cd $BLOCKLISTDIR && /bin/rm *.*
  783.                            for i in "${FILES[@]}"
  784.                            do
  785.                              curl -L $BASEURL"$i"$ENDURL -o $BLOCKLISTDIR/$i.gz
  786.                              gunzip -f $BLOCKLISTDIR/$i.gz
  787.                              mv $BLOCKLISTDIR/$i $BLOCKLISTDIR/$i.txt
  788.                            done
  789.                            transmission-gtk &
  790.                            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
  791.                            }
  792.                            
  793.  
  794. # show just first 3 octets of IP address and runs nmap just as original function.
  795. function cust-subnet()     {
  796.                            if [ -n "$1" ]; then
  797.                              net="$1"
  798.                            else
  799.                              net=$(cat /etc/resolv.conf | grep 'nameserver' | cut -c12-26 | awk -F '.' '{print $1"."$2"."$3".0/24"}')
  800.                            fi
  801.                            echo "testing $net for online hosts"
  802.                            nmap -sP $net | awk '/Host/ && /up/ { print $0; }'
  803.                            echo "done"
  804.                            }
  805.  
  806. #########################################################################
  807. # Good bash tips for everyone                                           #
  808. #########################################################################
  809.  
  810. function bashtips()        {
  811. cat <<EOF
  812. DIRECTORIES
  813. -----------
  814. ~-          Previous working directory
  815. pushd tmp   Push tmp && cd tmp
  816. popd        Pop && cd
  817. GLOBBING AND OUTPUT SUBSTITUTION
  818. --------------------------------
  819. ls a[b-dx]e Globs abe, ace, ade, axe
  820. ls a{c,bl}e Globs ace, able
  821. \$(ls)      \`ls\` (but nestable!)
  822. HISTORY MANIPULATION
  823. --------------------
  824. !!        Last command
  825. !?foo     Last command containing \`foo'
  826. ^foo^bar^ Last command containing \`foo', but substitute \`bar'
  827. !!:0      Last command word
  828. !!:^      Last command's first argument
  829. !\$       Last command's last argument
  830. !!:*      Last command's arguments
  831. !!:x-y    Arguments x to y of last command
  832. C-s       search forwards in history
  833. C-r       search backwards in history
  834. LINE EDITING
  835. ------------
  836. M-d     kill to end of word
  837. C-w     kill to beginning of word
  838. C-k     kill to end of line
  839. C-u     kill to beginning of line
  840. M-r     revert all modifications to current line
  841. C-]     search forwards in line
  842. M-C-]   search backwards in line
  843. C-t     transpose characters
  844. M-t     transpose words
  845. M-u     uppercase word
  846. M-l     lowercase word
  847. M-c     capitalize word
  848. COMPLETION
  849. ----------
  850. M-/     complete filename
  851. M-~     complete user name
  852. M-@     complete host name
  853. M-\$    complete variable name
  854. M-!     complete command name
  855. M-^     complete history
  856. EOF
  857.  
  858.                       }
Advertisement
Add Comment
Please, Sign In to add comment