-
Notifications
You must be signed in to change notification settings - Fork 4
/
script_ssh.sh
executable file
·103 lines (98 loc) · 3.09 KB
/
script_ssh.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
# Global options
pkgtype='' # defined in get_sysinfo
os_name='' # defined in get_sysinfo
getpublicip='' # defined in get_sysinfo
KUBECTL_PLUGINS_LOCAL_FLAG_CLOUD_PROVIDER=aws
get_sysinfo()
{
found_file=""
for f in system-release redhat-release; do
[ -f "/etc/${f}" ] && found_file="${f}" && break
done
case "${found_file}" in
system-release)
pkgtype="rpm"
if grep --quiet "Linux" /etc/${found_file}; then
os_name="Linux"
elif grep --quiet "Red Hat" /etc/${found_file}; then
os_name="Linux"
fi
;;
redhat-release )
pkgtype="deb"
if grep --quiet "8" /etc/${found_file}; then
os_name="Linux"
fi
;;
*)
OS=$(uname -s)
if [ "${OS}" == "Darwin" ]; then
os_name="Darwin"
fi
;;
*)
echo "Unsupported OS detected."
exit 1
;;
esac
}
get_publicIP()
{
get_sysinfo
case "${os_name}" in
Linux)
getpublicip="$HOME/.kube/plugins/kubectl-ssh-plugin-eks/getpublicip_linux" 2>&1
;;
Darwin)
getpublicip="$HOME/.kube/plugins/kubectl-ssh-plugin-eks/getpublicip_darwin" 2>&1
;;
esac
}
if [ "$KUBECTL_PLUGINS_LOCAL_FLAG_SELECTOR" == "" ] && [ -z "$1" ]
then
echo "Node name cannot be empty"
exit 1
else [ "$KUBECTL_PLUGINS_LOCAL_FLAG_CLOUD_PROVIDER" == "aws" ]
if [ "$KUBECTL_PLUGINS_LOCAL_FLAG_SELECTOR" == "" ]
then
selector=""
path="{.spec.externalID}"
pathregion="{.spec.providerID}"
else
selector="-l $KUBECTL_PLUGINS_LOCAL_FLAG_SELECTOR"
path="{.items[0].spec.externalID}"
pathregion="{.items[0].spec.providerID}"
fi
instanceID=$(kubectl get node $1 -o jsonpath="${path}" $selector)
if [ -z "${instanceID}" ]; then
instanceID=$(kubectl get node $1 -o jsonpath="${pathregion}" $selector | awk -F/ '{print $5}')
fi
region=$(kubectl get node $1 -o jsonpath="${pathregion}" $selector | awk -F/ '{print $4}')
get_publicIP
BOTH_IP_DNS=`${getpublicip} -region=${region%?} -instanceid=$instanceID`
DNS_TYPE=`echo "${BOTH_IP_DNS}" | awk -F " " '{print $1}'`
echo $DNS_TYPE
IP=`echo "${BOTH_IP_DNS}" | awk -F " " '{print $2}'`
if [ "${DNS_TYPE}" == "PrivateDnsName" ] && [ "${KUBECTL_PLUGINS_LOCAL_FLAG_IDENTITY_FILE}" == "" ]
then
echo "Worker Nodes does not have IGW, do you wish to continue to SSHing into ${IP} ?"
select yn in "Yes" "No"; do
case $yn in
Yes ) [[ ! -z "${IP}" ]] && ssh ${KUBECTL_PLUGINS_LOCAL_FLAG_SSH_USER}@$IP || echo "Can't SSH"; exit 1;;
No ) exit;;
esac
done
elif [ "${DNS_TYPE}" == "PrivateDnsName" ]
then
echo "Worker Nodes does not have IGW, do you still want to SSH ?"
select yn in "Yes" "No"; do
case $yn in
Yes ) [[ ! -z "${IP}" ]] && ssh -i ${KUBECTL_PLUGINS_LOCAL_FLAG_IDENTITY_FILE} ${KUBECTL_PLUGINS_LOCAL_FLAG_SSH_USER}@$IP || echo "Can't SSH"; exit 1;;
No ) exit;;
esac
done
else
[[ ! -z "${IP}" ]] && echo "SSHing into Worker Node with IP: ${IP}" && ssh -i ${KUBECTL_PLUGINS_LOCAL_FLAG_IDENTITY_FILE} ${KUBECTL_PLUGINS_LOCAL_FLAG_SSH_USER}@$IP || echo "Can't SSH"; exit 1;
fi
fi