0

I use ~/.ssh/config to manage hosts that I need to interact with frequently. 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

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.

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. Otherwise just defaulting to "~/.ssh/config" would do.

Daniel Mahler
  • 7,653
  • 5
  • 51
  • 90
  • The Python standard libraries don't include anything for this, so you'll have to use Paramiko or something similar. – muru Aug 08 '18 at 04:41

2 Answers2

1

It is possible to specify an alternative configuration file on the command line, but ~/.ssh/config is always the default. For an alternative configuration file use the -F configfile option.

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.

RalfFriedl
  • 1,134
  • 3
  • 11
  • 12
1

Using Paramiko

Paramiko is a pure-Python (3.6+) implementation of the SSHv2 protocol, providing both client and server functionality.

You may not need every feature Paramiko is providing, but based on their documentation, this module could do just what you need: paramiko.config.SSHConfig

Representation of config information as stored in the format used by OpenSSH. Queries can be made via lookup. The format is described in OpenSSH’s ssh_config man page. This class is provided primarily as a convenience to posix users (since the OpenSSH format is a de-facto standard on posix) but should work fine on Windows too.

You can define a non-standard config-file,the same way you would specify an alternate ssh-config file to ssh command line, as mentioned in a previous answer:

config = SSHConfig.from_file(open("some-path.config"))
# Or more directly:
config = SSHConfig.from_path("some-path.config")
# Or if you have arbitrary ssh_config text from some other source:
config = SSHConfig.from_text("Host foo\n\tUser bar")

You can then retrieve whichever configuration you need like so

For example with a configuration file like this:

Host foo.example.com
    PasswordAuthentication no
    Compression yes
    ServerAliveInterval 60

You could access different settings using:

my_config = SSHConfig()
my_config.parse(open('~/.ssh/config'))
conf = my_config.lookup('foo.example.com')

assert conf['passwordauthentication'] == 'no'
assert conf.as_bool('passwordauthentication') is False
assert conf['compression'] == 'yes'
assert conf.as_bool('compression') is True
assert conf['serveraliveinterval'] == '60'
assert conf.as_int('serveraliveinterval') == 60

In case you only need available hostnames, you can list all available ones, and then get information about their configuration using that context (More info here)

get_hostnames()

Return the set of literal hostnames defined in the SSH config (both explicit hostnames and wildcard entries).

This might not be compliant with your request for a standard library, though the project seems to be actively maintained and widely used as well

Bleacks
  • 31
  • 4
  • I think Paramiko have been available long time ago, before 3.6 (i think even 2.5) or is it a different beast? – gildux Feb 03 '23 at 02:03
  • Yes indeed, it's worth mentioning that based on their changelog: https://www.paramiko.org/changelog.html it seems to have been released first in Q4 2013 based on Python 2.5 – Bleacks Feb 03 '23 at 09:39
  • Thanks, I wasn't sure when I read "pure-Python (3.6+)" in your answer. I may have checked by myself but tired and going bed when I wrote. However I have a good memory (I encounter this part of soft when playing with Ansible years ago.) – gildux Feb 03 '23 at 10:47
  • For completeness, you may add an example of host name retrieval with the `config` variable. ;) – gildux Feb 03 '23 at 10:50
  • @gildux I should have used a quote, as this is the way Paramiko presents itself on their github – Bleacks Feb 03 '23 at 11:22
  • @gildux I did update, thanks for the feedback, appreciated – Bleacks Feb 03 '23 at 11:29
  • Great! I wish I can upvote many times… Many thanks for the answer: it may come late but it's always relevant, so may serve future users that will come here. – gildux Feb 03 '23 at 11:43