Skip to content

Instantly share code, notes, and snippets.

@ioxorg
Created February 1, 2023 06:54
Show Gist options
  • Save ioxorg/0618b32bb982dc03b5bbed3f427d77c2 to your computer and use it in GitHub Desktop.
Save ioxorg/0618b32bb982dc03b5bbed3f427d77c2 to your computer and use it in GitHub Desktop.
#!/bin/bash -e
usage(){
echo "kubectl ssh <pod_name> [-n | --namespace] [-u | --user] [<ssh-parameters>]"
}
get_node_ip(){
pod_name=$1
namespace=$2
kubectl get pods \
$pod_name \
$(test -n "$namespace" && echo "-n $namespace" || true) \
-o yaml | \
grep "hostIP:" | \
cut -f2 -d: | \
tr -d "[:blank:]"
exit "${PIPESTATUS[0]}"
}
if [ $# -lt 1 ]
then
usage
exit 1
fi
ssh_args=""
pod_name="$1"
shift
while test $# -gt 0
do
case $1 in
-u|--user)
user=$2
shift
shift
;;
-u=*|--user=*)
user="${1#*=}"
shift
;;
-n|--namespace)
namespace=$2
shift
shift
;;
-n=*|--namespace=*)
namespace="${1#*=}"
shift
;;
-h|--help)
usage
exit 0
;;
*)
ssh_args+="$1 "
shift
;;
esac
done
destination="$(get_node_ip $pod_name $namespace)"
if test -n "$user"
then
destination="${user}@${destination}"
fi
ssh $destination $ssh_args
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment