-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh-config-tool.py
executable file
·61 lines (45 loc) · 1.46 KB
/
ssh-config-tool.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
#!/usr/bin/env python
import os
import re
import argparse
import sys
from ssh_config_tool import (
ssh_command_to_config_entry,
split_ssh_config,
backup_ssh_config,
parse_ssh_config,
)
def translate(args):
ssh_command = " ".join(args.ssh_args)
config_entry = ssh_command_to_config_entry(ssh_command)
ssh_config_d = os.path.expanduser("~/.ssh/config.d")
os.makedirs(ssh_config_d, exist_ok=True)
host = re.search(r"^Host\s+(\S+)", config_entry, re.MULTILINE).group(1)
host_file = os.path.join(ssh_config_d, f"{host}.conf")
with open(host_file, "w") as f:
f.write(config_entry)
print(f"Generated {host_file}:")
with open(host_file, "r") as f:
print(f.read())
def main():
parser = argparse.ArgumentParser(description="SSH config management tool")
subparsers = parser.add_subparsers()
split_parser = subparsers.add_parser(
"split", help="Split SSH config into separate files"
)
split_parser.set_defaults(func=split_ssh_config)
translate_parser = subparsers.add_parser(
"translate", help="Translate SSH command to SSH config entry"
)
translate_parser.add_argument(
"ssh_args", nargs=argparse.REMAINDER, help="SSH command arguments"
)
translate_parser.set_defaults(func=translate)
args = parser.parse_args()
if "func" in args:
args.func(args)
else:
parser.print_help()
sys.exit(1)
if __name__ == "__main__":
main()