-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathkl.py
More file actions
executable file
·51 lines (38 loc) · 1.49 KB
/
Copy pathkl.py
File metadata and controls
executable file
·51 lines (38 loc) · 1.49 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
#!/usr/bin/python3
import os
import re
GREEN = "\033[1;32;40m"
RESET = "\033[0m"
def parse_ssh_config():
ssh_config_path = os.path.expanduser('~/.ssh/config')
if not os.path.exists(ssh_config_path):
print("SSH config file not found!")
return
hosts_map = {} # {host: (hostname, aliases)}
current_hostname = None
with open(ssh_config_path, 'r') as f:
for line in f:
line = line.strip()
if not line or line.startswith('#'):
continue
parts = line.lower().split()
if len(parts) < 2:
continue
key, value = parts[0], parts[1]
if key.lower() == 'host':
hosts = line.split(None, 1)[1].split()
main_host = hosts[0]
aliases = hosts[1:] if len(hosts) > 1 else []
if main_host != '*':
hosts_map[main_host] = (None, aliases)
elif key.lower() == 'hostname':
hostname = value
for host, (_, aliases) in hosts_map.items():
if hosts_map[host][0] is None:
hosts_map[host] = (hostname, aliases)
for host, (hostname, aliases) in hosts_map.items():
if hostname and not host.startswith('#'):
alias_str = f" ( {' '.join(aliases)} )" if aliases else ""
print(f"# ssh {GREEN}{host}{RESET} ---> {GREEN}{hostname}{RESET}{alias_str}")
if __name__ == "__main__":
parse_ssh_config()