added additional functions
This commit is contained in:
parent
1e55abf811
commit
67a5534246
8 changed files with 654 additions and 107 deletions
|
@ -8,6 +8,10 @@ from tabulate import tabulate
|
|||
from collections import OrderedDict
|
||||
|
||||
from .utils import print_warning, print_error, Colors
|
||||
"""
|
||||
This file is responsible for listing all SSH hosts discovered in ~/.ssh/conf/<label>/config.
|
||||
We've modified it to show a "Conf Directory" column instead of "IdentityFile."
|
||||
"""
|
||||
|
||||
async def check_ssh_port(ip_address, port):
|
||||
"""
|
||||
|
@ -63,8 +67,14 @@ def load_config_file(file_path):
|
|||
|
||||
async def check_host(host):
|
||||
"""
|
||||
Given a host block, resolve IP, check SSH port, identity file existence, etc.
|
||||
Return a row for tabulate.
|
||||
Given a host block, resolve IP, check SSH port, etc.
|
||||
Returns a row for tabulate containing:
|
||||
1) Host label
|
||||
2) User
|
||||
3) Port (color-coded if open)
|
||||
4) HostName
|
||||
5) IP Address (color-coded if resolved)
|
||||
6) Conf Directory (green if has IdentityFile, else no color)
|
||||
"""
|
||||
host_label = host.get('Host', 'N/A')
|
||||
hostname = host.get('HostName', 'N/A')
|
||||
|
@ -72,19 +82,6 @@ async def check_host(host):
|
|||
port = int(host.get('Port', '22'))
|
||||
identity_file = host.get('IdentityFile', 'N/A')
|
||||
|
||||
# Identity file check
|
||||
if identity_file != 'N/A':
|
||||
expanded_identity = os.path.expanduser(identity_file)
|
||||
identity_exists = os.path.isfile(expanded_identity)
|
||||
else:
|
||||
identity_exists = False
|
||||
|
||||
identity_display = (
|
||||
f"{Colors.GREEN}{identity_file}{Colors.RESET}"
|
||||
if identity_exists
|
||||
else f"{Colors.RED}{identity_file}{Colors.RESET}"
|
||||
)
|
||||
|
||||
# Resolve IP
|
||||
try:
|
||||
ip_address = socket.gethostbyname(hostname)
|
||||
|
@ -93,7 +90,7 @@ async def check_host(host):
|
|||
ip_address = None
|
||||
colored_ip = f"{Colors.RED}N/A{Colors.RESET}"
|
||||
|
||||
# Check if port is open
|
||||
# Check port
|
||||
if ip_address:
|
||||
port_open = await check_ssh_port(ip_address, port)
|
||||
colored_port = (
|
||||
|
@ -104,19 +101,27 @@ async def check_host(host):
|
|||
else:
|
||||
colored_port = f"{Colors.RED}{port}{Colors.RESET}"
|
||||
|
||||
# Conf Directory = ~/.ssh/conf/<host_label>
|
||||
conf_path = f"~/.ssh/conf/{host_label}"
|
||||
# If there's an IdentityFile, we color this path green
|
||||
if identity_file != 'N/A':
|
||||
conf_path_display = f"{Colors.GREEN}{conf_path}{Colors.RESET}"
|
||||
else:
|
||||
conf_path_display = conf_path
|
||||
|
||||
return [
|
||||
host_label,
|
||||
user,
|
||||
colored_port,
|
||||
hostname,
|
||||
colored_ip,
|
||||
identity_display
|
||||
conf_path_display
|
||||
]
|
||||
|
||||
async def list_hosts(conf_dir):
|
||||
"""
|
||||
List out all hosts found in ~/.ssh/conf/*/config, showing connectivity details.
|
||||
If no hosts are found, print an empty table or a warning message.
|
||||
List out all hosts found in ~/.ssh/conf/*/config.
|
||||
Shows columns: No., Host, User, Port, HostName, IP Address, Conf Directory
|
||||
"""
|
||||
pattern = os.path.join(conf_dir, "*", "config")
|
||||
conf_files = sorted(glob.glob(pattern))
|
||||
|
@ -126,7 +131,8 @@ async def list_hosts(conf_dir):
|
|||
blocks = load_config_file(conf_file)
|
||||
all_host_blocks.extend(blocks)
|
||||
|
||||
headers = ["No.", "Host", "User", "Port", "HostName", "IP Address", "IdentityFile"]
|
||||
# Prepare table
|
||||
headers = ["No.", "Host", "User", "Port", "HostName", "IP Address", "Conf Directory"]
|
||||
if not all_host_blocks:
|
||||
print_warning("No hosts found. The server list is empty.")
|
||||
print("\nSSH Conf Subdirectory Host List")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue