Skip to content

Commit

Permalink
checks for sshpass only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri Escalianti committed Aug 31, 2017
1 parent 97477a2 commit 62b5e9a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2.1:

- now checks for sshpass only when '-n' option is not set
- improved error messages
- added default Bash flags in README
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ $ runoverssh [OPTIONS] USERNAME COMMAND HOSTS...
#### Default behavior:

* Ask each `username@host` password at the start
* SSH connections use the flags `-o ConnectTimeout=5 -o StrictHostKeyChecking=no`
* Print all SSH output in the screen
* SSH flags `-o ConnectTimeout=5 -o StrictHostKeyChecking=no`
* Bash flags `-l`
* Prints all SSH output in the screen

#### Options:
```
Expand Down
50 changes: 27 additions & 23 deletions runoverssh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
script_name=`basename "$0"`
script_alias=`basename -s .sh "$0"`

# Check dependencies
command -v "ssh" >/dev/null 2>&1 || { echo >&2 "Error! You need to install 'ssh' to run this. Exiting."; exit 1; }
command -v "sshpass" >/dev/null 2>&1 || { echo >&2 "Error! You need to install 'sshpass' to run this. Exiting."; exit 1; }

# Print help
function print_help {
echo "Usage: ${script_name} [OPTIONS] USERNAME COMMAND HOSTS..."
Expand Down Expand Up @@ -60,7 +56,7 @@ function print_help {

# Standard parameters
username=""
command=""
remote_command=""
hosts=()

# Optional parameters
Expand All @@ -70,7 +66,7 @@ log_enabled="" # -l | --log
log_file="${script_alias}.log" # --logfile
global_pw="" # -g, --globalpw
passwords=()
no_pw=""
no_pw="" # -n, --nopw
ssh_flags="-o ConnectTimeout=5 -o StrictHostKeyChecking=no" # --sshflags
bash_flags="-l" # --bashflags
quiet_enabled="" # -q | --quiet
Expand Down Expand Up @@ -135,7 +131,7 @@ for parameter do
;;
*)
if [[ "${parameter}" == "--"* ]] || [[ "${parameter}" == "-"* ]]; then
echo "Error! Invalid option '${parameter}'. Exiting."
echo "Error: invalid option '${parameter}'. Exiting."
exit 1
fi

Expand All @@ -144,8 +140,8 @@ for parameter do
if [ -z "${username}" ]; then
username=${parameter}
else
if [ -z "${read_commands_from_file}" ] && [ -z "${command}" ]; then
command="${parameter}"
if [ -z "${read_commands_from_file}" ] && [ -z "${remote_command}" ]; then
remote_command="${parameter}"
else
if [ -z "${read_hosts_from_file}" ]; then
hosts+=("${parameter}")
Expand All @@ -161,35 +157,35 @@ done
# Check parameter and argument consistency

if [ -n "${waiting_option}" ]; then
echo "Error! Missing value for '${waiting_option}' parameter. Exiting."
echo "Error: missing value for '${waiting_option}' parameter. Exiting."
exit 1
fi

if [ -z "${username}" ]; then
echo "Error! Please specify the username to be used in SSH. Exiting."
echo "Error: please specify the username to be used in SSH. Exiting."
exit 1
fi

if [ -z "${read_commands_from_file}" ] && [ -z "${command}" ] ; then
echo "Error! Please specify the command or script file ('-s') to be executed over SSH. Exiting."
if [ -z "${read_commands_from_file}" ] && [ -z "${remote_command}" ] ; then
echo "Error: please specify the command or script file ('-s') to be executed over SSH. Exiting."
exit 1
fi
if [ -n "${read_commands_from_file}" ] && [ -n "${command}" ] ; then
echo "Error! Parameter conflict: do not put a command as argument when also specifying a script as parameter. Exiting."
if [ -n "${read_commands_from_file}" ] && [ -n "${remote_command}" ] ; then
echo "Error: parameter conflict: do not put a command as argument when also specifying a script as parameter. 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."
echo "Error: please specify at least one target host or use the '--hostsfile' option. Exiting."
exit 1
fi
if [ -n "${read_hosts_from_file}" ] && [ ${#hosts[@]} -gt 0 ]; then
echo "Error! Parameter conflict: do not list hosts as arguments when also specifying a hostsfile as parameter. Exiting."
echo "Error: parameter conflict: do not list hosts as arguments when also specifying a hostsfile as parameter. Exiting."
exit 1
fi

if [ -n "${global_pw}" ] && [ -n "${no_pw}" ]; then
echo "Error! Parameter conflict: do not set the 'no password' option when also specifying a global password. Exiting.";
echo "Error: parameter conflict: do not set the 'no password' option when also specifying a global password. Exiting.";
exit 1
fi

Expand All @@ -206,7 +202,7 @@ fi

# Check log file
if [ -d "${log_file}" ]; then
echo "Error! Log file '${log_file}' is a directory. Exiting."
echo "Error: log file '${log_file}' is a directory. Exiting."
exit 1
fi

Expand All @@ -220,6 +216,14 @@ if [ -n "${read_hosts_from_file}" ]; then
done<"${read_hosts_from_file}"
fi

# Check dependencies
command -v "ssh" >/dev/null 2>&1 || { echo >&2 "Error: program 'ssh' not found. Exiting."; exit 1; }

# Check for sshpass if the option '-n' is not set
if [ -z "${no_pw}" ]; then
command -v "sshpass" >/dev/null 2>&1 || { echo >&2 "Error: program 'sshpass' not found (use the '-n' option to use 'ssh' instead). Exiting."; exit 1; }
fi


# Get the SSH passwords (unless 'no password' is set)

Expand Down Expand Up @@ -271,9 +275,9 @@ if [ -n "${no_pw}" ]; then
fi
else
if [ -n "${quiet_enabled}" ]; then
ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${command}\"" >> "${log_file}"
ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${remote_command}\"" >> "${log_file}"
else
ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${command}\"" | tee -a "${log_file}"
ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${remote_command}\"" | tee -a "${log_file}"
fi
fi
done
Expand All @@ -290,9 +294,9 @@ else
fi
else
if [ -n "${quiet_enabled}" ]; then
sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${command}\"" >> "${log_file}"
sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${remote_command}\"" >> "${log_file}"
else
sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${command}\"" | tee -a "${log_file}"
sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${remote_command}\"" | tee -a "${log_file}"
fi
fi

Expand Down

0 comments on commit 62b5e9a

Please sign in to comment.