Skip to content

Commit c2cd357

Browse files
authored
Merge pull request #2 from iuryfukuda/master
Possibility to pass commands by arg parameter
2 parents e1168b6 + 03290a8 commit c2cd357

File tree

2 files changed

+69
-63
lines changed

2 files changed

+69
-63
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ Options:
9494
-i, --identity <identity_file> Identity key file
9595
-p, --pubkey <pub_key_file> Public key file
9696
-P, --port <port> SSH port for target node SSH server (default:22)
97+
-a, --args <args> Args to exec in ssh session
9798
--skip-agent Skip automatically starting SSH agent and adding
9899
SSH Identity key into the agent before SSH login
99100
(=> You need to manage SSH agent by yourself)
100101
--cleanup-agent Clearning up SSH agent at the end
101-
The agent is NOT cleaned up in case that
102+
The agent is NOT cleaned up in case that
102103
--skip-agent option is given
103104
--cleanup-jump Clearning up sshjump pod at the end
104105
Default: Skip cleaning up sshjump pod
@@ -138,6 +139,7 @@ Options:
138139
-i, --identity <identity_file> Identity key file
139140
-p, --pubkey <pub_key_file> Public key file
140141
-P, --port <port> SSH port for target node SSH server (default:22)
142+
-a, --args <args> Args to exec in ssh session
141143
--skip-agent Skip automatically starting SSH agent and adding
142144
SSH Identity key into the agent before SSH login
143145
(=> You need to manage SSH agent by yourself)
@@ -181,6 +183,13 @@ echo "uname -a" | kubectl ssh-jump aks-nodepool1-18558189-0
181183
Linux aks-nodepool1-18558189-0 4.15.0-1035-azure #36~16.04.1-Ubuntu SMP Fri Nov 30 15:25:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
182184
```
183185
186+
You can pass commands by `args`
187+
``` sh
188+
kubectl ssh-jump aks-nodepool1-18558189-0 --args "uname -a"
189+
190+
(Output)
191+
Linux aks-nodepool1-18558189-0 4.15.0-1035-azure #36~16.04.1-Ubuntu SMP Fri Nov 30 15:25:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
192+
```
184193
185194
You can clean up sshjump pod at the end of the command with `--cleanup-jump` option, otherwise, the sshjump pod stay running by default.
186195
```sh

kubectl-ssh-jump

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Options:
2626
-i, --identity <identity_file> Identity key file
2727
-p, --pubkey <pub_key_file> Public key file
2828
-P, --port <port> SSH port for target node SSH server (default:22)
29+
-a, --args <args> Args to exec in ssh session
2930
--skip-agent Skip automatically starting SSH agent and adding
3031
SSH Identity key into the agent before SSH login
3132
(=> You need to manage SSH agent by yourself)
@@ -56,6 +57,7 @@ write_options(){
5657
cat << EOF > ${PLUGIN_SSH_OPTIONS_FILE}
5758
sshuser=${sshuser}
5859
identity=${identity}
60+
5961
pubkey=${pubkey}
6062
port=${port}
6163
EOF
@@ -112,9 +114,10 @@ run_ssh_node(){
112114
local identity="$3"
113115
local pubkey="$4"
114116
local port="$5"
117+
local sshargs="$6"
115118

116119
# Install an SSH Server if not yet installed
117-
r=$(kubectl get pod sshjump 2>/dev/null | tail -1 | awk '{print $1}')
120+
r=$(kubectl get pod sshjump 2>/dev/null | tail -1 | awk '{print $1}') #
118121
if [ "${r}" != "sshjump" ];then
119122
echo "Creating SSH jump host (Pod)..."
120123
kubectl run sshjump --image=corbinu/ssh-server --port=22 --restart=Never
@@ -140,71 +143,61 @@ run_ssh_node(){
140143
# Using the SSH Server as a jumphost (via port-forward proxy), ssh into the desired Node
141144
ssh -i ${identity} -p ${port} ${sshuser}@${destnode} \
142145
-o "ProxyCommand ssh root@127.0.0.1 -p 2222 -i ${identity} -o \"StrictHostKeyChecking=no\" \"nc %h %p\"" \
143-
-o "StrictHostKeyChecking=no"
146+
-o "StrictHostKeyChecking=no" $sshargs
144147

145148
# Stop port-forward
146149
kill -3 ${pid_port_forward} 2>/dev/null
147150
}
148151

149-
plugin_main(){
150-
skip_agent="no"
151-
cleanup_jump="no"
152-
cleanup_agent="no"
153-
154-
for arg in "$@"; do
155-
option=""
156-
if [ "${arg:0:1}" = "-" ]; then
157-
if [ "${arg:1:1}" = "-" ]; then
158-
option="${arg:2}"
159-
prevopt="${arg:2}"
160-
else
161-
index=1
162-
while o="${arg:$index:1}"; do
163-
[ -n "$o" ] || break
164-
option="$o"
165-
prevopt="$o"
166-
let index+=1
167-
done
168-
fi
169-
case "${option}" in
170-
"h" | "help" )
171-
help
172-
exit 0
173-
;;
174-
"cleanup-jump" )
175-
cleanup_jump="yes"
176-
;;
177-
"cleanup-agent" )
178-
cleanup_agent="yes"
179-
;;
180-
"skip-agent" )
181-
skip_agent="yes"
182-
;;
183-
esac
184-
else
185-
if [ "${prevopt}" = "" ]; then
186-
destnode="${arg}"
187-
else
188-
case "${prevopt}" in
189-
"u" | "user" )
190-
c_sshuser="${arg}"
191-
;;
192-
"i" | "identity" )
193-
c_identity="${arg}"
194-
;;
195-
"p" | "pubkey" )
196-
c_pubkey="${arg}"
197-
;;
198-
"P" | "port" )
199-
c_port="${arg}"
200-
;;
201-
* )
202-
help >&2
203-
exit 1
204-
;;
205-
esac
206-
fi
207-
fi
152+
plugin_main() {
153+
skip_agent=no
154+
cleanup_jump=no
155+
cleanup_agent=no
156+
while [ $# -gt 0 ] ; do
157+
nSkip=1
158+
case $1 in
159+
"-h" | "--help")
160+
help
161+
exit 0
162+
;;
163+
"--cleanup-jump")
164+
cleanup_jump=yes
165+
;;
166+
"--cleanup-agent")
167+
cleanup_agent=yes
168+
;;
169+
"--skip-agent")
170+
skip_agent=yes
171+
;;
172+
"-u" | "--user" )
173+
c_sshuser=$2
174+
nSkip=2
175+
;;
176+
"-i" | "--identity" )
177+
c_identity=$2
178+
nSkip=2
179+
;;
180+
"-p" | "--pubkey" )
181+
c_pubkey=$2
182+
nSkip=2
183+
;;
184+
"-P" | "--port")
185+
c_port=$2
186+
nSkip=2
187+
;;
188+
"-a" | "--args" )
189+
sshargs="$2"
190+
nSkRip=2
191+
;;
192+
[a-z]*)
193+
destnode=$1
194+
;;
195+
*)
196+
help >&2
197+
exit 1
198+
;;
199+
esac
200+
shift $nSkip
208201
done
209202

210203
if [[ "$(type kubectl &>/dev/null; echo $?)" -eq 1 ]]; then
@@ -256,14 +249,18 @@ plugin_main(){
256249
c_port="${port}"
257250
fi
258251

252+
if [ "${sshargs}" != "" ]; then
253+
echo "using: args=$sshargs"
254+
fi
255+
259256
# Caching current ssh options
260257
write_options ${c_sshuser} ${c_identity} ${c_pubkey} ${c_port}
261258

262259
if [ "${skip_agent}" = "no" ]; then
263260
check_and_start_agent ${c_identity}
264261
fi
265262
# SSH Logging into desitnation node via Jump host
266-
run_ssh_node ${destnode} ${c_sshuser} ${c_identity} ${c_pubkey} ${c_port}
263+
run_ssh_node ${destnode} ${c_sshuser} ${c_identity} ${c_pubkey} ${c_port} "${sshargs}"
267264

268265
# Cleaning up resources if needed
269266
if [ "${cleanup_jump}" = "yes" ]; then

0 commit comments

Comments
 (0)