BASH
Running Commands on Remote Server via SSH
Execute commands on a remote server securely using SSH in Bash scripts, covering passwordless authentication, command execution, and file transfer (scp), crucial for server management.
#!/bin/bash
# --- Configuration ---
REMOTE_USER="webadmin"
REMOTE_HOST="your_remote_server.com" # Replace with your server IP or hostname
SSH_PORT=22 # Default SSH port
# Ensure passwordless SSH is set up (e.g., ssh-copy-id)
# ssh-keygen -t rsa -b 4096
# ssh-copy-id -p $SSH_PORT $REMOTE_USER@$REMOTE_HOST
# --- Functions ---
log_message() {
local type=$1
local message=$2
echo "[$(date +'%Y-%m-%d %H:%M:%S')] [$type] $message"
}
# Function to execute a command remotely
execute_remote_command() {
local command="$1"
log_message INFO "Executing remote command on $REMOTE_HOST: '$command'"
ssh -p "$SSH_PORT" "$REMOTE_USER@$REMOTE_HOST" "$command"
local exit_code=$?
if [ $exit_code -ne 0 ]; then
log_message ERROR "Remote command failed with exit code $exit_code."
return 1
fi
return 0
}
# Function to upload a file to the remote server
upload_file() {
local local_path="$1"
local remote_path="$2"
log_message INFO "Uploading $local_path to $REMOTE_HOST:$remote_path"
scp -P "$SSH_PORT" "$local_path" "$REMOTE_USER@$REMOTE_HOST:$remote_path"
local exit_code=$?
if [ $exit_code -ne 0 ]; then
log_message ERROR "File upload failed with exit code $exit_code."
return 1
fi
return 0
}
# Function to download a file from the remote server
download_file() {
local remote_path="$1"
local local_path="$2"
log_message INFO "Downloading $REMOTE_HOST:$remote_path to $local_path"
scp -P "$SSH_PORT" "$REMOTE_USER@$REMOTE_HOST:$remote_path" "$local_path"
local exit_code=$?
if [ $exit_code -ne 0 ]; then
log_message ERROR "File download failed with exit code $exit_code."
return 1
fi
return 0
}
# --- Main Script ---
log_message INFO "Script started."
# Example 1: Check remote disk usage
if ! execute_remote_command "df -h"; then
log_message ERROR "Failed to get remote disk usage."
exit 1
fi
# Example 2: Create a dummy file locally and upload it
echo "This is a test file from local." > local_test_file.txt
if upload_file "local_test_file.txt" "/tmp/remote_test_file.txt"; then
log_message INFO "File uploaded successfully. Verifying remote existence..."
execute_remote_command "cat /tmp/remote_test_file.txt"
else
log_message ERROR "Failed to upload file."
rm local_test_file.txt
exit 1
fi
rm local_test_file.txt
# Example 3: Download a remote file (e.g., /etc/hostname)
if download_file "/etc/hostname" "downloaded_hostname.txt"; then
log_message INFO "Remote hostname downloaded successfully. Content:"
cat downloaded_hostname.txt
else
log_message ERROR "Failed to download remote hostname."
rm -f downloaded_hostname.txt
exit 1
fi
rm -f downloaded_hostname.txt
# Example 4: Restart a service (use with caution!)
# if execute_remote_command "sudo systemctl restart nginx"; then
# log_message INFO "Nginx restarted successfully."
# else
# log_message ERROR "Failed to restart Nginx."
# exit 1
# fi
log_message INFO "Script finished."
How it works: This script demonstrates how to interact with a remote server using SSH and SCP. It defines functions for executing commands remotely (`execute_remote_command`), uploading files (`upload_file`), and downloading files (`download_file`). It uses `ssh` for command execution and `scp` for file transfers, highlighting the importance of passwordless SSH authentication (via `ssh-copy-id`). This is invaluable for automating deployment, server maintenance, and configuration tasks on remote machines.