-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshinfo.plugin.zsh
More file actions
260 lines (228 loc) · 11.2 KB
/
sshinfo.plugin.zsh
File metadata and controls
260 lines (228 loc) · 11.2 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Oh My Zsh sshinfo plugin
# Displays resolved SSH connection information before connecting.
if ! command -v sshinfo >/dev/null 2>&1; then
sshinfo() {
local __target="" __style="${ZSH_SSHINFO_STYLE:-staircase}"
local -a __ssh_args
for arg in "$@"; do
if [[ "$arg" == "--inline" ]]; then
__style="inline"
elif [[ "$arg" == "--staircase" ]]; then
__style="staircase"
else
[[ "$arg" != -* ]] && __target="$arg"
__ssh_args+=("$arg")
fi
done
if [[ -z "$__target" ]]; then
command ssh "$@"
return
fi
set -- "${__ssh_args[@]}"
local GREEN BLUE YELLOW RED RESET
if [[ -n "$TERM" && "$TERM" != "dumb" ]]; then
GREEN="$(tput setaf 2)"
BLUE="$(tput setaf 4)"
YELLOW="$(tput setaf 3)"
RED="$(tput setaf 1)"
RESET="$(tput sgr0)"
fi
local -A __config
local -a __identity_files __local_forwards __remote_forwards
local __whitelist=("user" "hostname" "port" "proxyjump" "proxycommand" "dynamicforward")
# Use process substitution to avoid variable dumps
while IFS= read -r line; do
local key="${line%% *}" value="${line#* }"
key="${key:l}"
if [[ " ${__whitelist[*]} " =~ " ${key} " ]]; then
__config[$key]=$value
elif [[ "$key" == "identityfile" ]]; then
__identity_files+=("$value")
elif [[ "$key" == "localforward" ]]; then
__local_forwards+=("$value")
elif [[ "$key" == "remoteforward" ]]; then
__remote_forwards+=("$value")
fi
done < <(command ssh -G "$__target" 2>/dev/null)
if [[ ${#__config} -eq 0 ]]; then
echo "${RED}❌ Host '$__target' not found or error in SSH configuration.${RESET}"
command ssh "$@"
return $?
fi
local C_BOLD="\e[1m" C_DIM="\e[2m" C_GRAY="\e[38;5;242m" C_CYAN="\e[38;5;45m" C_PURPLE="\e[38;5;141m"
local __conf_host="${__config[hostname]}"
local __target_ip=""
if [[ -n "$__conf_host" ]]; then
__target_ip=$(getent hosts "$__conf_host" 2>/dev/null | awk '{print $1}' | head -n1)
[[ -z "$__target_ip" ]] && __target_ip=$(dig +short "$__conf_host" 2>/dev/null | head -n1)
fi
local -a __hop_nodes
local __target_real="$__conf_host"
[[ -z "$__target_real" ]] && __target_real="$__target"
__hop_nodes=("${C_BOLD}${__target}${RESET}${C_DIM} [${__target_real}]${RESET}")
local __current_hop=""
local __pj="${__config[proxyjump]}"
local __pc="${__config[proxycommand]}"
if [[ -n "$__pj" ]]; then
__current_hop="${__pj%%,*}"
elif [[ -n "$__pc" ]]; then
local -a __args
__args=(${(z)__pc})
if [[ "${__args[1]}" == "ssh" ]]; then
for arg in "${__args[@]:1}"; do
[[ "$arg" == -* || "$arg" == *%* || "$arg" == "nc" || "$arg" == "proxyconnect" ]] && continue
__current_hop="$arg"
break
done
fi
fi
if [[ -n "$__current_hop" ]]; then
local -A __seen_hops
__seen_hops[$__target]=1
local __depth=0
while (( __depth++ < 5 )); do
[[ -z "$__current_hop" || -n "${__seen_hops[$__current_hop]}" ]] && break
__seen_hops[$__current_hop]=1
local __h_real="" __next_jump="" __next_cmd="" __found_hop=0
while IFS= read -r __h_line; do
__found_hop=1
local __h_key="${__h_line%% *}" __h_val="${__h_line#* }"
__h_key="${__h_key:l}"
[[ "$__h_key" == "hostname" ]] && __h_real="$__h_val"
[[ "$__h_key" == "proxyjump" ]] && __next_jump="$__h_val"
[[ "$__h_key" == "proxycommand" ]] && __next_cmd="$__h_val"
done < <(command ssh -G "$__current_hop" 2>/dev/null)
[[ $__found_hop -eq 0 ]] && break
__hop_nodes=("${C_CYAN}${__current_hop}${RESET}${C_DIM} [${__h_real:-?}]${RESET}" "${__hop_nodes[@]}")
local __next_h_tmp=""
if [[ -n "$__next_jump" ]]; then
__next_h_tmp="${__next_jump%%,*}"
elif [[ -n "$__next_cmd" ]]; then
local -a __h_args
__h_args=(${(z)__next_cmd})
if [[ "${__h_args[1]}" == "ssh" ]]; then
for h_arg in "${__h_args[@]:1}"; do
[[ "$h_arg" == -* || "$h_arg" == *%* || "$h_arg" == "nc" || "$h_arg" == "proxyconnect" ]] && continue
__next_h_tmp="$h_arg"
break
done
fi
fi
[[ -z "$__next_h_tmp" ]] && break
__current_hop="$__next_h_tmp"
done
fi
echo "\n ${GREEN}🟢${RESET} ${C_BOLD}SSH Connection to ${C_CYAN}${__target}${RESET}"
local __user="${__config[user]}"
local __port="${__config[port]}"
local __df="${__config[dynamicforward]}"
local __has_conn=0 __has_auth=0 __has_proxy=0
[[ -n "$__user" || -n "$__conf_host" || -n "$__port" ]] && __has_conn=1
(( ${#__identity_files[@]} > 0 )) && __has_auth=1
[[ -n "$__pj" || -n "$__pc" || -n "$__df" || ${#__local_forwards[@]} -gt 0 || ${#__remote_forwards[@]} -gt 0 ]] && __has_proxy=1
local __total_sec=$((__has_conn + __has_auth + __has_proxy))
local __cur_sec=0
if (( __has_conn )); then
__cur_sec=$((__cur_sec + 1))
echo " ${C_GRAY}╭──${RESET} ${C_PURPLE}${C_BOLD}CONNECTION${RESET}"
[[ -n "$__user" ]] && printf " ${C_GRAY}│${RESET} ${C_GRAY}👤${RESET} User : ${C_BOLD}%s${RESET}\n" "$__user"
if [[ -n "$__conf_host" ]]; then
local __ip_display=""
[[ -n "$__target_ip" ]] && __ip_display=" ($__target_ip)"
printf " ${C_GRAY}│${RESET} ${C_GRAY}🌐${RESET} Host : ${C_BOLD}%s${RESET}${C_DIM}%s${RESET}\n" "$__conf_host" "$__ip_display"
fi
[[ -n "$__port" ]] && printf " ${C_GRAY}│${RESET} ${C_GRAY}🔌${RESET} Port : %s\n" "$__port"
echo " ${C_GRAY}│${RESET}"
fi
if (( __has_auth )); then
__cur_sec=$((__cur_sec + 1))
local __l_top="├──"
(( __cur_sec == 1 )) && __l_top="╭──"
echo " ${C_GRAY}${__l_top}${RESET} ${C_PURPLE}${C_BOLD}SECURITY${RESET}"
if (( ${#__identity_files[@]} > 1 )); then
printf " ${C_GRAY}│${RESET} ${C_GRAY}🔑${RESET} Key : %s\n" "Default (~/.ssh/id_*)"
else
local __k_path="${__identity_files[1]}"
[[ -n "$__k_path" ]] && __k_path="${__k_path/#$HOME/~}"
printf " ${C_GRAY}│${RESET} ${C_GRAY}🔑${RESET} Key : %s\n" "$__k_path"
fi
echo " ${C_GRAY}│${RESET}"
fi
if (( __has_proxy )); then
__cur_sec=$((__cur_sec + 1))
local __l_top="├──"
(( __cur_sec == 1 )) && __l_top="╭──"
echo " ${C_GRAY}${__l_top}${RESET} ${C_PURPLE}${C_BOLD}NETWORK PATH${RESET}"
if [[ "$__style" == "staircase" ]]; then
printf " ${C_GRAY}│${RESET} ${C_GRAY}🛤️${RESET} Route : %b\n" "${__hop_nodes[1]}"
for (( i=2; i <= ${#__hop_nodes[@]}; i++ )); do
local __ind=$(( 15 + (i-2)*6 ))
printf " ${C_GRAY}│${RESET}%*s ${C_GRAY}╰─>${RESET} %b\n" "$__ind" "" "${__hop_nodes[i]}"
done
else
local __inline=""
for (( i=1; i <= ${#__hop_nodes[@]}; i++ )); do
__inline+="${__hop_nodes[i]}"
(( i < ${#__hop_nodes[@]} )) && __inline+=" ${C_GRAY}➜${RESET} "
done
printf " ${C_GRAY}│${RESET} ${C_GRAY}🛤️${RESET} Route : %b\n" "$__inline"
fi
[[ -n "$__df" ]] && printf " ${C_GRAY}│${RESET} ${C_GRAY}📡${RESET} Forward : %s\n" "$__df"
for lf in "${__local_forwards[@]}"; do printf " ${C_GRAY}│${RESET} ${C_GRAY}↪${RESET} LocalFwd : %s\n" "$lf"; done
for rf in "${__remote_forwards[@]}"; do printf " ${C_GRAY}│${RESET} ${C_GRAY}↪${RESET} RemoteFwd: %s\n" "$rf"; done
fi
echo " ${C_GRAY}╰───────────────────────────────────────────${RESET}\n"
command ssh "$@"
}
fi
if ! command -v s >/dev/null 2>&1; then alias s='sshinfo'; fi
if ! command -v connect >/dev/null 2>&1; then alias connect='sshinfo'; fi
alias ssh='sshinfo'
_sshinfo_find_all_config_files() {
local -a queue
[[ -f "$HOME/.ssh/config" ]] && queue=("$HOME/.ssh/config")
local -a seen=("${queue[@]}")
local i=1
while (( i <= ${#queue} )); do
local file="${queue[i++]}"
local config_dir="${file:h}"
while IFS= read -r line; do
if [[ ${line:l} =~ '^[[:space:]]*include[[:space:]]' ]]; then
local patterns_str="${line#[iI][nN][cC][lL][uU][dD][eE] }"
local -a patterns
patterns=("${(z)patterns_str}")
for pattern in "${patterns[@]}"; do
local full_pattern
if [[ "$pattern" != /* && "$pattern" != ~* ]]; then
full_pattern="$config_dir/$pattern"
else
full_pattern="${pattern/# /$HOME}"
fi
local -a found_files=(${~full_pattern}(N))
for f in "${found_files[@]}"; do
local found=0
for seen_file in "${seen[@]}"; do [[ "$seen_file" == "$f" ]] && found=1 && break; done
if [[ -f "$f" && $found -eq 0 ]]; then
seen+=("$f")
queue+=("$f")
fi
done
done
fi
done < "$file"
done
echo "${seen[@]}"
}
_sshinfo_parse_hosts_from_files() {
awk '/^[[:space:]]*($|#)/{next} tolower($1)=="host"{for(i=2;i<=NF;i++){if(substr($i,1,1)=="#")break;if($i!~/[*?]/&&$i!~/^!/)print $i}}' "$@" 2>/dev/null
}
_sshinfo_complete() {
local -a hosts config_files
config_files=($(_sshinfo_find_all_config_files))
(( ${#config_files[@]} > 0 )) && hosts+=($(_sshinfo_parse_hosts_from_files "${config_files[@]}"))
[[ -r "${HOME}/.ssh/known_hosts" ]] && hosts+=($(awk '!/^(#|\|)/{print $1}' "${HOME}/.ssh/known_hosts" | tr ',' '\n'))
local -a unique_hosts=("${(@u)hosts}")
unique_hosts=("${(@)unique_hosts:#line=''}")
compadd -a unique_hosts
}
compdef _sshinfo_complete sshinfo s connect