From a7cf48a48cf2be7ab0fe937d62f27b506e5f2240 Mon Sep 17 00:00:00 2001 From: DHANUSH RAJA <155062318+DHANUSHRAJA22@users.noreply.github.com> Date: Wed, 27 Aug 2025 03:50:12 +0530 Subject: [PATCH] Add Day 7 remote_execute.sh for remote command execution challenge.Add Day 7 remote_execute.sh for remote command execution challenge.Create remote_execute.sh --- Challenges/Day_7/remote_execute.sh | 163 +++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 Challenges/Day_7/remote_execute.sh diff --git a/Challenges/Day_7/remote_execute.sh b/Challenges/Day_7/remote_execute.sh new file mode 100644 index 0000000..1cf4a4c --- /dev/null +++ b/Challenges/Day_7/remote_execute.sh @@ -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 +# +# 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 " + 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