Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active March 11, 2024 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quonic/7c2f0fbc73e269bb374911be1f91461b to your computer and use it in GitHub Desktop.
Save quonic/7c2f0fbc73e269bb374911be1f91461b to your computer and use it in GitHub Desktop.
Get a list linux of packages ( apt, dnf, yum ) and containers ( flatpak, snap ) that have packages available to be updated and save them to custom fields. Mainly used with NinjaRMM.
#!/usr/bin/env bash
# Licence: MIT or use how you like
# Description:
# This script is used to get a list of system packages that can be updated and save them to a Custom Field.
# It also gets a list of snaps and flatpaks that can be updated and saves them to a Custom Field.
# Create the following Script Variables:
#
# packagesCustomField as a text field
# containersCustomField as a text field
# Create two multi-line Custom Fields.
# Example:
# - Packages
# - Containers
# Supported package managers:
# - dnf
# - apt
# - yum
# Supported container managers:
# - flatpak
# - snap
# Set the Custom Fields to use from the script variables or from the command line arguments
if [[ -n "$1" ]]; then
packages_cf=$1
fi
if [[ -n "$2" ]]; then
containers_cf=$2
fi
if [[ -n "${packagesCustomField}" && "${packagesCustomField}" != "null" ]]; then
packages_cf="${packagesCustomField}"
fi
if [[ -n "${containersCustomField}" && "${containersCustomField}" != "null" ]]; then
containers_cf="${containersCustomField}"
fi
# Function: SetCustomField
#
# Description:
# This function is used to set a custom field value.
#
# Parameters:
# - $1: The name of the custom field
# - $2: The value to set the custom field to
#
# Returns:
# - 0: Successfully set custom field
# - 1: Failed to set custom field
# - 2: NinjaRMM CLI not found or not executable
function SetCustomField() {
if [ -f "/opt/NinjaRMMAgent/programdata/ninjarmm-cli" ] && [ -x "/path/to/file" ]; then
if /opt/NinjaRMMAgent/programdata/ninjarmm-cli set "$1" "$2"; then
# Successfully set custom field
return 0
else
# Failed to set custom field
return 1
fi
else
# NinjaRMM CLI not found or not executable
return 2
fi
}
# List system packages that can be updated
if [ "$(command -v dnf)" ]; then
# RPM-based distributions
system_packages=$(dnf list upgrades)
system_type="dnf"
elif [ "$(command -v apt)" ]; then
# Debian-based distributions
system_packages=$(apt list --upgradable)
system_type="apt"
elif [ "$(command -v yum)" ]; then
# Red Hat-based distributions
system_packages=$(yum check-update)
system_type="yum"
fi
echo ""
echo "System packages that could be updated via: ${system_type}"
echo ""
echo "${system_packages}"
echo ""
if [[ -n "${packages_cf}" ]]; then
echo "Saving updatable packages to Custom Field: ${packages_cf}"
if [[ $(SetCustomField "${packages_cf}" "${system_packages}") -eq 0 ]]; then
echo "Saved updatable packages to Custom Field."
else
echo "Failed to save updatable packages to Custom Field."
fi
fi
# List snaps and flatpaks that can be updated
if [ "$(command -v flatpak)" ]; then
# Flatpak containers
container_packages=$(flatpak remote-ls --updates)
container_type="flatpak"
elif [ "$(command -v snap)" ]; then
# Snap containers
container_packages=$(snap refresh --list)
container_type="snap"
fi
echo ""
echo "Container packages that could be updated via: ${container_type}"
echo ""
echo "${container_packages}"
echo ""
if [[ -n "${containers_cf}" ]]; then
echo "Saving updatable packages to Custom Field: ${containers_cf}"
if [[ $(SetCustomField "${containers_cf}" "${container_packages}") -eq 0 ]]; then
echo "Saved updatable containers to Custom Field."
else
echo "Failed to save updatable containers to Custom Field."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment