#!/bin/bash #!/usr/bin/env bash start_time="$(date '+%Y-%m-%d_%H.%M.%S')" logdir=/tmp log="$logdir"/log_"$start_time".log log_x="$logdir"/log_x_"$start_time".log exec 3>&1 1>>"$log_x" 2>&1 set -x loginput() { echo "$*" >&3; echo "[INPUT] $(date '+%H:%M:%S') $*" >> "$log"; } logresponse() { echo "[RESPONSE] $(date '+%H:%M:%S') $*" >> "$log"; } loginfo() { echo "$*" >&3; echo "[INFO] $(date '+%H:%M:%S') $*" >> "$log"; } logwarn() { echo "$*" >&3; echo "[WARNING] $(date '+%H:%M:%S') $*" >> "$log"; } logerror() { echo "$*" >&3; echo "[ERROR] $(date '+%H:%M:%S') $*" >> "$log"; } # shellcheck disable=SC2329 catch_sigint () { logwarn "Signal Interrupt initiated. Stopping script." cleanup kill -INT "$$" } # shellcheck disable=SC2329 catch_exit () { cleanup kill -INT "$$" } # shellcheck disable=SC2329 cleanup () { loginfo "Cleaning up." pcie_disable loginfo "Unmounting drives." exec 1>/dev/null 2>&1 umount /mnt/"$ticket_number" rm -rf /tmp/"$ticket_number"/SOFTWARE rm -rf /tmp/"$ticket_number"/SYSTEM trap - EXIT trap - INT } trap catch_sigint SIGINT trap catch_exit EXIT get_netid () { netid= while [[ $netid =~ ^\s*$ ]]; do loginput "Enter R account netid: " read -r netid logresponse "$netid" if [[ $netid =~ ^\s*$ ]]; then logwarn "Your netid cannot be blank." else local ret_value=$(kinit "$netid"@EGR.MSU.EDU >&3; echo $?) if [[ ! $ret_value = "0" ]] then kdestroy netid= logwarn "Error when authenticating netid $netid." else clear loginfo "Authenticated as user $netid." fi fi done } get_ticket () { ticket_number= while [[ $ticket_number =~ ^\s*$ ]]; do loginput "Enter ticket number: " read -r ticket_number logresponse "$ticket_number" if [[ $ticket_number =~ ^\s*$ ]]; then logwarn "Ticket number cannot be blank." else mkdir -p /mnt/"$ticket_number" if ! mount -t cifs -o user="$netid",sec=krb5i //reinstallbackups/reinstallbackups/"$ticket_number" /mnt/"$ticket_number" then logerror "Failed to mount reinstallbackups. Directory does likely not exist" exit fi fi done } make_infolog () { loginfo "Reading backup to create a log." backupdir=/mnt/"$ticket_number" infolog="$logdir"/info_"$ticket_number"_"$start_time".log { echo "DETAILS" echo "Start Time: $start_time" echo "NetID: $netid" echo "" echo "PARTITION DETAILS" echo "Count: $(find "$backupdir"/* -maxdepth 0 -type d | wc -l)" } >> "$infolog" if [[ ! "$(find "$backupdir"/* -maxdepth 0 -type d | wc -l)" = 0 ]]; then for dir in "$backupdir"/*/; do if find "$dir" -maxdepth 3 -ipath "*/etc/os-release" | grep "." then loginfo "Linux install detected on in $dir." echo "Linux install detected on in $dir." >> "$infolog" echo "" >> "$infolog" { echo "LINUX DETAILS" cat "$(find "$dir" -maxdepth 3 -ipath "*/etc/os-release")" echo "Hostname: $(cat "$(find "$dir" -maxdepth 3 -ipath "*/etc/hostname")")" cat "$(find "$dir" -maxdepth 3 -ipath "*/etc/passwd")" | grep -E '.+:x:[0-9]{4,}:[0-9]{4,}:.+,,,.*' | sed -E 's/.+:x:[0-9]{4,}:[0-9]{4,}:(.+),,,.*/User: \1/' echo "" tree -r -a -t -L 1 -D "$(find "$dir" -maxdepth 3 -type d -ipath "*/home")" echo "" } >> "$infolog" loginfo "Reading $dir" echo "" >> "$infolog" elif find "$dir" -maxdepth 3 -ipath "*System32/config" -not -ipath "*Windows.old*" | grep "." then winpath=$(find "$dir" -maxdepth 3 -ipath "*System32/config" -not -ipath "*Windows.old*") loginfo "Windows install detected on in $dir." echo "Windows install detected on in $dir." >> "$infolog" echo "" >> "$infolog" winsoftwarepath=$(find "$winpath" -maxdepth 2 -ipath "*System32/config/SOFTWARE" -not -ipath "*Windows.old*") winsystempath=$(find "$winpath" -maxdepth 2 -ipath "*System32/config/SYSTEM" -not -ipath "*Windows.old*") cp "$winsoftwarepath" /tmp/log_backup_info.sh/SOFTWARE cp "$winsystempath" /tmp/"$ticket_number"/SYSTEM CurrentVersion=$(hivexregedit --export --unsafe-printable-strings --max-depth 1 --prefix \\HKEY_LOCAL_MACHINE\\SOFTWARE /tmp/"$ticket_number"/SOFTWARE '\Microsoft\Windows NT\CurrentVersion') ComputerName=$(hivexregedit --export --unsafe-printable-strings --max-depth 1 --prefix \\HKEY_LOCAL_MACHINE\\SYSTEM /tmp/"$ticket_number"/SYSTEM '\ControlSet001\Control\ComputerName\ComputerName') Parameters=$(hivexregedit --export --unsafe-printable-strings --max-depth 1 --prefix \\HKEY_LOCAL_MACHINE\\SYSTEM /tmp/"$ticket_number"/SYSTEM '\ControlSet001\Services\Tcpip\Parameters') LogonUI=$(hivexregedit --export --unsafe-printable-strings --max-depth 1 --prefix \\HKEY_LOCAL_MACHINE\\SOFTWARE /tmp/"$ticket_number"/SOFTWARE '\Microsoft\Windows\CurrentVersion\Authentication\LogonUI') ProfileList=$(hivexregedit --export --unsafe-printable-strings --max-depth 2 --prefix \\HKEY_LOCAL_MACHINE\\SOFTWARE /tmp/"$ticket_number"/SOFTWARE '\Microsoft\Windows NT\CurrentVersion\ProfileList') { echo "WINDOWS DETAILS" echo "" echo "$ComputerName" | grep -E '"ComputerName"=str\(1\):".+"' | sed -E 's/"ComputerName"=str\(1\):"(.+)"/Host Name: \1/' echo "$CurrentVersion" | grep -E '"DisplayVersion"=str\(1\):".+"' | sed -E 's/"DisplayVersion"=str\(1\):"(.+)"/Windows Version: \1/' echo "$CurrentVersion" | grep -E '"CurrentBuild"=str\(1\):".+"' | sed -E 's/"CurrentBuild"=str\(1\):"(.+)"/Current Build: \1/' echo "$CurrentVersion" | grep -E '"EditionID"=str\(1\):".+"' | sed -E 's/"EditionID"=str\(1\):"(.+)"/Windows Edition: \1/' echo "$CurrentVersion" | grep -E '"ProductName"=str\(1\):".+"' | sed -E 's/"ProductName"=str\(1\):"(.+)"/Product Name: \1/' echo "$CurrentVersion" | grep -E '"RegisteredOrganization"=str\(1\):".+"' | sed -E 's/"RegisteredOrganization"=str\(1\):"(.+)"/Registered Organization: \1/' echo "$CurrentVersion" | grep -E '"RegisteredOwner"=str\(1\):".+"' | sed -E 's/"RegisteredOwner"=str\(1\):"(.+)"/Registered Owner: \1/' echo "$Parameters" | grep -E '"Domain"=str\(1\):".+"' | sed -E 's/"Domain"=str\(1\):"(.+)"/Domain: \1/' echo "$LogonUI" | grep -E '"LastLoggedOnUser"=str\(1\):".+"' | sed -E 's/"LastLoggedOnUser"=str\(1\):"(.+)"/Last Logged On User: \1/' echo "$LogonUI" | grep -E '"LastLoggedOnDisplayName"=str\(1\):".+"' | sed -E 's/"LastLoggedOnDisplayName"=str\(1\):"(.+)"/Last Logged On User: \1/' echo "$ProfileList" | grep -E '"ProfileImagePath"=str\(2\):".+"' | sed -E 's/"ProfileImagePath"=str\(2\):"(.+)"/User: \1/' echo "" if find "$dir" -maxdepth 2 -type d -ipath "*/Users" -not -ipath "*Windows.old*" | grep "." then tree -r -a -t -L 1 -D "$(find "$dir"/ -maxdepth 2 -type d -ipath "*/Users" -not -ipath "*Windows.old*" | head -1)" fi if find "$dir" -maxdepth 2 -type d -ipath "*/Documents and Settings" -not -ipath "*Windows.old*" | grep "." then tree -r -a -t -L 1 -D "$(find "$dir"/ -maxdepth 2 -type d -ipath "*/Documents and Settings" -not -ipath "*Windows.old*" | head -1)" fi echo "" } >> "$infolog" else tree -a -R -D "$dir" >> "$infolog" fi done else logwarn "No partitions detected, device is likely empty." fi echo "END OF LOG" >> "$infolog" echo "" >&3 cat "$infolog" >&3 echo "" >&3 cp "$infolog" /mnt/"$ticket_number" } main (){ loginfo "" get_netid get_ticket mount_remote loginfo "" make_infolog #Logs a bunch of details to a info log file from the system. loginfo "" loginfo "Finished gathering logs of $ticket_number." cleanup } main