Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions Challenges/Day_7/remote_execute.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/bin/bash

# remote_execute.sh - Remote Command Execution Script
#
# Description:
# This script allows you to execute commands remotely on target hosts using SSH.
# It takes a target host, username, and command as arguments and executes the
# command on the remote host, displaying the output.
#
# Usage:
# ./remote_execute.sh <target_host> <username> <command>
#
# Arguments:
# target_host - The hostname or IP address of the remote host
# username - The username to authenticate with on the remote host
# command - The command to execute on the remote host (use quotes if contains spaces)
#
# Examples:
# ./remote_execute.sh 192.168.1.100 admin "ls -la"
# ./remote_execute.sh server.example.com user "df -h"
# ./remote_execute.sh myserver john "uptime"
#
# Prerequisites:
# - SSH must be installed and configured
# - SSH key-based authentication is recommended for security
# - The target host must be accessible via SSH
#
# Author: BashBlaze Day 7 Challenge
# Date: $(date +"%Y-%m-%d")

# Function to display usage information
show_usage() {
echo "Usage: $0 <target_host> <username> <command>"
echo ""
echo "Arguments:"
echo " target_host - The hostname or IP address of the remote host"
echo " username - The username to authenticate with on the remote host"
echo " command - The command to execute on the remote host"
echo ""
echo "Examples:"
echo " $0 192.168.1.100 admin \"ls -la\""
echo " $0 server.example.com user \"df -h\""
echo " $0 myserver john \"uptime\""
echo ""
exit 1
}

# Function to validate arguments
validate_arguments() {
if [ $# -ne 3 ]; then
echo "Error: Invalid number of arguments."
echo "Expected 3 arguments, got $#."
echo ""
show_usage
fi

local target_host="$1"
local username="$2"
local command="$3"

# Check if target_host is empty
if [ -z "$target_host" ]; then
echo "Error: Target host cannot be empty."
show_usage
fi

# Check if username is empty
if [ -z "$username" ]; then
echo "Error: Username cannot be empty."
show_usage
fi

# Check if command is empty
if [ -z "$command" ]; then
echo "Error: Command cannot be empty."
show_usage
fi

# Basic hostname/IP validation
if ! echo "$target_host" | grep -E '^[a-zA-Z0-9.-]+$' > /dev/null; then
echo "Error: Invalid target host format."
echo "Target host should contain only letters, numbers, dots, and hyphens."
show_usage
fi

# Basic username validation
if ! echo "$username" | grep -E '^[a-zA-Z0-9_-]+$' > /dev/null; then
echo "Error: Invalid username format."
echo "Username should contain only letters, numbers, underscores, and hyphens."
show_usage
fi
}

# Function to execute remote command
execute_remote_command() {
local target_host="$1"
local username="$2"
local command="$3"

echo "Connecting to $username@$target_host..."
echo "Executing command: $command"
echo ""
echo "=== Remote Command Output ==="

# Execute the SSH command and capture the exit status
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no "$username@$target_host" "$command"
local ssh_exit_status=$?

echo ""
echo "=== End of Remote Output ==="

# Check if SSH command was successful
if [ $ssh_exit_status -eq 0 ]; then
echo "Remote command executed successfully."
else
echo "Error: Remote command failed with exit status: $ssh_exit_status"
case $ssh_exit_status in
255)
echo "Possible causes: Connection refused, host unreachable, or authentication failed."
;;
1)
echo "Possible cause: Command not found or command execution failed."
;;
*)
echo "Check the SSH connection and command syntax."
;;
esac
exit $ssh_exit_status
fi
}

# Main script execution
main() {
# Display script header
echo "===== Remote Command Execution Script ====="
echo "BashBlaze Day 7 Challenge"
echo ""

# Validate arguments
validate_arguments "$@"

# Extract arguments
local target_host="$1"
local username="$2"
local command="$3"

# Check if SSH is available
if ! command -v ssh > /dev/null 2>&1; then
echo "Error: SSH command not found. Please install SSH client."
exit 1
fi

# Execute remote command
execute_remote_command "$target_host" "$username" "$command"

echo ""
echo "Script execution completed."
}

# Check if script is being sourced or executed
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi