diff --git a/CHANGELOG b/CHANGELOG index 087f490..611e9a9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +2.2: + +- added option to pass arguments to the script file (ex: --script "test.sh" --args "arg1 arg2") + 2.1: - now checks for sshpass only when '-n' option is not set diff --git a/README.md b/README.md index 4c3c839..e8ddee9 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ $ runoverssh [OPTIONS] USERNAME COMMAND HOSTS... -r, --hostsfile [file] use the list of hosts from a file (one host per line) -n, --nopw no password (use ssh directly instead of sshpass) + -a, --args specify the arguments to be passed to the script file -l, --log save ssh output (default: runoverssh.log) (append) -q, --quiet disable ssh screen output diff --git a/runoverssh b/runoverssh index 41f6d88..678ba86 100755 --- a/runoverssh +++ b/runoverssh @@ -32,6 +32,7 @@ function print_help { echo " -r, --hostsfile [file] use the list of hosts from a file (one host per line)" echo "" echo " -n, --nopw no password (use ssh directly instead of sshpass)" + echo " -a, --args specify the arguments to be passed to the script file" echo " -l, --log save ssh output (default: ${script_alias}.log) (append)" echo " -q, --quiet disable ssh screen output" echo "" @@ -51,6 +52,8 @@ function print_help { echo "" echo " runoverssh --script myscript.sh --hostsfile hostlist remoteuser" echo "" + echo " runoverssh --script myscript.sh --args '-q -s -t' --hostsfile hostlist remoteuser" + echo "" echo "Bugs or Requests: https://github.com/yuriescl/runoverssh/issues" } @@ -60,16 +63,17 @@ remote_command="" hosts=() # Optional parameters -read_commands_from_file="" # -s | --script -read_hosts_from_file="" # -r | --hostsfile -log_enabled="" # -l | --log +read_commands_from_file="" # -s , --script +script_arguments="" # -a , --args +read_hosts_from_file="" # -r , --hostsfile +log_enabled="" # -l , --log log_file="${script_alias}.log" # --logfile global_pw="" # -g, --globalpw passwords=() no_pw="" # -n, --nopw ssh_flags="-o ConnectTimeout=5 -o StrictHostKeyChecking=no" # --sshflags bash_flags="-l" # --bashflags -quiet_enabled="" # -q | --quiet +quiet_enabled="" # -q , --quiet if [[ "$1" == "--help" ]]; then print_help @@ -95,6 +99,9 @@ for parameter do "-s" | "--script") read_commands_from_file="${parameter}" ;; + "-a" | "--args") + script_arguments="${parameter}" + ;; "-r" | "--hostsfile") read_hosts_from_file="${parameter}" ;; @@ -114,7 +121,7 @@ for parameter do else # first or not waiting option is_first="no" case "${parameter}" in - "-s" | "--script" | "-r" | "--hostsfile" | "--logfile" | "--sshflags" | "--bashflags") + "-s" | "--script" | "-a" | "--args" | "-r" | "--hostsfile" | "--logfile" | "--sshflags" | "--bashflags") waiting_option="${parameter}" ;; "-g" | "--globalpw") @@ -175,6 +182,11 @@ if [ -n "${read_commands_from_file}" ] && [ -n "${remote_command}" ] ; then exit 1 fi +if [ -n "${script_arguments}" ] && [ -z "${read_commands_from_file}" ]; then + echo "Error: arguments specified but no script file provided. Exiting." + exit 1 +fi + if [ -z "${read_hosts_from_file}" ] && [ ${#hosts[@]} -eq 0 ]; then echo "Error: please specify at least one target host or use the '--hostsfile' option. Exiting." exit 1 @@ -269,9 +281,9 @@ if [ -n "${no_pw}" ]; then echo "Connecting as ${username}@${host}..." if [ -n "${read_commands_from_file}" ]; then if [ -n "${quiet_enabled}" ]; then - ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -s" < "${read_commands_from_file}" >> "${log_file}" + ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -s" < "${read_commands_from_file}" "${script_arguments}" >> "${log_file}" else - ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -s" < "${read_commands_from_file}" | tee -a "${log_file}" + ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -s" < "${read_commands_from_file}" "${script_arguments}" | tee -a "${log_file}" fi else if [ -n "${quiet_enabled}" ]; then @@ -288,9 +300,9 @@ else echo "Connecting as ${username}@${host}..." if [ -n "${read_commands_from_file}" ]; then if [ -n "${quiet_enabled}" ]; then - sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -s" < "${read_commands_from_file}" >> "${log_file}" + sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -s" < "${read_commands_from_file}" "${script_arguments}" >> "${log_file}" else - sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -s" < "${read_commands_from_file}" | tee -a "${log_file}" + sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -s" < "${read_commands_from_file}" "${script_arguments}" | tee -a "${log_file}" fi else if [ -n "${quiet_enabled}" ]; then