Skip to content

Commit 62b5e9a

Browse files
author
Yuri Escalianti
committed
checks for sshpass only when necessary
1 parent 97477a2 commit 62b5e9a

3 files changed

Lines changed: 35 additions & 25 deletions

File tree

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2.1:
2+
3+
- now checks for sshpass only when '-n' option is not set
4+
- improved error messages
5+
- added default Bash flags in README

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ $ runoverssh [OPTIONS] USERNAME COMMAND HOSTS...
1010
#### Default behavior:
1111

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

1617
#### Options:
1718
```

runoverssh

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
script_name=`basename "$0"`
2222
script_alias=`basename -s .sh "$0"`
2323

24-
# Check dependencies
25-
command -v "ssh" >/dev/null 2>&1 || { echo >&2 "Error! You need to install 'ssh' to run this. Exiting."; exit 1; }
26-
command -v "sshpass" >/dev/null 2>&1 || { echo >&2 "Error! You need to install 'sshpass' to run this. Exiting."; exit 1; }
27-
2824
# Print help
2925
function print_help {
3026
echo "Usage: ${script_name} [OPTIONS] USERNAME COMMAND HOSTS..."
@@ -60,7 +56,7 @@ function print_help {
6056

6157
# Standard parameters
6258
username=""
63-
command=""
59+
remote_command=""
6460
hosts=()
6561

6662
# Optional parameters
@@ -70,7 +66,7 @@ log_enabled="" # -l | --log
7066
log_file="${script_alias}.log" # --logfile
7167
global_pw="" # -g, --globalpw
7268
passwords=()
73-
no_pw=""
69+
no_pw="" # -n, --nopw
7470
ssh_flags="-o ConnectTimeout=5 -o StrictHostKeyChecking=no" # --sshflags
7571
bash_flags="-l" # --bashflags
7672
quiet_enabled="" # -q | --quiet
@@ -135,7 +131,7 @@ for parameter do
135131
;;
136132
*)
137133
if [[ "${parameter}" == "--"* ]] || [[ "${parameter}" == "-"* ]]; then
138-
echo "Error! Invalid option '${parameter}'. Exiting."
134+
echo "Error: invalid option '${parameter}'. Exiting."
139135
exit 1
140136
fi
141137

@@ -144,8 +140,8 @@ for parameter do
144140
if [ -z "${username}" ]; then
145141
username=${parameter}
146142
else
147-
if [ -z "${read_commands_from_file}" ] && [ -z "${command}" ]; then
148-
command="${parameter}"
143+
if [ -z "${read_commands_from_file}" ] && [ -z "${remote_command}" ]; then
144+
remote_command="${parameter}"
149145
else
150146
if [ -z "${read_hosts_from_file}" ]; then
151147
hosts+=("${parameter}")
@@ -161,35 +157,35 @@ done
161157
# Check parameter and argument consistency
162158

163159
if [ -n "${waiting_option}" ]; then
164-
echo "Error! Missing value for '${waiting_option}' parameter. Exiting."
160+
echo "Error: missing value for '${waiting_option}' parameter. Exiting."
165161
exit 1
166162
fi
167163

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

173-
if [ -z "${read_commands_from_file}" ] && [ -z "${command}" ] ; then
174-
echo "Error! Please specify the command or script file ('-s') to be executed over SSH. Exiting."
169+
if [ -z "${read_commands_from_file}" ] && [ -z "${remote_command}" ] ; then
170+
echo "Error: please specify the command or script file ('-s') to be executed over SSH. Exiting."
175171
exit 1
176172
fi
177-
if [ -n "${read_commands_from_file}" ] && [ -n "${command}" ] ; then
178-
echo "Error! Parameter conflict: do not put a command as argument when also specifying a script as parameter. Exiting."
173+
if [ -n "${read_commands_from_file}" ] && [ -n "${remote_command}" ] ; then
174+
echo "Error: parameter conflict: do not put a command as argument when also specifying a script as parameter. Exiting."
179175
exit 1
180176
fi
181177

182178
if [ -z "${read_hosts_from_file}" ] && [ ${#hosts[@]} -eq 0 ]; then
183-
echo "Error! Please specify at least one target host or use the '--hostsfile' option. Exiting."
179+
echo "Error: please specify at least one target host or use the '--hostsfile' option. Exiting."
184180
exit 1
185181
fi
186182
if [ -n "${read_hosts_from_file}" ] && [ ${#hosts[@]} -gt 0 ]; then
187-
echo "Error! Parameter conflict: do not list hosts as arguments when also specifying a hostsfile as parameter. Exiting."
183+
echo "Error: parameter conflict: do not list hosts as arguments when also specifying a hostsfile as parameter. Exiting."
188184
exit 1
189185
fi
190186

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

@@ -206,7 +202,7 @@ fi
206202

207203
# Check log file
208204
if [ -d "${log_file}" ]; then
209-
echo "Error! Log file '${log_file}' is a directory. Exiting."
205+
echo "Error: log file '${log_file}' is a directory. Exiting."
210206
exit 1
211207
fi
212208

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

219+
# Check dependencies
220+
command -v "ssh" >/dev/null 2>&1 || { echo >&2 "Error: program 'ssh' not found. Exiting."; exit 1; }
221+
222+
# Check for sshpass if the option '-n' is not set
223+
if [ -z "${no_pw}" ]; then
224+
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; }
225+
fi
226+
223227

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

@@ -271,9 +275,9 @@ if [ -n "${no_pw}" ]; then
271275
fi
272276
else
273277
if [ -n "${quiet_enabled}" ]; then
274-
ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${command}\"" >> "${log_file}"
278+
ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${remote_command}\"" >> "${log_file}"
275279
else
276-
ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${command}\"" | tee -a "${log_file}"
280+
ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${remote_command}\"" | tee -a "${log_file}"
277281
fi
278282
fi
279283
done
@@ -290,9 +294,9 @@ else
290294
fi
291295
else
292296
if [ -n "${quiet_enabled}" ]; then
293-
sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${command}\"" >> "${log_file}"
297+
sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${remote_command}\"" >> "${log_file}"
294298
else
295-
sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${command}\"" | tee -a "${log_file}"
299+
sshpass -p "${passwords[$i]}" ssh ${ssh_flags} ${username}@${host} "bash ${bash_flags} -c \"${remote_command}\"" | tee -a "${log_file}"
296300
fi
297301
fi
298302

0 commit comments

Comments
 (0)