Scripting presentation

From kipiki
Jump to: navigation, search
#!/bin/bash

# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.

## Dan Bidleman 7/18/18
## Best viewed with VIM
## :syntax on
## :set bg=dark
## :colo elflord
## DRB 8/15/18 - Additional functions for second presentation

## Welcome to Bash - Bourne Again SHell

## This script aims to show some basic usage of bash scripting and how you can use it to make interesting and useful things.
## A few great resources for learning bash are:
## The venerable 'The Linux Documentation Project' or TLDP - http://tldp.org
## cheat.sh - http://cheat.sh (works from curl or webbrowser)
## man pages
## I'm sure there are a LOT others, one off the top of my head thats always been good: nixcraft blog (https://www.cyberciti.biz/)

## anything prefaced (except the #!/bin/bash) with a # is a comment for the rest of the line

# We don't like running things as root, well lets keep others from doing it too:
if [[ $EUID -eq 0 ]]; then
  echo "This script must be run as a NON-ROOT user, goodbye"
  exit 1 ## the exit 1 status is an error (anything non-zero is a bad status, 
         ## you can use different exit codes in your program to help the end user to find and identify issues)
fi

## functions need to be defined before they are called
##Lets make our first function, the menu, it will be called over and over
function menu {
clear
echo -e "\e[32mWelcome to JaxLUG Bash Scripting presentation!"
echo "Your options are as follow:"

select ACTION in "doom" "tux" "expansion" "arrays" "sedf" "privescal" "combo" "colors" "remote" "exit"
do
  echo
  echo "Your Choice: $ACTION"
  echo
  break
done

# call the other functions based on user input, showing a few ways to do the if statements
if [ "$ACTION" = "doom" ]; then
  echo running doom
  doom
fi
if [ "$ACTION" == tux ]; then
  echo running tux
  tux
fi
if [ "$ACTION" == expansion ]; then echo running expansion; expansion; fi
if [ "$ACTION" == arrays ]; then echo running array; arrays; fi
if [ "$ACTION" == sedf ]; then echo running sed; sedf; fi
if [ "$ACTION" == privescal ]; then echo running privescal; privescal; fi
if [ "$ACTION" == combo ]; then echo running combo; combo; fi
if [ "$ACTION" == colors ]; then echo running colors; colors; fi
if [ "$ACTION" == remote ]; then echo running remote; remote; fi
if [ "$ACTION" == exit ]; then echo have a nice day; exit 0; fi

}

# this function plays the doom music video, uses an if/else to do the selection
function doom {
echo Would you like to play it in full screen or original size?
echo 1 - Full Screen
echo 2 - Original size
read -n 1 SIZE
if [ $SIZE -eq 1 ]; then
    mplayer -fs DOOM-\ Stayin\ Alive-7B89wYMsiy0.mkv
  elif [ $SIZE -eq 2 ]; then
    mplayer DOOM-\ Stayin\ Alive-7B89wYMsiy0.mkv
  else
    echo You failed to enter a proper entry, goodbye
fi
echo Press return to go back to the main menu:
read
menu
}

# give poor lil tux a fish
function tux () {
# both while's will work, the while true needs the break in the then logic, whereas the give != fish doesn't
#while [ "$GIVE" != fish ]; do
while true; do
if [ -z $1 ]; then
  echo Tux wants a fish, what do you give him?
  read GIVE;
 else
  GIVE=$1
fi
if [ "$GIVE" == fish ];
  then
    echo "Mmmmm fish, Tux happy!"
    if [ ! -z $1 ]; then exit 0; fi
    sleep 1
    break
  else
    echo "Tux doesn't like that, Tux wants fish!"
    sleep 1
    if [ ! -z $1 ]; then exit 0; fi
fi
done
echo Press return to go back to the main menu:
read
menu
}

# here we will show bash expansion
function expansion {
echo Today\'s date is `date`
echo This is an example of brace expansion
echo sp{el,il,al}l
echo This is using bash expansion to list a significant count of numbers without effort
echo {3..50}

echo Press return to go back to the main menu:
read
menu
}

# arrays!
function arrays {
echo you can put things into arrays and then pluck them out at will:
FOO=(bobs your uncle)
echo pulling the all of the variables:
echo ${FOO[*]}
echo pulling just the middle variable:
echo ${FOO[1]}
echo changing the middle variable and then re-printing them
FOO[1]="my"
echo ${FOO[*]}

echo Press return to go back to the main menu:
read
menu
}

# These functions are part of the second presentation
# sed and tr and pipe
function sedf {
sedvar="The red duck polevaulted over the building"
echo    "This is the original: $sedvar"
echo -n "This is sed modified: "
echo $sedvar | sed 's/red/green/' 
echo -n "This is tr modified : "
echo $sedvar | tr [:lower:] [:upper:]

echo Press return to go back to the main menu:
read
menu
}

# privilage escalation example
function privescal {
echo foo > rootowned/foo
sudo echo foo2 > rootowned/foo2
sudo bash -c 'echo foo3 > rootowned/foo3'

echo Press return to go back to the main menu:
read
menu
}

# combos! - priv escalation, find, grep, pipes, inline logic, redirects, tee, awk, pipe exit codes
function combo {
# pipe, find, priv, grep, tee, awk pipexitcodes
for i in `find /var/log -mtime -30 -name messages.?.gz`; do sudo zgrep ACPI $i | grep "EC:"; done | awk '{print $1 "_" $2 "_" $3}' | sort | uniq -c | tee combo.out
echo ""

# pipe exit status
false | true
echo "first: ${PIPESTATUS[0]} second: ${PIPESTATUS[1]}"
echo ""

#inline logic
echo exitnomatter0 ; false ; echo exitnomatter1
echo exitmatters0 && false && echo exitmatters1
echo ""

# redirects
echo "firstline foo1" > foo1
echo "secondline foo1" > foo1

echo "firstline foo2" >> foo2
echo "secondline foo2" >> foo2

echo "firstline foo3" > foo3
echo "secondline foo3" > foo3

echo Press return to go back to the main menu:
read
menu
}

# terminal colors
function colors {
echo -e "You can make all kinds of colors including \e[32mGreen \e[34mBlue \e[0m white and \e[31mRed \e[39m"

## thanks to the below for the following script:
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
for fgbg in 38 48 ; do # Foreground / Background
    for color in {0..255} ; do # Colors
        # Display the color
        printf "\e[${fgbg};5;%sm  %3s  \e[0m" $color $color
        # Display 6 colors per lines
        if [ $((($color + 1) % 6)) == 4 ] ; then
            echo # New line
        fi
    done
    echo # New line
done

echo Press return to go back to the main menu:
read
menu
}

# remote system execution
function remote {
ssh jaxlug.org "/usr/bin/systemctl status httpd"

echo Press return to go back to the main menu:
read
menu
}

# This is the start of the program (minus the root check)
# functions above are called by invocation here

# here we are getting the options, h does not need a argument but t does
# this also shows case statements
while getopts "ht:" OPT; do
  case "${OPT}" in
    h)
      echo This is the help menu for the bash script
      echo -h - this menu
      echo "-t <food> - give tux a food item and see if he likes it"
      exit 0
      ;;
    t)
      echo Lets give tux a $2 and see if he likes it:
      tux $2 
      ;;
    [?])
      echo invalid option, please see -h
      exit 1
      ;;
  esac
exit 0
done

# here we call the menu if no arguments have been passed
menu