sshc


Namesshc JSON
Version 2.0.0 PyPI version JSON
download
home_pagehttps://github.com/fahadahammed/sshc
SummarySSH Config and Ansible Inventory Generator.
upload_time2024-01-24 20:19:06
maintainer
docs_urlNone
authorfahadahammed
requires_python>=3.7,<4.0
licenseMIT
keywords packaging poetry
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sshc - SSH Configuration Management Tool with Ansible Inventory Generation
This tool can help you manage ssh config files with hosts as well as ansible inventory file.

## What it does?

1. It creates a host database.
2. Create SSH config from that host database.
3. Create Ansible inventory from that same host database.

### Example of generated SSH config
```ini
# Generated At: 2023-01-24 11:35:25.885044

# -- <
Host server1
HostName 192.168.0.100
Port 22
User ubuntu
IdentityFile /home/fahad/.ssh/id_rsa
LogLevel INFO
Compression yes
# Comment: Personal Server: ONE
# -- >

# -- <
Host server2
HostName 10.10.0.102
Port 4522
User root
IdentityFile /home/fahad/.ssh/id_rsa
LogLevel DEBUG
Compression no
# Comment: Personal Server: TWO
# -- >
```
### Example of generated Ansible Inventory
```json
{
    "all": {
        "hosts": {
            "server1": {
                "ansible_host": "192.168.0.100",
                "ansible_port": 22,
                "ansible_user": "ubuntu",
                "ansible_ssh_private_key_file": "/home/fahad/.ssh/id_rsa"
            },
            "server2": {
                "ansible_host": "10.10.0.102",
                "ansible_port": 4522,
                "ansible_user": "root",
                "ansible_ssh_private_key_file": "/home/fahad/.ssh/id_rsa"
            }
        },
        "children": {
            "personal": {
                "hosts": {
                    "server1": null,
                    "server2": null
                }
            },
            "home": {
                "hosts": {
                    "server1": null
                }
            },
            "storage": {
                "hosts": {
                    "server2": null
                }
            }
        }
    },
    "others": {
        "generated_at": "2023-01-24 11:35:25.885044"
    }
}
```

## Why?
### Problem it tried to solve
- Working with a bunch of servers gets messy to track those down.
- Managing Ansible Inventory and also SSH config file separate is redundant.

### Tried to solve via
- Using a JSON file as a common database of hosts.
- Setting name, ports, user, private key, ssh compression, ssh connection log level etc when inserting a host information.
- Set groups, do comment on specific host for host management.
- Well sorted config files.
- Ansible inventory is managed using JSON file.
- Add host to multiple groups which end up with ansible hosts group.
- Remove and update host entry easily.

## Description
### Structure

1. Insert host information to a JSON file as a DB.
2. Generate SSH Config file and an Ansible Inventory file.

### Technology Stack
1. python
2. json
3. yaml
4. openssh
5. ansible

### Dependency

#### Runtime
- Python3.7+
- Linux

#### Development
- Poetry

## Installation

```shell
% pip3 install sshc --upgrade
```

## Usage

### Step 1: Need the DB to be initiated for the first time
#### Pattern
```shell
usage: sshc init [-h] [--destination DESTINATION] [--dbfile DBFILE]

options:
  -h, --help            show this help message and exit
  --destination DESTINATION
                        Config HOME?
  --dbfile DBFILE       SSHC DB File.

```

#### Example
```shell
% sshc init
```

### Step 2: Insert host information to the Database
#### Pattern
```shell
usage: sshc insert [-h] --name NAME --host HOST [--user USER] [--port PORT] [--comment COMMENT] [--loglevel {INFO,DEBUG,ERROR,WARNING}] [--compression {yes,no}]
                   [--groups GROUPS [GROUPS ...]] [--identityfile IDENTITYFILE] [--destination DESTINATION] [--dbfile DBFILE]

options:
  -h, --help            show this help message and exit
  --name NAME           Server Name?
  --host HOST           SSH Host?
  --user USER           SSH User?
  --port PORT           SSH Port?
  --comment COMMENT     SSH Identity File.
  --loglevel {INFO,DEBUG,ERROR,WARNING}
                        SSH Log Level.
  --compression {yes,no}
                        SSH Connection Compression.
  --groups GROUPS [GROUPS ...]
                        Which group to include?
  --identityfile IDENTITYFILE
                        SSH Default Identity File Location. i.e. id_rsa
  --destination DESTINATION
                        Config HOME?
  --dbfile DBFILE       SSHC DB File.
```

#### Example
```shell
% sshc insert --name Google --host 8.8.8.8 --port 22 --user groot --identityfile /home/fahad/fahad.pem --comment "This is the server where you are not authorized to have access." --configfile /home/fahad/.ssh/config --groups google, fun
```

### Step 3: Generate ssh config and as well as ansible inventory file
#### Pattern
```shell
usage: sshc generate [-h] [--configfile CONFIGFILE] [--inventoryfile INVENTORYFILE] [--destination DESTINATION] [--dbfile DBFILE] [--filetype {json,yaml,yml}]

options:
  -h, --help            show this help message and exit
  --configfile CONFIGFILE
                        SSH Config File.
  --inventoryfile INVENTORYFILE
                        Ansible Inventory File.
  --destination DESTINATION
                        Config HOME?
  --dbfile DBFILE       SSHC DB File.
  --filetype {json,yaml,yml}
                        Preferred file type for Ansible inventory. Default is json and you can choose yaml too.
```

#### Example

```shell
% python3 sshc.py generate
```

This command will read all the entries in the DB and generate
1. SSH config file in your preferred directory or default one(i.e. $HOME/.ssh/sshc_ssh_config).
2. Ansible Inventory file will be created at your preferred directory or in default one (i.e. $HOME/.ssh/sshc_ansible_inventory.json).

If you stick with default directory you will find the generated files in:
1. Default Directory: `$HOME/.ssh`
2. Generated Ansible Inventory: `$HOME/.ssh/sshc_ansible_inventory.json`
3. Generated SSH Config: `$HOME/.ssh/sshc_ssh_config`

You can use these configs like below.

For SSH,
```shell
% ssh -F $HOME/.ssh/sshc_ssh_config
```

For Ansible,
```shell
% ansible -i $HOME/.ssh/sshc_ansible_inventory.json all --list-host
```

**Note: If you choose default SSH config file location and ansible host file location, sshc will replace the file. Be careful.**

#### Recommended Way of Generating Configurations
- There are two terms to keep in mind.
  - SSH default
  - sshc default
- Use sshc default paths which is different from SSH and Ansible default config.
- Use those newly created files(which should be separate than default one) either passing `-F` for SSH and `-i` for Ansible.

### Others
Help message of the tool
```shell
% sshc --help
```

```shell
usage: sshc [-h] [--version] {init,insert,delete,update,read,generate} ...

SSH Config and Ansible Inventory Generator !

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

subcommands:
  The main command of this CLI tool.

  {init,insert,delete,update,read,generate}
                        The main commands have their own arguments.
    init                Initiate Host DB !
    insert              Insert host information !
    delete              Delete host information !
    update              Update host information !
    read                Read Database !
    generate            Generate necessary config files !
```

### Delete Inserted Data

```shell
% sshc delete --hostname <HOSTNAME>
```

### Update Inserted Data

```shell
usage: sshc update [-h] --name NAME [--host HOST] [--user USER] [--port PORT] [--comment COMMENT]
                   [--loglevel {INFO,DEBUG,ERROR,WARNING}] [--compression {yes,no}] [--groups GROUPS [GROUPS ...]]
                   [--identityfile IDENTITYFILE] [--destination DESTINATION] [--dbfile DBFILE]

options:
  -h, --help            show this help message and exit
  --name NAME           Server Name?
  --host HOST           SSH Host?
  --user USER           SSH User?
  --port PORT           SSH Port?
  --comment COMMENT     SSH Identity File.
  --loglevel {INFO,DEBUG,ERROR,WARNING}
                        SSH Log Level.
  --compression {yes,no}
                        SSH Connection Compression.
  --groups GROUPS [GROUPS ...]
                        Which group to include?
  --identityfile IDENTITYFILE
                        SSH Default Identity File Location. i.e. id_rsa
  --destination DESTINATION
                        Config HOME?
  --dbfile DBFILE       SSHC DB File.
```

### Read DB Data

```shell
% sshc read
```

You can pass verbose too

```shell
% sshc read --verbose yes
```

## Known issues or Limitations

- Tested in Ubuntu 22.04
- Windows is not tested

## Getting help
If you have questions, concerns, bug reports and others, please file an issue in this repository's Issue Tracker.

## Getting involved
If you want to contribute to this tool, feel free to fork the repo and create Pull request with your changes.
Keep in mind to
- include better comment to understand.
- create PR to **development** branch.

---
## Author
- [Fahad Ahammed - DevOps Enthusiast - Dhaka, Bangladesh](https://github.com/fahadahammed)
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/fahadahammed/sshc",
    "name": "sshc",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7,<4.0",
    "maintainer_email": "",
    "keywords": "packaging,poetry",
    "author": "fahadahammed",
    "author_email": "iamfahadahammed@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/13/c0/41687e55e4d95309cad058214d308dfbe6a25dd87c6c00ed5436f94d1f64/sshc-2.0.0.tar.gz",
    "platform": null,
    "description": "# sshc - SSH Configuration Management Tool with Ansible Inventory Generation\nThis tool can help you manage ssh config files with hosts as well as ansible inventory file.\n\n## What it does?\n\n1. It creates a host database.\n2. Create SSH config from that host database.\n3. Create Ansible inventory from that same host database.\n\n### Example of generated SSH config\n```ini\n# Generated At: 2023-01-24 11:35:25.885044\n\n# -- <\nHost server1\nHostName 192.168.0.100\nPort 22\nUser ubuntu\nIdentityFile /home/fahad/.ssh/id_rsa\nLogLevel INFO\nCompression yes\n# Comment: Personal Server: ONE\n# -- >\n\n# -- <\nHost server2\nHostName 10.10.0.102\nPort 4522\nUser root\nIdentityFile /home/fahad/.ssh/id_rsa\nLogLevel DEBUG\nCompression no\n# Comment: Personal Server: TWO\n# -- >\n```\n### Example of generated Ansible Inventory\n```json\n{\n    \"all\": {\n        \"hosts\": {\n            \"server1\": {\n                \"ansible_host\": \"192.168.0.100\",\n                \"ansible_port\": 22,\n                \"ansible_user\": \"ubuntu\",\n                \"ansible_ssh_private_key_file\": \"/home/fahad/.ssh/id_rsa\"\n            },\n            \"server2\": {\n                \"ansible_host\": \"10.10.0.102\",\n                \"ansible_port\": 4522,\n                \"ansible_user\": \"root\",\n                \"ansible_ssh_private_key_file\": \"/home/fahad/.ssh/id_rsa\"\n            }\n        },\n        \"children\": {\n            \"personal\": {\n                \"hosts\": {\n                    \"server1\": null,\n                    \"server2\": null\n                }\n            },\n            \"home\": {\n                \"hosts\": {\n                    \"server1\": null\n                }\n            },\n            \"storage\": {\n                \"hosts\": {\n                    \"server2\": null\n                }\n            }\n        }\n    },\n    \"others\": {\n        \"generated_at\": \"2023-01-24 11:35:25.885044\"\n    }\n}\n```\n\n## Why?\n### Problem it tried to solve\n- Working with a bunch of servers gets messy to track those down.\n- Managing Ansible Inventory and also SSH config file separate is redundant.\n\n### Tried to solve via\n- Using a JSON file as a common database of hosts.\n- Setting name, ports, user, private key, ssh compression, ssh connection log level etc when inserting a host information.\n- Set groups, do comment on specific host for host management.\n- Well sorted config files.\n- Ansible inventory is managed using JSON file.\n- Add host to multiple groups which end up with ansible hosts group.\n- Remove and update host entry easily.\n\n## Description\n### Structure\n\n1. Insert host information to a JSON file as a DB.\n2. Generate SSH Config file and an Ansible Inventory file.\n\n### Technology Stack\n1. python\n2. json\n3. yaml\n4. openssh\n5. ansible\n\n### Dependency\n\n#### Runtime\n- Python3.7+\n- Linux\n\n#### Development\n- Poetry\n\n## Installation\n\n```shell\n% pip3 install sshc --upgrade\n```\n\n## Usage\n\n### Step 1: Need the DB to be initiated for the first time\n#### Pattern\n```shell\nusage: sshc init [-h] [--destination DESTINATION] [--dbfile DBFILE]\n\noptions:\n  -h, --help            show this help message and exit\n  --destination DESTINATION\n                        Config HOME?\n  --dbfile DBFILE       SSHC DB File.\n\n```\n\n#### Example\n```shell\n% sshc init\n```\n\n### Step 2: Insert host information to the Database\n#### Pattern\n```shell\nusage: sshc insert [-h] --name NAME --host HOST [--user USER] [--port PORT] [--comment COMMENT] [--loglevel {INFO,DEBUG,ERROR,WARNING}] [--compression {yes,no}]\n                   [--groups GROUPS [GROUPS ...]] [--identityfile IDENTITYFILE] [--destination DESTINATION] [--dbfile DBFILE]\n\noptions:\n  -h, --help            show this help message and exit\n  --name NAME           Server Name?\n  --host HOST           SSH Host?\n  --user USER           SSH User?\n  --port PORT           SSH Port?\n  --comment COMMENT     SSH Identity File.\n  --loglevel {INFO,DEBUG,ERROR,WARNING}\n                        SSH Log Level.\n  --compression {yes,no}\n                        SSH Connection Compression.\n  --groups GROUPS [GROUPS ...]\n                        Which group to include?\n  --identityfile IDENTITYFILE\n                        SSH Default Identity File Location. i.e. id_rsa\n  --destination DESTINATION\n                        Config HOME?\n  --dbfile DBFILE       SSHC DB File.\n```\n\n#### Example\n```shell\n% sshc insert --name Google --host 8.8.8.8 --port 22 --user groot --identityfile /home/fahad/fahad.pem --comment \"This is the server where you are not authorized to have access.\" --configfile /home/fahad/.ssh/config --groups google, fun\n```\n\n### Step 3: Generate ssh config and as well as ansible inventory file\n#### Pattern\n```shell\nusage: sshc generate [-h] [--configfile CONFIGFILE] [--inventoryfile INVENTORYFILE] [--destination DESTINATION] [--dbfile DBFILE] [--filetype {json,yaml,yml}]\n\noptions:\n  -h, --help            show this help message and exit\n  --configfile CONFIGFILE\n                        SSH Config File.\n  --inventoryfile INVENTORYFILE\n                        Ansible Inventory File.\n  --destination DESTINATION\n                        Config HOME?\n  --dbfile DBFILE       SSHC DB File.\n  --filetype {json,yaml,yml}\n                        Preferred file type for Ansible inventory. Default is json and you can choose yaml too.\n```\n\n#### Example\n\n```shell\n% python3 sshc.py generate\n```\n\nThis command will read all the entries in the DB and generate\n1. SSH config file in your preferred directory or default one(i.e. $HOME/.ssh/sshc_ssh_config).\n2. Ansible Inventory file will be created at your preferred directory or in default one (i.e. $HOME/.ssh/sshc_ansible_inventory.json).\n\nIf you stick with default directory you will find the generated files in:\n1. Default Directory: `$HOME/.ssh`\n2. Generated Ansible Inventory: `$HOME/.ssh/sshc_ansible_inventory.json`\n3. Generated SSH Config: `$HOME/.ssh/sshc_ssh_config`\n\nYou can use these configs like below.\n\nFor SSH,\n```shell\n% ssh -F $HOME/.ssh/sshc_ssh_config\n```\n\nFor Ansible,\n```shell\n% ansible -i $HOME/.ssh/sshc_ansible_inventory.json all --list-host\n```\n\n**Note: If you choose default SSH config file location and ansible host file location, sshc will replace the file. Be careful.**\n\n#### Recommended Way of Generating Configurations\n- There are two terms to keep in mind.\n  - SSH default\n  - sshc default\n- Use sshc default paths which is different from SSH and Ansible default config.\n- Use those newly created files(which should be separate than default one) either passing `-F` for SSH and `-i` for Ansible.\n\n### Others\nHelp message of the tool\n```shell\n% sshc --help\n```\n\n```shell\nusage: sshc [-h] [--version] {init,insert,delete,update,read,generate} ...\n\nSSH Config and Ansible Inventory Generator !\n\noptions:\n  -h, --help            show this help message and exit\n  --version             show program's version number and exit\n\nsubcommands:\n  The main command of this CLI tool.\n\n  {init,insert,delete,update,read,generate}\n                        The main commands have their own arguments.\n    init                Initiate Host DB !\n    insert              Insert host information !\n    delete              Delete host information !\n    update              Update host information !\n    read                Read Database !\n    generate            Generate necessary config files !\n```\n\n### Delete Inserted Data\n\n```shell\n% sshc delete --hostname <HOSTNAME>\n```\n\n### Update Inserted Data\n\n```shell\nusage: sshc update [-h] --name NAME [--host HOST] [--user USER] [--port PORT] [--comment COMMENT]\n                   [--loglevel {INFO,DEBUG,ERROR,WARNING}] [--compression {yes,no}] [--groups GROUPS [GROUPS ...]]\n                   [--identityfile IDENTITYFILE] [--destination DESTINATION] [--dbfile DBFILE]\n\noptions:\n  -h, --help            show this help message and exit\n  --name NAME           Server Name?\n  --host HOST           SSH Host?\n  --user USER           SSH User?\n  --port PORT           SSH Port?\n  --comment COMMENT     SSH Identity File.\n  --loglevel {INFO,DEBUG,ERROR,WARNING}\n                        SSH Log Level.\n  --compression {yes,no}\n                        SSH Connection Compression.\n  --groups GROUPS [GROUPS ...]\n                        Which group to include?\n  --identityfile IDENTITYFILE\n                        SSH Default Identity File Location. i.e. id_rsa\n  --destination DESTINATION\n                        Config HOME?\n  --dbfile DBFILE       SSHC DB File.\n```\n\n### Read DB Data\n\n```shell\n% sshc read\n```\n\nYou can pass verbose too\n\n```shell\n% sshc read --verbose yes\n```\n\n## Known issues or Limitations\n\n- Tested in Ubuntu 22.04\n- Windows is not tested\n\n## Getting help\nIf you have questions, concerns, bug reports and others, please file an issue in this repository's Issue Tracker.\n\n## Getting involved\nIf you want to contribute to this tool, feel free to fork the repo and create Pull request with your changes.\nKeep in mind to\n- include better comment to understand.\n- create PR to **development** branch.\n\n---\n## Author\n- [Fahad Ahammed - DevOps Enthusiast - Dhaka, Bangladesh](https://github.com/fahadahammed)",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "SSH Config and Ansible Inventory Generator.",
    "version": "2.0.0",
    "project_urls": {
        "Documentation": "https://github.com/fahadahammed/sshc",
        "Homepage": "https://github.com/fahadahammed/sshc",
        "Repository": "https://github.com/fahadahammed/sshc"
    },
    "split_keywords": [
        "packaging",
        "poetry"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "27bb0c75c5abc470a4dccdfb9c53bf13350b6062386f6708a31ac860c8f83912",
                "md5": "3e35c5c2c594223a70549834bbff6c3a",
                "sha256": "9890d60dff615f1e071d4ba52223ccd2cf852ac29dfd6dff9dd6740c1b08e579"
            },
            "downloads": -1,
            "filename": "sshc-2.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3e35c5c2c594223a70549834bbff6c3a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7,<4.0",
            "size": 9590,
            "upload_time": "2024-01-24T20:19:05",
            "upload_time_iso_8601": "2024-01-24T20:19:05.260202Z",
            "url": "https://files.pythonhosted.org/packages/27/bb/0c75c5abc470a4dccdfb9c53bf13350b6062386f6708a31ac860c8f83912/sshc-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "13c041687e55e4d95309cad058214d308dfbe6a25dd87c6c00ed5436f94d1f64",
                "md5": "3405d495223b38676e23fbc885048fe2",
                "sha256": "b60ca2c620f59d034b2223197ca29e5e716ee3620108c6e1958b862f1e71214c"
            },
            "downloads": -1,
            "filename": "sshc-2.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3405d495223b38676e23fbc885048fe2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7,<4.0",
            "size": 10357,
            "upload_time": "2024-01-24T20:19:06",
            "upload_time_iso_8601": "2024-01-24T20:19:06.949311Z",
            "url": "https://files.pythonhosted.org/packages/13/c0/41687e55e4d95309cad058214d308dfbe6a25dd87c6c00ed5436f94d1f64/sshc-2.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-24 20:19:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fahadahammed",
    "github_project": "sshc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sshc"
}
        
Elapsed time: 0.21872s