项目作者: ivuk

项目描述 :
JSON to ssh_config file generator
高级语言: Python
项目地址: git://github.com/ivuk/json-to-ssh_config.git
创建时间: 2014-10-14T18:14:17Z
项目社区:https://github.com/ivuk/json-to-ssh_config

开源协议:Apache License 2.0

下载


json-to-ssh_config

Code style: black

A simple JSON to ssh_config file generator. The basic usage model is that you
stick all the JSON files into a certain folder (the default value is
~/.ssh/confs/, overrideable via the -s parameter), which then in turn get
loaded/validated via Python’s json.load(), and translated to
ssh_config(5) file format.

There are some assumptions that the script makes:

  • All JSON files are in the same folder
  • All JSON files end with a .conf suffix
  • All JSON files get {displayed,written} alphabetically, except for global.conf,
    which is always last

There are two basic syntax models that are supported:

  1. {
  2. "Hosts":
  3. [{
  4. "Host": "aliasname",
  5. "HostName": "fqdnname",
  6. "Port": 12345
  7. }]
  8. }

Which results in:

  1. $ ./gensshconf.py -s .
  2. # Content from ./demo1.conf
  3. Host aliasname
  4. HostName fqdnname
  5. Port 12345

And:

  1. {
  2. "Options":
  3. {
  4. "IdentityFile": "pathtoidentityfile",
  5. "Port": 12345
  6. },
  7. "Hosts":
  8. [{
  9. "Host": "aliasname1",
  10. "HostName": "fqdnname1"
  11. },
  12. {
  13. "Host": "aliasname2",
  14. "HostName": "fqdnname2",
  15. "Port": 23456
  16. }]
  17. }

Which results in:

  1. $ ./gensshconf.py -s .
  2. # Content from ./demo2.conf
  3. Host aliasname1
  4. HostName fqdnname1
  5. IdentityFile pathtoidentityfile
  6. Port 12345
  7. Host aliasname2
  8. HostName fqdnname2
  9. Port 23456
  10. IdentityFile pathtoidentityfile

The global.conf file is a special case, its intended usage is to contain the
Host * settings that apply to all hosts:

  1. {
  2. "Hosts":
  3. [{
  4. "Host": "*",
  5. "ControlMaster": "auto",
  6. "ControlPath": "somepath",
  7. "ForwardAgent": "no",
  8. "IdentityFile": "somefile",
  9. "IdentitiesOnly": "yes",
  10. "Protocol": 2,
  11. "User": "username",
  12. "VisualHostKey": "yes"
  13. }]
  14. }

Which results in:

  1. $ ./gensshconf.py -s .
  2. # Content from ./global.conf
  3. Host *
  4. ControlMaster auto
  5. ControlPath somepath
  6. ForwardAgent no
  7. IdentityFile somefile
  8. IdentitiesOnly yes
  9. Protocol 2
  10. User username
  11. VisualHostKey yes

Note that there are some aspects of ssh_config(5) which are not supported
directly, such as overriding a set of hosts via wildcards (*.example.com).
That could be hacked around via a combination of some smart file naming and
existing syntax.

There are two supported output modes, screen and file, controlled via the
-o parameter. The default value is screen. When used with the -o file
parameter, the default output file is outfile-example in the script
directory. This can be overridden via the -f parameter:

  1. $ ./gensshconf.py -o file -f ~/.ssh/config

This is still a fairly rudimentary implementation, but it seems to work
properly for me. YMMV disclaimer is implied, as always. :)