Skip to content

Commit 78215f6

Browse files
committed
Improved the code style.
* Rewritten more of ssh calls into functions. * Improved the option check message.
1 parent be470f5 commit 78215f6

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

utils/oscap-ssh

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,14 @@ OSCAP_SUDO=""
9292
# SSH_ADDITIONAL_OPTIONS may be defined in the calling shell
9393
SSH_TTY_ALLOCATION_OPTION=""
9494

95-
# $1: The SSH command.
96-
# $2: More of additional options (optional, space-separated string)
95+
# $1, $2, ... SSH options (pass them as separate arguments)
9796
function ssh_execute_with_options {
97+
ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS "$@" -p "$SSH_PORT" "$SSH_HOST"
98+
}
99+
100+
# $1: The SSH command.
101+
# $2: More of additional options (optional, pass one space-separated string)
102+
function ssh_execute_with_command_and_options {
98103
ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS $2 -p "$SSH_PORT" "$SSH_HOST" "$1"
99104
}
100105

@@ -145,7 +150,7 @@ function check_oscap_arguments {
145150
elif [ "$1 $2" == "oval collect" ]; then
146151
true
147152
else
148-
die "This script only supports '-h', '--help', '--v', '--version', 'info', 'xccdf eval', 'oval eval' and 'oval collect'."
153+
die "This script only supports 'sudo' as first argument, '-h', '--help', '--v', '--version', 'info', 'xccdf eval', 'oval eval' and 'oval collect'."
149154
fi
150155
}
151156

@@ -162,10 +167,10 @@ MASTER_SOCKET_DIR=$(mktemp -d)
162167
MASTER_SOCKET="$MASTER_SOCKET_DIR/ssh_socket"
163168

164169
echo "Connecting to '$SSH_HOST' on port '$SSH_PORT'..."
165-
ssh -M -f -N -o ServerAliveInterval=60 -o ControlPath="$MASTER_SOCKET" -p "$SSH_PORT" $SSH_ADDITIONAL_OPTIONS "$SSH_HOST" || die "Failed to connect!"
170+
ssh_execute_with_options -M -f -N -o ServerAliveInterval=60 || die "Failed to connect!"
166171
echo "Connected!"
167172

168-
REMOTE_TEMP_DIR=$(ssh_execute_with_options "mktemp -d") || die "Failed to create remote temporary directory!"
173+
REMOTE_TEMP_DIR=$(ssh_execute_with_command_and_options "mktemp -d") || die "Failed to create remote temporary directory!"
169174

170175
oscap_args=("$@")
171176

@@ -262,7 +267,7 @@ echo "Starting the evaluation..."
262267
# changing directory because of --oval-results support. oval results files are
263268
# dumped into PWD, and we can't be sure by the file names - we need controlled
264269
# environment
265-
ssh_execute_with_options "cd $REMOTE_TEMP_DIR; $OSCAP_SUDO oscap ${oscap_args[*]}" "$SSH_TTY_ALLOCATION_OPTION"
270+
ssh_execute_with_command_and_options "cd $REMOTE_TEMP_DIR; $OSCAP_SUDO oscap ${oscap_args[*]}" "$SSH_TTY_ALLOCATION_OPTION"
266271
OSCAP_EXIT_CODE=$?
267272
echo "oscap exit code: $OSCAP_EXIT_CODE"
268273

@@ -284,9 +289,9 @@ if [ "$OVAL_RESULTS" == "yes" ]; then
284289
fi
285290

286291
echo "Removing remote temporary directory..."
287-
ssh_execute_with_options "rm -r $REMOTE_TEMP_DIR" || die "Failed to remove remote temporary directory!"
292+
ssh_execute_with_command_and_options "rm -r $REMOTE_TEMP_DIR" || die "Failed to remove remote temporary directory!"
288293
echo "Disconnecting ssh and removing master ssh socket directory..."
289-
ssh -o ControlPath="$MASTER_SOCKET" $SSH_ADDITIONAL_OPTIONS -p "$SSH_PORT" "$SSH_HOST" -O exit || die "Failed to disconnect!"
294+
ssh_execute_with_options -O exit || die "Failed to disconnect!"
290295
rm -r "$MASTER_SOCKET_DIR" || die "Failed to remove local master SSH socket directory!"
291296

292297
exit $OSCAP_EXIT_CODE

0 commit comments

Comments
 (0)