How to Run Local Scripts or commands on Remote Servers Using SSH: 

A Guide for System Administrators:

Introduction

Hello, friends! Today, I am very excited to share a wonderful tool I have developed. It's a bash shell script that uses SSH to run local scripts or commands on remote servers. This tool is a game-changer for all system administrators. It's designed to make your work easier, faster, and more efficient. 

Introducing myssh: Simplified SSH Script

I've created a special tool called "myssh". It's a bash script that simplifies SSH (Secure Shell) usage, making it much easier and more efficient.

Key Features of myssh:

Setting Up myssh for Passwordless SSH Access

Step 1: User Configuration:

# Default SSH user

DEFAULT_USER="root"

(to)


# Default SSH user

DEFAULT_USER="user_name"

Step 2: Script Installation:

Step 3: Setting Permissions:

These steps will establish myssh on your system, enabling efficient, passwordless SSH access to your remote hosts. 

In my case, this involves setting up passwordless authentication for the root user.

Source code for the myssh tool. click hear to Download the script 

#!/bin/bash

# Author: virtualnetworkingconcept.com

# ANSI codes for text colors

RED="\033[31m"

GREEN="\033[32m"

YELLOW="\033[33m"

RESET="\033[0m"


# Default SSH user

DEFAULT_USER="root"


# Function to display help/usage details

display_help() {

    echo "Usage: myssh [SSH_OPTIONS] [USER@]HOST [COMMAND]"

    echo

    echo "Remote SSH with simplified and streamlined options."

    echo

    echo "EXAMPLES:"

    echo -e "# myssh user@host 'uptime'"

    echo -e "# myssh host 'uptime'"

    echo -e "# myssh user@host 'tail -f /var/log/messages'"

    echo

    echo "# Report bugs to virtualnetworkingconcept.com"

    echo

    exit 0

}

>/tmp/myssh_out.log

# Check if the first argument is --help or -h

if [[ "$1" == "--help" || "$1" == "-h" || -z "$1" ]]; then

    display_help

fi

# Construct the SSH target (user@host)

SSH_TARGET="$1"

if [[ "$SSH_TARGET" != *@* ]]; then

    SSH_TARGET="${DEFAULT_USER}@${SSH_TARGET}"

fi

# Shift off the first argument (user@host)

shift

# Continue with the SSH command

ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o ConnectTimeout=2 -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o TCPKeepAlive=yes -o LogLevel=ERROR "${SSH_TARGET}" "$@"

# Check the exit status of the ssh command

if [ $? -eq 255 ]; then

   host_only="${SSH_TARGET#*@}"

   echo -e "$host_only ${YELLOW}Unreachable${RESET}" | tee /tmp/unreachable_hosts.log

fi


Setting Up RunOnClient for Local scripts or commands execution on remote hosts using myssh.

Step 1: User Configuration

Step 2: Script Installation

Step 3: Setting Permissions

Source code for the RunOnClient tool. click hear to Download the script 

#!/bin/bash

#Author:www.virtualnetworkingconcept.com


# ANSI escape codes for text colors

BLACK="\033[30m"

RED="\033[31m"

GREEN="\033[32m"

YELLOW="\033[33m"

BLUE="\033[34m"

MAGENTA="\033[35m"

CYAN="\033[36m"

WHITE="\033[37m"

NC="\033[0m"


# Default SSH user

DEFAULT_USER="root"


# Function to display help/usage details

display_help() {

    echo "Usage: $0"

    echo

    echo "Remote SSH with simplified and streamlined options."

    echo

    echo "Local script execution on a remote host:"

    echo -e "${YELLOW}# RunOnClient local_script.sh <remote_hostname>${NC}"

    echo

    echo -e "Local script parallel execution on a multiple remote hosts: ${MAGENTA}>>>> High Speed >>>>${NC}"

    echo -e "${GREEN}# RunOnClient local_script.sh <hosts_inventory.txt>${NC}"

    echo

    echo "Local script serial execution on a multiple remote hosts:"

    echo -e "${CYAN}# RunOnClient local_script.sh <hosts_inventory.txt> -wait${NC}"

    echo

    echo -e "Execute commands parallel on multiple remote hosts: ${MAGENTA}>>>> High Speed >>>>${NC}"

    echo -e "${GREEN}# RunOnClient <hosts_inventory.txt> <command>${NC}"

    echo -e "${GREEN}# RunOnClient <hosts_inventory.txt> \"<commands>\"${NC}"

    echo

    echo "Execute commands serially on multiple remote hosts:"

    echo -e "${CYAN}# RunOnClient <hosts_inventory.txt> -wait <command>${NC}"

    echo -e "${CYAN}# RunOnClient <hosts_inventory.txt> -wait \"<commands>\"${NC}"

    echo

    echo "Execute command on a remote hosts:"

    echo -e "${YELLOW}# myssh <remote_hostname> <command>${NC}"

    echo

    echo "Report bugs to www.virtualnetworkingconcept.com"

    exit 0

}

# Check if the first argument is --help or -h

if [[ "$1" == "--help" || "$1" == "-h" || -z "$1" ]] && [[ ! "$2" =~ \.(sh|ksh|bash|csh|tcsh|zsh)$ ]]; then

    display_help

fi

#-----------------------------------------------------------------------------#

# Check if the script is called with exactly one argument inventory file

if [ -n "$1" ] && [ -n "$2" ] && [ -f "$2" ] && [ "$3" != "-wait" ] && [[ "$1" =~ \.(sh|ksh|bash|csh|tcsh|zsh)$ ]] && [[ ! "$2" =~ \.(sh|ksh|bash|csh|tcsh|zsh)$ ]]; then

script_execution="$1"

remote_hostname="$2"

# Maximum number of parallel connections

MAX_PARALLEL=100

count=0

# Read hosts into an array

mapfile -t hosts <<< "$(sed '/^$/d;s/ //g' "$remote_hostname" | grep '.'| sort -u)"

# Start SSH connections

for host in "${hosts[@]}"; do

  cat "$script_execution" | myssh "${DEFAULT_USER}"@$host 'bash -s' &

# Wait for current batch to finish before starting new connections

if [[ $count -ge $MAX_PARALLEL ]]; then

    wait

    count=0

fi

done

#-----------------------------------------------------------------------------#

# Check if the script is called for single hostname

elif [ -n "$1" ]  && [[ "$1" =~ \.(sh|ksh|bash|csh|tcsh|zsh)$ ]] && [ -n "$2" ] && [ ! -f "$2" ] && [ "$3" != "-wait" ]; then

script_execution="$1"

remote_hostname="$2"

#Start SSH connections

cat "$script_execution" | myssh "${DEFAULT_USER}"@"$remote_hostname" 'bash -s'

#-----------------------------------------------------------------------------#

# Check if the script is called with an inventory file and a command

elif [[ -n "$1" ]] && [[ -n "$2" ]] && [[ -f "$1" ]] && [[ ! "$1" =~ \.(sh|ksh|bash|csh|tcsh|zsh)$ ]] && [[ ! -f "$2" ]] && [[ "$2" != "-wait" ]] && [[ "$3" != "-wait" ]]; then

    inventory_file="$1"

    command_to_execute="$2"

# Maximum number of parallel connections

MAX_PARALLEL=100

count=0

    # Read hosts into an array

    mapfile -t hosts <<< "$(sed '/^$/d;s/ //g' "$inventory_file" | grep '.'| sort -u)"

    # Start SSH connections to execute the command

    for host in "${hosts[@]}"; do

        myssh "${DEFAULT_USER}"@"$host" "$command_to_execute" 2>/dev/null | sed "s|^|$host: |" &

# Wait for current batch to finish before starting new connections

if [[ $count -ge $MAX_PARALLEL ]]; then

    wait

    count=0

fi

done

#-----------------------------------------------------------------------------#

# Check if the script is called with an inventory file and a command for single hostname from inventory file

elif [[ -n "$1" ]] && [[ -n "$2" ]] && [[ -n "$3" ]] && [[ -f "$1" ]] && [[ ! "$1" =~ \.(sh|ksh|bash|csh|tcsh|zsh)$ ]] && [[ ! -f "$2" ]] && [[ "$3" != "-wait" ]] && [[ "$2" == "-wait" ]]; then

    inventory_file="$1"

    command_to_execute="$3"

    # Read hosts into an array

        mapfile -t hosts <<< "$(sed '/^$/d;s/ //g' "$inventory_file" | grep '.'| sort -u)"

    # Start SSH connections to execute the command

    for host in "${hosts[@]}"; do

    myssh "${DEFAULT_USER}"@"$host" "$command_to_execute" 2>/dev/null | sed "s|^|$host: |" &

    echo "-"

wait

done

#-----------------------------------------------------------------------------#

# Check if the script is called for single hostname from inventory file.

elif [ -n "$1" ] && [ -n "$2" ] && [ -f "$2" ] && [ "$3" == "-wait" ] && [[ "$1" =~ \.(sh|ksh|bash|csh|tcsh|zsh)$ ]]; then

script_execution="$1"

remote_hostname="$2"

# Read hosts into an array

mapfile -t hosts <<< "$(sed '/^$/d;s/ //g' "$remote_hostname" | grep '.'| sort -u)"

# Start SSH connections

for host in "${hosts[@]}"; do

  cat "$script_execution" | myssh "${DEFAULT_USER}"@$host 'bash -s' &

wait

done

#-----------------------------------------------------------------------------#

else

    echo "Usage: $0"

    echo

    echo "Remote SSH with simplified and streamlined options."

    echo

    echo "Local script execution on a remote host:"

    echo -e "${YELLOW}# RunOnClient local_script.sh <remote_hostname>${NC}"

    echo

    echo -e "Local script parallel execution on a multiple remote hosts: ${MAGENTA}>>>> High Speed >>>>${NC}"

    echo -e "${GREEN}# RunOnClient local_script.sh <hosts_inventory.txt>${NC}"

    echo

    echo "Local script serial execution on a multiple remote hosts:"

    echo -e "${CYAN}# RunOnClient local_script.sh <hosts_inventory.txt> -wait${NC}"

    echo

    echo -e "Execute commands parallel on multiple remote hosts: ${MAGENTA}>>>> High Speed >>>>${NC}"

    echo -e "${GREEN}# RunOnClient <hosts_inventory.txt> <command>${NC}"

    echo -e "${GREEN}# RunOnClient <hosts_inventory.txt> \"<commands>\"${NC}"

    echo

    echo "Execute commands serially on multiple remote hosts:"

    echo -e "${CYAN}# RunOnClient <hosts_inventory.txt> -wait <command>${NC}"

    echo -e "${CYAN}# RunOnClient <hosts_inventory.txt> -wait \"<commands>\"${NC}"

    echo

    echo "Execute command on a remote hosts:"

    echo -e "${YELLOW}# myssh <remote_hostname> <command>${NC}"

    echo

    echo "Report bugs to www.virtualnetworkingconcept.com"

    exit 0

fi

#-----------------------------------------------------------------------------#

# Optionally, wait for all connections to complete

wait

Script Features and Operations

The RunOnClient script supports various operations for efficient remote management:

# RunOnClient -h

# RunOnClient --help

# RunOnClient


# myssh -h

# myssh --help

# myssh 

# RunOnClient local_script.sh <remote_hostname>

# RunOnClient local_script.sh <hosts_inventory.txt>

# RunOnClient <hosts_inventory.txt> "<command>"

# RunOnClient local_script.sh <hosts_inventory.txt> -wait

# RunOnClient <hosts_inventory.txt> -wait "<command>"

# myssh <remote_hostname> <command>

Conclusion

As system administrators, our job is to make things run smoothly and efficiently. With this tool, you'll be able to handle remote tasks with ease, giving you more time to focus on other important aspects of your work.