Remote Shell Commands Executor

The RemoteShellCommandsExecutor class is used to execute the bash commands on a remote host using the SSH as way to communicate

Author

N7ghtm4r3 - Tecknobit

See also

Constructors

Link copied to clipboard
constructor(host: BrownieHost)
Constructor to instantiate the object
constructor(sshUser: String, hostAddress: String, sshPassword: String)
Constructor to instantiate the object

Properties

Link copied to clipboard
protected val BASH_SCRIPT_OPTION: String = "-s"
BASH_SCRIPT_OPTION the bash option used to launch a script
Link copied to clipboard
private val EXEC_CHANNEL_TYPE: String = "exec"
EXEC_CHANNEL_TYPE exec type of the channel used to execute the commands
Link copied to clipboard
private val EXECUTE_BASH_SCRIPT: String = "bash -s"
EXECUTE_BASH_SCRIPT bash command to execute a script
Link copied to clipboard
protected val FIND_SERVICE_PATH: String = "find / -type f -name "%s" 2>/dev/null "
FIND_SERVICE_PATH the bash command used to find the path of a service inside the filesystem of the host
Link copied to clipboard
private val GET_BROADCAST_IP_ADDRESS_COMMAND: String = "ip -4 addr show | grep -E 'inet .*brd' | awk '{print $4}'"
GET_BROADCAST_IP_ADDRESS_COMMAND bash command used to retrieve the broadcast ip address of the network interface used to execute the Wake-on-Lan start
Link copied to clipboard
protected val GET_CURRENT_HOST_STATS: String = "echo -e "$(top -bn1 | grep 'Cpu(s)' | awk '{print 100 - $8}'), $(if ls /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq &>/dev/null; then freq=$(awk '{s+=$1} END {print s/NR/1000000}' /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq); printf "%.2f" $freq; else freq=$(lscpu | grep 'CPU MHz' | awk '{print $3/1000}'); printf "%.2f" $freq; fi), $(free -b | awk '/Mem:/ {printf "%.2f/%.2f\n", $3/1073741824, $2/1073741824}'), $(df --block-size=1G --total | awk '/total/ {printf "%d/%d\n", $3, $2}'), $(if lsblk -d -o NAME | grep -q mmcblk; then echo "SD_CARD"; elif lsblk -d -o NAME | grep -q nvme; then echo "SSD_NVMe"; elif lsblk -d -o NAME | grep -q vda; then echo "VIRTUAL_DISK"; elif lsblk -d -o ROTA | awk 'NR>1' | grep -q 0; then echo "SSD"; else echo "HARD_DISK"; fi)" "
GET_CURRENT_HOST_STATS the bash command used to retrieve the current stats of the physical machine of the host
Link copied to clipboard
private val GET_MAC_ADDRESS_COMMAND: String = "ip link show | awk -F': ' '/^[0-9]+: e/{print $2}' | head -n 1 | xargs -I {} ip link show {} | awk '/ether/ {print $2}'"
GET_MAC_ADDRESS_COMMAND bash command used to retrieve the mac address of the network interface used to execute the Wake-on-Lan start
Link copied to clipboard
protected val GREP_RUNNING_SERVICES_COMMAND: String = " ps -ef | grep -E %s | grep -v grep | awk '{print $2}' | paste -sd ',' "
GREP_RUNNING_SERVICES_COMMAND the bash command used to retrieve all the running services on a host and format their pids as list comma separated
Link copied to clipboard
protected val KILL_SERVICE: String = "kill %s"
KILL_SERVICE the bash command used to kill a service currently running on the host
Link copied to clipboard
protected val NOHUP_OUT_FILE: String = "nohup.out"
NOHUP_OUT_FILE the pathname of the generated nohup.
Link copied to clipboard
protected val PIPE_CHARACTER: String = "|"
PIPE_CHARACTER constant value for the pipe character
Link copied to clipboard
protected val REMOVE_FILE_COMMAND: String = "rm %s"
REMOVE_FILE_COMMAND the bash command used to remove a file from the filesystem of the host
Link copied to clipboard
protected val REMOVE_NOHUP_OUT_FILE_COMMAND: String = "rm -f %s"
REMOVE_NOHUP_OUT_FILE_COMMAND the bash command used to remove the NOHUP_OUT_FILE from the host
Link copied to clipboard
private val SERVICE_STARTER_SCRIPT: String = "service-starter.sh"
SERVICE_STARTER_SCRIPT the pathname of the script used to start the services
Link copied to clipboard
private val session: Session
session current SSH session
Link copied to clipboard
private val STRICT_HOST_KEY_CHECKING_OPTION: String = "StrictHostKeyChecking"
STRICT_HOST_KEY_CHECKING_OPTION strict host key checking option
Link copied to clipboard
protected val SUDO_REBOOT: String = "sudo reboot"
SUDO_REBOOT the bash command used to reboot the physical machine of the host
Link copied to clipboard
protected val SUDO_SHUTDOWN_NOW: String = "sudo shutdown now"
SUDO_SHUTDOWN_NOW the bash command used to shut down the physical machine of the host

Functions

Link copied to clipboard
protected open fun appendExitStatus(errorMessage: String, exitStatus: Int): String
Method used to append the exit status of the command to the error result of the same command
Link copied to clipboard
Method used to detect the stopped services
Link copied to clipboard
protected open fun execBashCommand(command: String, onCommandExecuted: ShellCommandsExecutor.OnCommandExecuted, closeSession: Boolean): String
protected open fun execBashCommand(command: String): String
protected open fun execBashCommand(command: String, closeSession: Boolean): String
protected open fun execBashCommand(command: String, onCommandExecuted: ShellCommandsExecutor.OnCommandExecuted): String
Method used to execute a bash command
Link copied to clipboard
protected open fun execServiceStarter(service: BrownieHostService, out: OutputStream)
Method used to execute the SERVICE_STARTER_SCRIPT script
Link copied to clipboard
protected open fun extractServicePid(commandResult: String): Long
Method used to extract from the shell result the pid of the started process
Link copied to clipboard
open fun findServicePath(name: String): String
Method to find the path of the service using the FIND_SERVICE_PATH command
Link copied to clipboard
Method used to get the current stats of the host with the GET_CURRENT_HOST_STATS command
Link copied to clipboard
Method used to obtain a specific instance to correctly execute bash commands on hosts shells
Link copied to clipboard
Method to retrieve the network interface details useful to execute the Wake-on-Lan
Link copied to clipboard
protected open fun purgeNohupOutIfRequired(service: BrownieHostService)
Method used to purge the NOHUP_OUT_FILE when required
Link copied to clipboard
open fun rebootHost(service: HostsService, host: BrownieHost)
Method used to reboot the host using the SUDO_REBOOT command
Link copied to clipboard
Method used to reboot a service
Link copied to clipboard
open fun removeService(serviceName: String, closeSession: Boolean): String
Method to simply remove a service from a host
Link copied to clipboard
open fun startService(service: BrownieHostService): Long
Method used to start a service
Link copied to clipboard
open fun stopHost(service: HostsService, host: BrownieHost)
Method used to stop the host using the SUDO_SHUTDOWN_NOW command
Link copied to clipboard
open fun stopService(service: BrownieHostService, closeSession: Boolean): String
Method used to stop a service