Skip to content

Commit

Permalink
added option to pass arguments to the script file
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri Escalianti committed Sep 12, 2017
1 parent 62b5e9a commit a60c83f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions runoverssh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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"
}

Expand All @@ -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
Expand All @@ -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}"
;;
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a60c83f

Please sign in to comment.