Last Updated: February 25, 2016
·
4.685K
· emre

automate SSH configs with python

I use sshconfigs a lot. It's always good to connect a custom configured ssh connection with just "ssh host_name".

recently, I released a command line utility to handle ssh configs. It does not bring magical stuff compared to vim ~/.ssh/config. But I just like this way more, and commands like list/search would be pretty useful if you have a lot of servers in your ssh config.

after the release, a lot of people asked to use it as a library instead of a command-line tool. So, here it is, a little documentation about how to use storm as a python library (luckily, it was decoupled enough to use it like this.)

installation


pip install stormssh
</code></pre>
initialization:


from storm import ssh_config

sconfig = ssh_config.ConfigParser("/home/emrebey/.ssh/config")
sconfig.load()

</code></pre>

get the raw config

output:


[{
        'host': 'vps',
        'type': 'entry',
        'options': {
            'hostname': 'emreyilmaz.me',
            'user': 'root',
            'port': '22'
        },
        'order': 1
    }, {
        'host': 'netscaler',
        'type': 'entry',
        'options': {
            'hostname': 'netscaler.com',
            'user': 'root',
            'port': '22'
        },
        'order': 2
    }
]

</code></pre>

add a host to config

sconfig.add_host('name', {
    'user': 'user',
    'hostname': 'hostname.com',
    'port': 2400,
})

sconfig.writetossh_config() # save it to the config
</code></pre>

modify a host


sconfig.update_host('name', {
    'user': 'root',
    'hostname': 'root.com',
    'port': 22,
})

</code></pre>

deleting a single host by name

from storm.exceptions import StormValueError
try:
    sconfig.delete_host('name')
except StormValueError:
    pass
</code></pre>

delete all hosts

sconfig.deleteallhosts()
</code></pre>

searching hosts

print sconfig.search_host('vps')
</code></pre>

output: 

[{
        'host': 'vps',
        'type': 'entry',
        'options': {
            'hostname': 'emreyilmaz.me',
            'user': 'emre',
            'port': '22'
        },
        'order': 1
    }, {
        'host': 'vps_backup',
        'type': 'entry',
        'options': {
            'hostname': 'emreyilmaz.me',
            'user': 'root',
            'port': '24'
        },
        'order': 2
    }
]
</code></pre>

browse ssh_config.py for more.

3 Responses
Add your response

See also github-keygen, a tool dedicated to build you the best settings for your Github connections: https://github.com/dolmen/github-keygen

over 1 year ago ·

@dolmen, looks nice.

over 1 year ago ·

https://github.com/emre/storm/issues/87#issuecomment-56963429

from storm.parsers.sshconfigparser import ConfigParser as StormParser

config = StormParser("/users/emre/.ssh/config")

config.load()
[{'host': '', 'type': 'comment', 'order': 1, 'value': '#Host *'}, ...]

config.search_host("blog")
[{'host': 'blog', 'type': 'entry', 'options': {'hostname': '178.62.233.223', 'user': 'root', 'port': '22'}, 'order': 18}]

over 1 year ago ·