Remote Linux Server Management with Bash Scripts

How to Remote Linux Server Management with Bash Scripts

System administrators use Bash scripts and SSH to check memory and disk usage on remote servers. Linux Server Management This helps them quickly gather performance data from multiple servers in one place, saving time and ensuring everything runs smoothly.

Remote Linux Server Management with Bash Scripts Objective :

  • Runs a local script:  monitor memory usage in the local server 
  • Connects to a remote server via SSH to:
    • Execute a script to monitor memory usage.
    • Capture memory and disk usage information.
    • Retrieve disk information.

Remote Linux Server Management with Bash Scripts Prerequisites :

  • Access to a Linux-based server with SSH enabled.
  • Basic knowledge of Bash scripting.

Remote Linux Server Management with Bash Scripts Note :

  • Navigate to the directory where the script is located.
  • you can change the user name to your requirement 
  • Customization: Modify the username (oracle) in the SSH command (ssh oracle@192.168.220.179) to match your environment.
  • Understand each command before executing the script to ensure it fits your environment and requirements.

ALSO READ: How to Set Up SSH Passwordless Connection

Example output :

Access to a Linux-based server with SSH enabled.

Execute a script to monitor memory usage.

Script :

#!/bin/bash

# Navigate to the script directory
echo "Navigating to the script directory: /home/oracle/krishna/memory_usage_alert"
cd /home/oracle/krishna/memory_usage_alert

# Run your local script (memory_usage.sh)
echo "Running local script: memory_usage.sh"
./memory_usage.sh

# Connect to the remote server
echo "Connecting to the remote server: oracle@192.168.220.179"
ssh oracle@192.168.220.179 << EOF
echo "Connected to the remote server"

# Navigate to the remote directory
echo "Navigating to the remote directory: /home/oracle/krishna"
cd /home/oracle/krishna

# Execute remote script
echo "Executing remote script: memory_usage_alert.sh"
./memory_usage_alert.sh

# Capture memory usage
echo "Capturing memory usage in memory.txt"
free -h > memory.txt

# Capture disk usage
echo "Capturing disk usage in disk.txt"
df -h > disk.txt

# Capture disk information
echo "Capturing disk information in disk_count.txt"
lsblk > disk_count.txt

# Exit remote session
echo "Exiting remote session"
exit
EOF

echo "Script execution completed"

Now that we have successfully run the remote and local server scripts to complete the task, please check the script above.

Thank you.

Leave a Comment