Skip to content

Commit eacbc2e

Browse files
committed
2.0.2: fix the bug
1 parent c1b9b3f commit eacbc2e

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

ssh-oci-bastion.sh

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ENVIRONMENT
6161
ProxyJump
6262
\`\`\`
6363
64-
v2.0.1 May 2023 Created by Dima Korobskiy
64+
v2.0.2 May 2023 Created by Dima Korobskiy
6565
Credits: George Chacko, Oracle
6666
HEREDOC
6767
exit 1
@@ -71,19 +71,19 @@ HEREDOC
7171
# If a character is followed by a colon, the option is expected to have an argument
7272
while getopts np:o:h OPT; do
7373
case "$OPT" in
74-
n)
75-
readonly SKIP_SSH=true
76-
;;
77-
p)
78-
port="$OPTARG"
79-
#ports+=("$OPTARG")
80-
;;
81-
o)
82-
readonly PROFILE_OPT="--profile $OPTARG"
83-
;;
84-
*) # -h or `?`: an unknown option
85-
usage
86-
;;
74+
n)
75+
readonly SKIP_SSH=true
76+
;;
77+
p)
78+
port="$OPTARG"
79+
#ports+=("$OPTARG")
80+
;;
81+
o)
82+
readonly PROFILE_OPT="--profile $OPTARG"
83+
;;
84+
*) # -h or `?`: an unknown option
85+
usage
86+
;;
8787
esac
8888
done
8989
echo -e "\n# \`$0 $*\`: run by \`${USER:-${USERNAME:-${LOGNAME:-UID #$UID}}}@${HOSTNAME}\`, in \`${PWD}\` #\n"
@@ -110,7 +110,7 @@ if ! command -v pcregrep >/dev/null; then
110110
exit 1
111111
fi
112112

113-
if ! command -v perl > /dev/null; then
113+
if ! command -v perl >/dev/null; then
114114
echo "Please install Perl"
115115
exit 1
116116
fi
@@ -142,7 +142,6 @@ if [[ ! $SSH_PUB_KEY ]]; then
142142
exit 1
143143
fi
144144

145-
146145
if [[ $port ]]; then
147146
echo -e "\nCreating a port forwarding tunnel for the port $port: this can take up to 20s to succeed ..."
148147
# `--session-ttl`: session duration in seconds (defaults to 30 minutes, maximum is 3 hours).
@@ -156,8 +155,8 @@ if [[ $port ]]; then
156155
echo "Bastion Port Forwarding Session OCID=$session_ocid"
157156

158157
# shellcheck disable=SC2086 # $PROFILE_OPT is a two-word CLI option
159-
ssh_command=$(oci bastion session get $PROFILE_OPT --session-id "$session_ocid" \
160-
| jq --raw-output '.data["ssh-metadata"].command')
158+
ssh_command=$(oci bastion session get $PROFILE_OPT --session-id "$session_ocid" |
159+
jq --raw-output '.data["ssh-metadata"].command')
161160
# Result: `ssh -i <privateKey> -N -L <localPort>:{HOST_IP}:5432 -p 22 [email protected]`
162161
# Remove the placeholder
163162
ssh_command="${ssh_command/-i <privateKey>/}"
@@ -186,8 +185,8 @@ if [[ $HOST_USER ]]; then
186185
echo "Bastion Session OCID=$session_ocid"
187186

188187
# shellcheck disable=SC2086 # $PROFILE_OPT is a two-word CLI option
189-
ssh_command=$(oci bastion session get $PROFILE_OPT --session-id "$session_ocid" \
190-
| jq --raw-output '.data["ssh-metadata"].command')
188+
ssh_command=$(oci bastion session get $PROFILE_OPT --session-id "$session_ocid" |
189+
jq --raw-output '.data["ssh-metadata"].command')
191190
# Result: `ssh -i <privateKey> -o ProxyCommand=\"ssh -i <privateKey> -W %h:%p -p 22
192191
# [email protected]\" -p 22 {HOST_USER}@{HOST_IP}`
193192
# Extract the bastion session SSH destination: the `[email protected]` part
@@ -203,8 +202,10 @@ if [[ $HOST_USER ]]; then
203202
# -i input edited in-place
204203
# -p iterate over filename arguments
205204
# -0 use null as record separator
205+
# Don't combine these options: the combination might not work
206206
# `@host` in the bastion session has to be escaped
207-
perl -i -p -0 -e "s/(Host ${OCI_INSTANCE}.*?)ProxyJump.*/\1ProxyJump ${bastion_session_dest//@/\\@}/s" ~/.ssh/config
207+
perl -i -p -0 -e "s/(Host ${OCI_INSTANCE}.*?)ProxyJump.*?\n/\1ProxyJump ${bastion_session_dest//@/\\@}\n/s" \
208+
~/.ssh/config
208209
else
209210
# Append
210211
cat >>~/.ssh/config <<HEREDOC

0 commit comments

Comments
 (0)