2
0
Files
TacticalRMM_Scripts/Supporting_scripts/Linux - Update TRMM agent.sh

194 lines
6.9 KiB
Bash

#!/bin/bash
# Usage: "./Linux - Update TRMM agent.sh" update amd64
#
# Description:
# This script can be used to download the latest version of a Tactical RMM agent, compile it and replace/update it.
# Preferrably this script is run in combination with the scheduling script that can be found under Task scripts in this repository.
#
# Arguments:
# update - Used to specify that the agent needs to be updated
# [SystemType] - Specify what type of system the agent needs to be updated on ('amd64' 'x86' 'arm64' 'armv6')
#
# Author: D. de Kooker (info@dcomputers.nl)
# Sources: https://github.com/netvolt/LinuxRMM-Script/blob/main/rmmagent-linux.sh
# Changelog:
# 06-09-2023 - Change the script from source to exclude the Install and uninstall part,
# adjusted it so it could be run on it's own (or by scheduled task script with TRMM) to update a linux agent.
check_zip=$(which unzip 2> /dev/null)
if [[ $check_zip == "" || $check_zip =~ .*"no unzip".* ]]; then
echo "unzip could not be found. Trying to install automatically."
if [[ `which yum` ]]; then
yum -y install unzip
echo "Unzip installed. Continue script..."
elif [[ `which apt` ]]; then
apt-get -y update
apt-get -y install unzip
echo "Unzip installed. Continue script..."
else
echo "unzip could not be installed. Please install unzip manually."
fi
exit 1
fi
if [[ $1 == "" ]]; then
echo "First argument is empty !"
echo "Type help for more information"
exit 1
fi
if [[ $1 == "help" ]]; then
echo "There is help but more information is available at github.com/ZoLuSs/rmmagent-script"
echo ""
echo "List of UPDATE argument (no argument name)"
echo "Arg 1: 'update'"
echo "Arg 2: System type 'amd64' 'x86' 'arm64' 'armv6'"
echo ""
echo ""
exit 0
fi
if [[ $1 != "update" ]]; then
echo "First argument can only be 'update' !"
echo "Type help for more information"
exit 1
fi
if [[ $1 == "update" && $2 == "" ]]; then
echo "Argument 2 (System type) is empty !"
echo "Type help for more information"
exit 1
fi
## Setting var for easy scription
system=$2
go_version="1.22.1"
go_url_amd64="https://go.dev/dl/go$go_version.linux-amd64.tar.gz"
go_url_x86="https://go.dev/dl/go$go_version.linux-386.tar.gz"
go_url_arm64="https://go.dev/dl/go$go_version.linux-arm64.tar.gz"
go_url_armv6="https://go.dev/dl/go$go_version.linux-armv6l.tar.gz"
function check_profile () {
source /etc/environment
profile_file="/root/.profile"
path_count=$(cat $profile_file | grep -o "export PATH=/usr/local/go/bin" | wc -l)
if [[ $path_count -ne 0 ]]; then
echo "Removing incorrect \$PATH variable\(s\)"
sed -i "/export\ PATH\=\/usr\/local\/go\/bin/d" $profile_file
fi
path_count=$(cat $profile_file | grep -o "export PATH=\$PATH:/usr/local/go/bin" | wc -l)
if [[ $path_count -ne 1 ]]; then
echo "Fixing \$PATH Variable"
sed -i "/export\ PATH\=\$PATH\:\/usr\/local\/go\/bin/d" $profile_file
echo "export PATH=\$PATH:/usr/local/go/bin" >> $profile_file
fi
source $profile_file
}
function go_install() {
if ! command -v go &> /dev/null; then
## Installing golang
case $system in
amd64)
wget -O /tmp/golang.tar.gz $go_url_amd64
;;
x86)
wget -O /tmp/golang.tar.gz $go_url_x86
;;
arm64)
wget -O /tmp/golang.tar.gz $go_url_arm64
;;
armv6)
wget -O /tmp/golang.tar.gz $go_url_armv6
;;
esac
rm -rvf /usr/local/go/
tar -xvzf /tmp/golang.tar.gz -C /usr/local/
rm /tmp/golang.tar.gz
export GOPATH=/usr/local/go
export GOCACHE=/root/.cache/go-build
echo "Go is installed (version $go_current_version)."
else
# Get the current version of Go installed
go_current_version=$(go version | awk '{print $3}' | sed 's/go//')
if [ "$go_current_version" != "$go_version" ]; then
echo "Version mismatch. Current installed version is $go_current_version. Desired version is $go_version."
echo "Installing Go $go_version..."
## Installing golang
case $system in
amd64)
wget -O /tmp/golang.tar.gz $go_url_amd64
;;
x86)
wget -O /tmp/golang.tar.gz $go_url_x86
;;
arm64)
wget -O /tmp/golang.tar.gz $go_url_arm64
;;
armv6)
wget -O /tmp/golang.tar.gz $go_url_armv6
;;
esac
rm -rvf /usr/local/go/
tar -xvzf /tmp/golang.tar.gz -C /usr/local/
rm /tmp/golang.tar.gz
export GOPATH=/usr/local/go
export GOCACHE=/root/.cache/go-build
echo "Go $go_version installed."
else
echo "Go is up to date (version $go_current_version)."
fi
fi
}
function agent_compile() {
## Compiling and installing tactical agent from github
echo "Agent Compile begin"
wget -O /tmp/rmmagent.zip "https://github.com/amidaware/rmmagent/archive/refs/heads/master.zip"
unzip /tmp/rmmagent -d /tmp/
rm /tmp/rmmagent.zip
cd /tmp/rmmagent-master
case $system in
amd64)
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o /tmp/temp_rmmagent
;;
x86)
env CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags "-s -w" -o /tmp/temp_rmmagent
;;
arm64)
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-s -w" -o /tmp/temp_rmmagent
;;
armv6)
env CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -ldflags "-s -w" -o /tmp/temp_rmmagent
;;
esac
cd /tmp
rm -R /tmp/rmmagent-master
}
function update_agent() {
systemctl stop tacticalagent
cp /tmp/temp_rmmagent /usr/local/bin/rmmagent
rm /tmp/temp_rmmagent
systemctl start tacticalagent
}
case $1 in
update)
check_profile
go_install
agent_compile
update_agent
echo "Tactical Agent Update is done"
exit 0;;
esac