summaryrefslogtreecommitdiff
path: root/linux-cleaner.sh
blob: a03e8d87b476c3483fc55a669a3920b5a0a973b6 (plain)
    1 #!/usr/bin/env bash
    2 #
    3 # A Linux system generalizer to prep for problem-free cloning
    4 # Copyright (C) 2016  Aaron Ball <nullspoon@oper.io>
    5 #
    6 # This program is free software: you can redistribute it and/or modify
    7 # it under the terms of the GNU General Public License as published by
    8 # the Free Software Foundation, either version 3 of the License, or
    9 # (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 #
   19 #
   20 # Description
   21 # -----------
   22 #
   23 # **Danger, Will Robinson! Danger!**
   24 # This script is catastrophic!
   25 #
   26 # This script will generalize the system it is executed on, and remove itself
   27 # afterwards, leaving no trace.
   28 #
   29 # The intent is to produce a linux system that can be cloned without having
   30 # duplicate mac addresses, duplicate ssh host keys, strange unexpected shell
   31 # histories, etc.
   32 #
   33 # NOTE: This only fully works on Centos 6 and 7 presently.
   34 #
   35 
   36 # Set the log file
   37 # If not set, logs will be output to stdout
   38 #logfile=/root/cleanup.log
   39 
   40 function log {
   41   timestamp=$(date '+%F %T')
   42   if [[ -z ${logfile} ]]; then
   43     # Output to stdout if no logfile is specified
   44     echo "[${timestamp}]: ${*}"
   45   else
   46     # Logfile var set. Output there.
   47     echo "[${timestamp}]: ${*}" >> ${logfile}
   48   fi
   49 }
   50 
   51 
   52 function main {
   53   if [[ $(id -u) != 0 ]]; then
   54     echo "This script must be run as root, which you are not."
   55     return 1
   56   fi
   57 
   58   log "Initializing cleanup"
   59 
   60   log "Removing ssh host_keys"
   61   rm -f /etc/ssh/ssh_host_*
   62 
   63 
   64   log "Preventing root command history from being written"
   65   unset HISTFILE
   66   log "Cleanup of root history file"
   67   rm -f /root/.bash_history
   68 
   69 
   70   log "Removing hard-coded mac addresses."
   71   service network stop
   72   local ifcfg_files=/etc/sysconfig/network-scripts/ifcfg-e*
   73   sed -i '/^HWADDR=.*$/d' ${ifcfg_files}
   74 
   75 
   76   log "Cleaning up nic udev rules..."
   77   # Centos 6 udev net rules file
   78   local net_6udev='/etc/udev/rules.d/70-persistent-net.rules'
   79   [[ -f "${net_6udev}" ]] && rm -f "${net_6udev}"
   80 
   81   # CentOS 7 udev net rules file
   82   local net_7udev='/etc/udev/rules.d/70-persistent-ipoib.rules'
   83   [[ -f "${net_7udev}" ]] && rm -f "${net_7udev}"
   84 
   85   # Delete myself (don't worry, I'll still exist in your memories/ram)
   86   rm -f ${mypath}
   87 
   88   log "Cleanup complete. Shutting down in 5 seconds."
   89 
   90   # Sleep for a few seconds so the user has a chance to cancel
   91   sleep 5
   92 
   93   # Shutdown
   94   shutdown -h now
   95 }
   96 
   97 main ${@}

Generated by cgit