-
Notifications
You must be signed in to change notification settings - Fork 82
/
ssh.lua
240 lines (218 loc) · 8.81 KB
/
ssh.lua
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
-- Some modifications added based on:
-- https://github.com/dodmi/Clink-Addons
local clink_version = require('clink_version')
local w = require('tables').wrap
local parser = clink.arg.new_parser
local function read_lines (filename)
local lines = w({})
local f = io.open(filename)
if not f then
return lines
end
for line in f:lines() do
table.insert(lines, line)
end
f:close()
return lines
end
local function extract_address(pattern, match_type, portflag)
if not pattern then
return nil
end
local addr, port = pattern:match('%[([^%]]+)%]:(%d+)')
if not addr then
addr = pattern:match('%[([^%]]+)%]')
end
if not addr then
addr = pattern
end
local match
if portflag and port then
match = addr .. portflag .. port
else
match = addr
end
if clink_version.supports_display_filter_description then
return { match=match, type=match_type }
else
return match
end
end
-- read all Host entries in the user's ssh config file
local function list_ssh_hosts(portflag)
local matches = w({})
local lines = read_lines(clink.get_env("userprofile") .. "/.ssh/config")
for _, line in ipairs(lines) do
line = line:gsub('(#.*)$', '')
local host = line:match('^Host%s+(.*)$')
if host then
for pattern in host:gmatch('([^%s]+)') do
if not pattern:match('[%*%?/!]') then
table.insert(matches, extract_address(pattern, 'alias', portflag))
end
end
end
end
return matches:filter()
end
local function list_known_hosts(portflag)
return read_lines(clink.get_env("userprofile") .. "/.ssh/known_hosts")
:map(function (line)
line = line:gsub('(#.*)$', '')
return extract_address(line:match('^([^%s,]*).*'), 'cmd', portflag)
end)
:filter()
end
local function match_display_filter(matches)
for i, v in ipairs(matches) do
matches[i] = v:gsub('( %-p )', '\027[39m%1')
end
return matches
end
local function ondisplaymatches(matches)
local new_matches = {}
for _,m in ipairs(matches) do
m.display = m.match:gsub('( %-p )', '\027[39m%1')
table.insert(new_matches, m)
end
return new_matches
end
local function hosts_with_port_flag(token) -- luacheck: no unused args
if clink_version.supports_display_filter_description then
clink.ondisplaymatches(ondisplaymatches)
else
clink.match_display_filter = match_display_filter
end
return list_ssh_hosts(' -p ')
:concat(list_known_hosts(' -p '))
end
local function hosts_with_port(token) -- luacheck: no unused args
if clink_version.supports_display_filter_description then
clink.ondisplaymatches(ondisplaymatches)
else
clink.match_display_filter = match_display_filter
end
return list_ssh_hosts(':')
:concat(list_known_hosts(':'))
end
local function hosts(token) -- luacheck: no unused args, no unused
return list_ssh_hosts()
:concat(list_known_hosts())
end
-- return the list of available local ips
local function localIPs(token) -- luacheck: no unused args
local assignedIPs = {}
local f = io.popen('2>nul wmic nicconfig list IP')
if f then
local netLine
for line in f:lines() do
netLine = line:match('%{(.*)%}')
if netLine then
for ip in netLine:gmatch('%"([^,%s]*)%"') do
table.insert(assignedIPs, ip)
end
end
end
f:close()
end
return assignedIPs
end
-- return the list of supported ciphers
local function supportedCiphers(token) -- luacheck: no unused args
local ciphers = {}
local f = io.popen('2>nul ssh -Q cipher')
if f then
for line in f:lines() do
table.insert(ciphers, line)
end
f:close()
end
return ciphers
end
-- return the list of supported MACs
local function supportedMACs(token) -- luacheck: no unused args
local macs = {}
local f = io.popen('2>nul ssh -Q mac')
if f then
for line in f:lines() do
table.insert(macs, line)
end
f:close()
end
return macs
end
-- return the list of supported options
local function supportedOptions(token) -- luacheck: no unused args
--[[ curl --silent https://raw.githubusercontent.com/openssh/openssh-portable/master/ssh_config.5 | awk '$1==".It" && $2=="Cm" && $3!="Host" && $3!="Match" {print " "$3}' | sort ]] -- luacheck: no max line length
local options = {
"AddKeysToAgent", "AddressFamily", "BatchMode", "BindAddress", "BindInterface", "CanonicalDomains",
"CanonicalizeFallbackLocal", "CanonicalizeHostname", "CanonicalizeMaxDots", "CanonicalizePermittedCNAMEs",
"CASignatureAlgorithms", "CertificateFile", "CheckHostIP", "Ciphers", "ClearAllForwardings", "Compression",
"ConnectionAttempts", "ConnectTimeout", "ControlMaster", "ControlPath", "ControlPersist", "DynamicForward",
"EnableEscapeCommandline", "EnableSSHKeysign", "EscapeChar", "ExitOnForwardFailure", "FingerprintHash",
"ForkAfterAuthentication", "ForwardAgent", "ForwardX11", "ForwardX11Timeout", "ForwardX11Trusted",
"GatewayPorts", "GlobalKnownHostsFile", "GSSAPIAuthentication", "GSSAPIDelegateCredentials", "HashKnownHosts",
"HostbasedAcceptedAlgorithms", "HostbasedAuthentication", "HostKeyAlgorithms", "HostKeyAlias", "Hostname",
"IdentitiesOnly", "IdentityAgent", "IdentityFile", "IgnoreUnknown", "Include", "IPQoS",
"KbdInteractiveAuthentication", "KbdInteractiveDevices", "KexAlgorithms", "KnownHostsCommand", "LocalCommand",
"LocalForward", "LogLevel", "LogVerbose", "MACs", "NoHostAuthenticationForLocalhost",
"NumberOfPasswordPrompts", "ObscureKeystrokeTiming", "PasswordAuthentication", "PermitLocalCommand",
"PermitRemoteOpen", "PKCS11Provider", "Port", "PreferredAuthentications", "ProxyCommand", "ProxyJump",
"ProxyUseFdpass", "PubkeyAcceptedAlgorithms", "PubkeyAuthentication", "RekeyLimit", "RemoteCommand",
"RemoteForward", "RequestTTY", "RequiredRSASize", "RevokedHostKeys", "SecurityKeyProvider", "SendEnv",
"ServerAliveCountMax", "ServerAliveInterval", "SessionType", "SetEnv", "StdinNull", "StreamLocalBindMask",
"StreamLocalBindUnlink", "StrictHostKeyChecking", "SyslogFacility", "Tag", "TCPKeepAlive", "Tunnel",
"TunnelDevice", "UpdateHostKeys", "User", "UserKnownHostsFile", "VerifyHostKeyDNS", "VisualHostKey",
"XAuthLocation",
}
return options
end
local ssh_parser = parser({hosts_with_port_flag},
"-4", "-6", "-A", "-a", "-C", "-f", "-G", "-g", "-K", "-k", "-M",
"-N", "-n", "-q", "-s", "-T", "-t", "-V", "-v", "-X", "-x", "-Y", "-y",
"-B" .. parser({fromhistory=true}), -- How to find available bind_interface's?
"-b" .. parser({localIPs}),
"-c" .. parser({supportedCiphers}),
"-D" .. parser({fromhistory=true, localIPs}),
"-E" .. parser({clink.filematches}),
"-e" .. parser({fromhistory=true}),
"-F" .. parser({clink.filematches}),
"-I" .. parser({fromhistory=true}),
"-i" .. parser({clink.filematches}),
"-J" .. parser({hosts_with_port}),
"-L" .. parser({fromhistory=true}),
"-l" .. parser({fromhistory=true}),
"-m" .. parser({supportedMACs}),
"-O" .. parser({fromhistory=true}),
"-o" .. parser({supportedOptions}),
"-p" .. parser({fromhistory=true}),
"-Q" .. parser({"cipher", "cipher_auth", "help", "mac", "kex", "kex-gss", "key", "key-cert", "key-plain", "key-sig", "protocol-version", "sig"}), -- luacheck: no max line length
"-R" .. parser({fromhistory=true}),
"-S" .. parser({fromhistory=true}),
"-W" .. parser({hosts_with_port}),
"-w" .. parser({fromhistory=true})
)
:adddescriptions({
["-B"] = { " bind_interface", "" },
["-b"] = { " bind_address", "" },
["-c"] = { " cipher_spec", "" },
["-D"] = { " [bind_address:]port", "" },
["-E"] = { " log_file", "" },
["-e"] = { " escape_char", "" },
["-F"] = { " configfile", "" },
["-I"] = { " pkcs11", "" },
["-i"] = { " identity_file", "" },
["-J"] = { " [user@]host[:port]", "" },
["-L"] = { " address", "" },
["-l"] = { " login_name", "" },
["-m"] = { " mac_spec", "" },
["-O"] = { " ctl_cmd", "" },
["-o"] = { " option", "" },
["-p"] = { " port", "" },
["-Q"] = { " query_option", "" },
["-R"] = { " address", "" },
["-S"] = { " ctl_path", "" },
["-W"] = { " host:port", "" },
["-w"] = { " local_tun[:remote_tun]", "" },
})
clink.arg.register_parser("ssh", ssh_parser)