-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli.py
361 lines (308 loc) · 11 KB
/
cli.py
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env python3
from __future__ import print_function, absolute_import
import os
import stat
import json
import getpass
import socket
import sys
# windows does not have termios...
try:
import termios
import tty
has_termios = True
except ImportError:
has_termios = False
import click
from ssh_config.client import Host
from ssh_config.client import SSHConfig
from ssh_config.keywords import Keywords
from ssh_config.version import __version__
def get_sshconfig(configpath, create=True):
config_fullpath = os.path.expanduser(configpath)
sshconfig = SSHConfig(config_fullpath)
return sshconfig
def write_config(config, msg, success):
if click.confirm(msg, abort=False):
config.write()
click.secho(success, fg="green")
def posix_shell(chan):
from paramiko.py3compat import u
import select
oldtty = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(0.0)
while True:
r, w, e = select.select([chan, sys.stdin], [], [])
if chan in r:
try:
x = u(chan.recv(1024))
if len(x) == 0:
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in r:
x = sys.stdin.read(1)
if len(x) == 0:
break
chan.send(x)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
@click.group()
@click.option(
"-f", "--path", default=os.path.expanduser("~/.ssh/config"), show_default=True
)
@click.option("--debug/--no-debug", default=False)
@click.version_option(__version__)
@click.pass_context
def cli(ctx, path, debug):
ctx.ensure_object(dict)
ctx.obj["DEBUG"] = debug
ctx.obj["path"] = path
if os.path.exists(path):
ctx.obj["config"] = get_sshconfig(path)
else:
if ctx.invoked_subcommand != "gen":
raise SystemExit(f"SSH config does not exists, {path}")
@cli.command("attributes")
def get_attributes():
"""Print possible attributes for Host"""
for attr, attr_type in Keywords:
click.echo(f"{attr}")
@cli.command("ssh")
@click.argument("name")
@click.pass_context
def interactive_shell(ctx, name):
"""Interative shell for Host"""
config = ctx.obj["config"]
if not config.exists(name):
click.secho(f"{name} does not exist", fg="red")
raise SystemExit
host = config.get(name)
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if host.IdentityFile:
identity_file = os.path.expanduser(host.IdentityFile)
port = host.Port or 22
click.echo(f"{host.HostName}, {host.User}, {port}, {host.IdentityFile}")
if identity_file is None:
password = getpass.getpass(f"{host.User}@{name}'s password: ")
else:
password = None
try:
ssh.connect(
host.HostName,
username=host.User,
port=port,
password=password,
key_filename=identity_file,
allow_agent=True,
)
channel = ssh.get_transport().open_session()
channel.get_pty()
channel.invoke_shell()
posix_shell(channel)
except Exception as e:
click.secho(f"Failed to connect to ssh, {e}", fg="red")
ssh.close()
@cli.command("gen")
@click.pass_context
def gen_config(ctx):
"""Generate the ssh config"""
config_path = ctx.obj["path"]
ssh_path = os.path.dirname(config_path)
if not os.path.exists(config_path):
if not os.path.exists(ssh_path):
os.mkdir(ssh_path)
open(config_path, "w").close()
os.chmod(config_path, stat.S_IREAD | stat.S_IWRITE)
click.echo(f"Created at {config_path}")
else:
if click.confirm(
f"Do you want to overwrite (file: {config_path})?", abort=True
):
open(config_path, "w").close()
os.chmod(config_path, stat.S_IREAD | stat.S_IWRITE)
click.echo(f"Created at {config_path}")
@cli.command("ls")
@click.option("-l", is_flag=True, help="More detail")
@click.pass_context
def list_config(ctx, l):
"""Enumerate the configs"""
config = ctx.obj["config"]
for host in config:
if l:
click.echo(f"{host.name:20s}{host.HostName}")
else:
click.echo(host.name)
return 0
@cli.command("get")
@click.argument("name")
@click.pass_context
def get_config(ctx, name):
"""Get ssh config with name"""
config = ctx.obj["config"]
if not config.exists(name):
click.secho(f"No host found, {name}", fg="red")
raise SystemExit()
selected = config.get(name)
click.echo(selected)
return 0
@cli.command("add")
@click.argument("name")
@click.argument("attributes", metavar="<Attribute=Value>", nargs=-1)
@click.pass_context
def add_config(ctx, name, attributes):
"""Add SSH Config into config file"""
config = ctx.obj["config"]
if config.exists(name):
click.secho(f"{name} already exists, use `update` instead of `add`", fg="red")
raise SystemExit
attrs = {}
for attribute in attributes:
try:
# TODO: Check validation
attribute, value = attribute.split("=")
if attribute == 'Port':
value = int(value)
attrs[attribute] = value
except Exception:
raise AttributeError("attribute format is <Attribute=Value>")
# Ask the essential attributes
if 'HostName' not in attrs:
attrs['HostName'] = click.prompt("HostName")
if 'User' not in attrs:
attrs['User'] = click.prompt("User", default=os.getenv("USER"), show_default=True)
attrs['Port'] = attrs.get("Port") or click.prompt("Port",
type=int, default=22, show_default=True)
if 'IdentityFile' not in attrs:
attrs['IdentityFile'] = click.prompt("IdentityFile",
type=str, default="~/.ssh/id_rsa",
show_default=True)
if not attributes:
while click.confirm("Do you have additonal attribute?"):
# TODO: Check validation
attribute = click.prompt("Attribute: ")
value = click.prompt("Value: ")
attrs[attribute] = value
host = Host(name, attrs)
config.add(host)
click.echo(host)
write_config(config, "Information is correct ?", "Added!")
@cli.command("update")
@click.argument("name")
@click.argument("attributes", nargs=-1, metavar="<key=value>")
@click.pass_context
def update_config(ctx, name, attributes):
"""Update the ssh Host config Attribute key=value format"""
config = ctx.obj["config"]
if not config.exists(name):
click.secho(f"{name} does not exist, use `update` instead of `add`", fg="red")
raise SystemExit
host = config.get(name)
click.echo(host)
click.echo("=" * 25)
for attribute in attributes:
try:
key, value = attribute.split("=")
for attr, _ in Host.attrs:
if attr.lower() == key.lower():
config.update(name, {attr: value})
break
else:
raise Exception(f"No exists Attribute: {key}")
except Exception as e:
click.secho(f"Wrong format of attribute, {e}", fg="red")
raise SystemExit
click.echo(host)
write_config(config, "Information is correct ?", "Updated!")
@cli.command("rename")
@click.argument("name")
@click.argument("new_name")
@click.pass_context
def rename_config(ctx, name, new_name):
config = ctx.obj["config"]
if not config.exists(name):
click.secho(f"{name} does not exist", fg="red")
raise SystemExit
config.get(name).set_name(new_name)
click.echo(config.get(new_name))
write_config(config, "Information is correct ?", "Renamed!")
@cli.command("remove")
@click.argument("name")
@click.pass_context
def remove_config(ctx, name):
config = ctx.obj["config"]
if not config.exists(name):
click.secho(f"{name} does not exist", fg="red")
raise SystemExit
click.echo(config.get(name))
config.remove(name)
write_config(config, "Do you want to remove ?", "Removed!")
@cli.command("inventory")
@click.option("--list", "-l", "list_", is_flag=True)
@click.option("--host")
@click.pass_context
def inventory(ctx, list_, host):
"""Ansible inventory plugin"""
config = ctx.obj["config"]
if list_:
inventory_data = {
"_meta": {"hostvars": {}},
"all": {"children": ["ungrouped"]},
"ungrouped": {"hosts": []},
}
for host in config:
hostvars = {
"ansible_host": host.HostName,
"ansible_port": host.Port or 22,
"ansible_ssh_user": host.User or os.getenv("USER"),
}
if host.IdentityFile:
hostvars["ansible_ssh_private_key_file"] = host.IdentityFile
inventory_data["_meta"]["hostvars"][host.name] = hostvars
inventory_data["ungrouped"]["hosts"].append(host.name)
click.echo(json.dumps(inventory_data, indent=2))
elif host:
host = config.get(host)
hostvars = {
"ansible_host": host.HostName,
"ansible_port": host.Port or 22,
"ansible_ssh_user": host.User or os.getenv("USER"),
}
click.echo(json.dumps(hostvars, indent=2))
def main():
"""ssh-config {version}
Usage:
ssh-config [options] <command> [<args>...]
Options:
-h --help Show this screen.
-v --version Show version.
-V --verbose Verbose output
-f --config FILE Specify an ssh client file [default: ~/.ssh/config]
Commands:
[x] gen Generate ssh config file
[x] ls Show list of Hosts in client file
[x] get Get ssh client config with Name
[x] add Add new Host configuration
[x] update Update host configuration
[x] rename Update host configuration
[x] rm Remove exist Host configuration
[] import Import Hosts from csv file to SSH Client config
[] export Export Hosts to csv format
[] bastion Bastion register/use
[] ping Send ping to selected host
[-] version Show version information
"""
try:
cli()
except SystemExit as e:
if e.code != 0:
raise
if __name__ == "__main__":
main()