简体   繁体   English

从bash和python访问〜/ .ssh / config别名

[英]Accessing ~/.ssh/config aliases from bash and python

I use ~/.ssh/config to manage hosts that I need to interact with frequently. 我使用〜/ .ssh / config来管理需要频繁交互的主机。 Sometimes I would like to access the aliases from that file in scripts that do not use ssh directly, ie if I have a .ssh/config with 有时我想在不直接使用ssh的脚本中访问该文件的别名,即如果我使用.ssh / config

Host host1
  User user1
  Hostname server1.somewhere.net

I would like to be able to say something like sshcfg['host1'].Hostname to get server1.somewhere.net in scripting languages, particularly Python and something sensible in Bash. 我希望能够说出类似sshcfg['host1'].Hostname ,以便以脚本语言(尤其是Python)和Bash中的一些有意义的语言获取server1.somewhere.net

I would prefer to do this with standard tools/libraries if possible. 如果可能,我希望使用标准工具/库来执行此操作。 I would also prefer the tools to autodetect the the current configuration from the environment rather than have to be explicitly pointed at a configuration file. 我也希望使用这些工具从环境中自动检测当前配置,而不必将其明确指向配置文件。 I do not know if there is a way to have alternate configs in ssh but if there is I would like a way to autodetect the currently active one. 我不知道是否有一种方法可以在ssh中具有备用配置,但是如果我想自动检测当前处于活动状态的方法。 Otherwise just defaulting to "~/.ssh/config" would do. 否则,只需默认为“〜/ .ssh / config”即可。

It is possible to specify an alternative configuration file on the command line, but ~/.ssh/config is always the default. 可以在命令行上指定备用配置文件,但是〜/ .ssh / config始终是默认设置。 For an alternative configuration file use the -F configfile option. 对于备用配置文件,请使用-F configfile选项。

You can either try parsing the original config file, or have a file that is better suited for manipulation and generate an alternative configuration file or part of the default configuration file from that. 您可以尝试解析原始配置文件,或者拥有更适合于操作的文件,并从中生成替代配置文件或默认配置文件的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM