Skip to content

Instantly share code, notes, and snippets.

@victorbrca
Created March 31, 2023 21:11
Show Gist options
  • Select an option

  • Save victorbrca/e42629121b0ee079f91f74c35fbf8ae9 to your computer and use it in GitHub Desktop.

Select an option

Save victorbrca/e42629121b0ee079f91f74c35fbf8ae9 to your computer and use it in GitHub Desktop.

Revisions

  1. victorbrca created this gist Mar 31, 2023.
    82 changes: 82 additions & 0 deletions apt-history.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    function apt-history() {
    local install_date login_user command type changed usage

    usage="apt-history - Displays content from /var/log/apt/history* in a table format (similar to yum history)"
    if [ $# -gt 0 ] && [[ "$1" == "-h" ]] ; then
    echo "$usage"
    return 0
    elif [ $# -gt 0 ] ; then
    echo "This function does not accept options"
    return 1
    fi


    # Make sure we are root
    if [[ $(id -u) -ne 0 ]] ; then
    echo "You need to be root"
    return 1
    fi

    # Makes sure log folder exists
    if [ ! -d /var/log/apt ] ; then
    echo "Missing the APT log folder"
    return 1
    fi

    apt_history_files="$(find /var/log/apt -name 'history*' | xargs ls -tr)"
    {
    echo " ID | Login user | Date and time | Command | Type | Altered |"
    echo " -- | ---------- | ------------------ | ----------------- | ------- | ------- |"

    for file in $apt_history_files ; do
    if echo "$file" | grep -q gz ; then
    cat_cmd="zcat"
    else
    cat_cmd="cat"
    fi
    $cat_cmd "$file" | while read line ; do
    # Gets the install date
    if echo "$line" | grep -q 'Start-Date' ; then
    install_date="$(echo "$line" | grep 'Start-Date' | awk -F':' '{print $2 ":" $3}')"
    fi

    # Gets the command
    if echo "$line" | grep -q 'Commandline' ; then
    command="$(echo "$line" | grep 'Commandline' | awk -F':' '{print $2}' | awk '{print $1 , $2}')"
    fi

    # Gets the user
    if echo "$line" | grep -q 'Requested-By:' ; then
    login_user="$(echo "$line" | grep 'Requested-By:' | awk -F':' '{print $2}' | awk '{print $1}')"
    fi

    # Gets the installed files
    if echo "$line" | grep -q 'Install' ; then
    type="Install"
    changed="$(echo "$line" | grep 'Install' | sed 's/),/)|/g' | tr '|' '\n' | wc -l)"
    fi

    if echo "$line" | grep -q 'Upgrade:' ; then
    type="Upgrade"
    changed="$(echo "$line" | grep 'Upgrade:' | sed 's/),/)|/g' | tr '|' '\n' | wc -l)"
    fi

    if echo "$line" | grep -q 'Remove:' ; then
    type="Remove"
    changed="$(echo "$line" | grep 'Remove:' | sed 's/),/)|/g' | tr '|' '\n' | wc -l)"
    fi

    # Outputs on end of block
    if echo "$line" | grep -q 'End-Date:' ; then
    # Fixes blank username
    if [ ! "$login_user" ] ; then
    login_user="system"
    fi

    echo " $login_user | $install_date | $command | $type | $changed | "
    unset install_date login_user command type changed
    fi
    done
    done | nl -s "|" | sort -r -t "|" -k1
    } | sed 's/|/,|,/g' | column -s "," -t | less
    }