2121script_name=` basename " $0 " `
2222script_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
2925function print_help {
3026 echo " Usage: ${script_name} [OPTIONS] USERNAME COMMAND HOSTS..."
@@ -60,7 +56,7 @@ function print_help {
6056
6157# Standard parameters
6258username=" "
63- command =" "
59+ remote_command =" "
6460hosts=()
6561
6662# Optional parameters
@@ -70,7 +66,7 @@ log_enabled="" # -l | --log
7066log_file=" ${script_alias} .log" # --logfile
7167global_pw=" " # -g, --globalpw
7268passwords=()
73- no_pw=" "
69+ no_pw=" " # -n, --nopw
7470ssh_flags=" -o ConnectTimeout=5 -o StrictHostKeyChecking=no" # --sshflags
7571bash_flags=" -l" # --bashflags
7672quiet_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
163159if [ -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
166162fi
167163
168164if [ -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
171167fi
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
176172fi
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
180176fi
181177
182178if [ -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
185181fi
186182if [ -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
189185fi
190186
191187if [ -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
194190fi
195191
206202
207203# Check log file
208204if [ -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
211207fi
212208
@@ -220,6 +216,14 @@ if [ -n "${read_hosts_from_file}" ]; then
220216 done< " ${read_hosts_from_file} "
221217fi
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
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