Fedora And Red Hat System Administration/Bash Scripting

< Fedora And Red Hat System Administration

Basics edit

Handling Arguments edit

This program will display all arguments:

 for ARG; do
     echo "had arg: $ARG"
 done

Example Functions edit

Launch Apps and Set Niceness edit

 showdates () {
      cal
      date
      ddate
 }
 
 donice () {
     COMMAND=$1
     NICEVAL=$(grep "^$COMMAND" ~/.nice-settings | cut -d: -f2)
     if test -n "$NICEVAL"
     then
         nice -n "$NICEVAL" $*
     else
         nice $*
     fi
 }
 
 grpshare () {
     if [ $1 = '-g' ]; then
         SET_GROUP=$2
         shift; shift
     else
         SET_GROUP=users
     fi
 
     for FILE; do
         chgrp $SET_GROUP -R $FILE
         chmod g+rw -R $FILE
     done
 }

Example Scripts edit

Setting Environment Variable edit

 #!/bin/bash
 
 for PROFILE in $(find /home/ -name .bash_profile)
 do
     if egrep -q '^(export[[:space:]]+)?EDITOR' $PROFILE
     then
        # User already set an editor, override that
        sed -i.orig -r 's/^(export[[:space:]]+)?EDITOR=.*/export EDITOR=nano/' $PROFILE
     else
         # No previous editor was set
         echo "export EDITOR=/usr/bin/nano" >>$PROFILE
     fi
 done

Setting Aliases edit

 #!/bin/bash
 
 for BASHRC in $(find /home/ -name .bashrc)
 do
     for ALIAS in "alias rm='rm -i'" "alias cp='cp -i'" "alias mv='mv -i'"
     do
         ALIAS_CMD=$(echo $ALIAS | cut -d= -f1)
         if ! egrep -q "^$ALIAS_CMD" $BASHRC
         then
             echo $ALIAS >>$BASHRC
         fi
     done
 done

Checking MD5s for SUID and SGID Executables edit

 #!/bin/bash
 # /root/bin or /usr/local/bin would be a good place for this script
 # Probably need to run this as root, otherwise many errors will show up about
 # permission problems with reading these files.
 NOTIFY_EMAIL=user@station.example.com
 
 # Assume (for now) that we already have a /etc/suid.md5 to compare new file to
 find / -type f -perm +6000 -exec md5sum {} \; >/etc/suid.md5.new
 
 if diff /etc/suid.md5 /etc/suid.md5.new &>/tmp/suid-check-diff.out
 then
     echo "No SUIDs have changed"
 else
     if [ "$1" = "interactive" ]
     then
         cat /tmp/suid-check-diff.out
         echo "Check FAILED!  SUID executable(s) have changed!"
         read -p "Is this okay? (yes/no) " RESPONSE
         if [ "$RESPONSE" = "yes" ]
         then
             mv /etc/suid.md5.new /etc/suid.md5
         fi
     else
         echo "Check FAILED!  SUID executable(s) have changed!" | mail -s "SUID Change" $NOTIFY_EMAIL
     fi
 fi

Checking URLs for New Content edit

 #!/bin/bash
 
 URL_FILE=$HOME/.content-check-urls
 URL_DIR=$HOME/.content-check
 
 if ! [ -d $URL_DIR ]; then
     mkdir $URL_DIR
 fi
 
 if ! [ -f $URL_FILE ]; then
     echo "new-content-check: $URL_FILE not found"
     exit 1
 fi
 
 for URL in $(cat $URL_FILE); do
     MD5_FILE=$URL_DIR/$(echo $URL | md5sum | cut -d' ' -f1)
 
     if [ -f $MD5_FILE ]; then
         # Looks like we´ve got an old version of this data, gotta check it
         links -dump "$URL" | md5sum > $MD5_FILE.new
         if ! diff $MD5_FILE $MD5_FILE.new &>/dev/null; then
             echo "New content at $URL"
             mv -f $MD5_FILE.new $MD5_FILE
         else
             rm $MD5_FILE.new
         fi
     else
         # No old version, just store the new sum
         links -dump "$URL" | md5sum > $MD5_FILE
     fi
 done

Connecting via ssh using keys edit

 #!/bin/bash
 SUCCESS=0
 WRONG_ARGS=65
 if [ $# -ne 2 ]
 then
  echo "Uso: `basename $0` user host"
  echo "Es : `basename $0` myuser host.example.com"
  exit $WRONG_ARGS
 fi
 if [ ! -e "$HOME/.ssh/id_rsa.pub" ];
 then
 echo "missing rsa key:"
 echo "run \"ssh-keygen -t rsa -b 2048\" and try again"
 exit $WRONG_ARGS
 else
 echo rsa public key found
 fi
 #Creating check file
 echo    "#!/bin/bash">check.key.sh
 echo    "if test -n \"\`ls .*|grep ssh\`\"">>check.key.sh
 echo    " then">>check.key.sh
 echo    " if test -n \"\`grep \"`cat $HOME/.ssh/id_rsa.pub|cut -d " " -f2`\" .ssh/authorized_keys2\`\"">>check.key.sh
 echo    "   then">>check.key.sh
 echo    "   echo public key found">>check.key.sh
 echo    "  else">>check.key.sh
 echo    "   echo missing public key:">>check.key.sh
 echo    "   echo putting public key on remote keyring">>check.key.sh
 echo    "   cat id_rsa.pub >>.ssh/authorized_keys2">>check.key.sh
 echo    "  fi">>check.key.sh
 echo    "else">>check.key.sh
 echo    "echo missind directory .ssh: creating ...">>check.key.sh
 echo    " mkdir -p .ssh">>check.key.sh
 echo    " echo creating remote keyring and copying public key ...">>check.key.sh
 echo    " cp id_rsa.pub .ssh/authorized_keys2">>check.key.sh
 echo    "fi">>check.key.sh
 echo    " echo setting keyring permissions ...">>check.key.sh
 echo    " chmod 600 .ssh/authorized_keys2">>check.key.sh
 echo    " echo setting directory permissions">>check.key.sh
 echo    " chmod 700 .ssh">>check.key.sh
 echo    "echo check key finished">>check.key.sh
 echo    "echo \"Please run the following to access the host again\"">>check.key.sh
 echo    "echo ssh $1@$2">>check.key.sh
 echo    "echo deleting check files ...">>check.key.sh
 echo    "rm id_rsa.pub">>check.key.sh
 echo    "rm check.key.sh">>check.key.sh
 chmod +x check.key.sh
 scp -p ~/.ssh/id_rsa.pub check.key.sh $1@$2:
 rm check.key.sh 
 ssh $1@$2 ./check.key.sh
 ssh $1@$2