跳到内容
🛠️ToolsShed

SSH Config Generator

为多个主机生成~/.ssh/config条目,支持自定义设置。

主机条目 1
~/.ssh/config
Host myserver
  HostName 192.168.1.100
  User ubuntu
  IdentityFile ~/.ssh/id_rsa
  ServerAliveInterval 60

如何使用:

  1. 保存输出到 ~/.ssh/config
  2. 运行:chmod 600 ~/.ssh/config
  3. 连接方式:ssh <HostAlias>

关于此工具

SSH Config Generator 通过程序化生成 ~/.ssh/config 条目来简化管理多个 SSH 连接的过程。与其手动编辑 SSH 配置文件并记住每个服务器的主机别名、IP 地址和密钥路径相比,这个工具让你能够以结构化格式定义所有 SSH 主机,并在几秒钟内生成完整的配置文件。拥有组织良好的 SSH config 对于频繁连接到不同服务器的开发人员、系统管理员和 DevOps 工程师至关重要。

要使用此工具,请输入你的 SSH 主机及其相应的设置,例如主机名、端口、用户名、身份文件路径和任何其他 SSH 选项。生成器会根据 SSH config 语法自动格式化这些条目并显示输出,你可以将其直接复制到 ~/.ssh/config 文件中或添加到现有配置中。这种方法消除了语法错误,确保无论你管理多少台服务器,所有主机定义的格式都保持一致。

常见问题

代码实现

import paramiko

# SSH with config file support using Paramiko
config = paramiko.SSHConfig()
with open('/home/user/.ssh/config') as f:
    config.parse(f)

host_config = config.lookup('myserver')
print(host_config)
# {'hostname': '192.168.1.100', 'user': 'ubuntu', 'port': '22'}

# Connect using resolved config
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
    hostname=host_config['hostname'],
    username=host_config.get('user', 'root'),
    port=int(host_config.get('port', 22)),
    key_filename=host_config.get('identityfile', [None])[0]
)
stdin, stdout, stderr = client.exec_command('uptime')
print(stdout.read().decode())
client.close()

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.