Registry / devops / sshconf

sshconf

JSON →
library0.2.7pypypi✓ verified 83d ago

sshconf is a Python library designed for reading and modifying your `~/.ssh/config` file in a non-intrusive way. It aims to keep the file's structure largely intact after modifications, providing a simple interface to manage SSH client configurations. The current version is 0.2.7. It has an irregular release cadence, with the last release in June 2024.

pip install sshconf
INSTALL
IMPORT
SIG · SSHCONF
S
sshconf
devopspythonv0.2.7
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

read_ssh_config
from sshconf import read_ssh_config
import sshconf

This quickstart demonstrates how to create a new SSH configuration, add and modify host entries, and then write the configuration to a file. It uses `sshconf.empty()` to start a new configuration and `write()` to save it, avoiding modification of your actual `~/.ssh/config` file for safety. Remember that `save()` will overwrite the file it was read from.

import sshconf import os # Create a temporary config file for demonstration temp_config_path = os.path.expanduser('~/.ssh/config_temp') # Ensure the .ssh directory exists os.makedirs(os.path.dirname(temp_config_path), exist_ok=True) # Start with an empty configuration config = sshconf.empty() # Add a new host entry config.add( 'devserver', Hostname='192.168.1.100', User='devuser', IdentityFile=os.path.expanduser('~/.ssh/id_rsa_dev') ) # Update an existing entry or add a new parameter config.set( 'devserver', Port=2222, ForwardAgent='yes' ) # Add another host with minimal options config.add( 'testserver', Hostname='test.example.com', User='testuser' ) # Remove a parameter from a host config.unset('devserver', 'ForwardAgent') # Get configuration for a host host_config = config.host('devserver') print(f"Devserver config: {host_config.to_dict()}") # Write the changes to a new file (or save() to overwrite original) config.write(temp_config_path) print(f"SSH config written to: {temp_config_path}") print("Contents of the temporary config file:") with open(temp_config_path, 'r') as f: print(f.read()) # Clean up the temporary file (optional) # os.remove(temp_config_path)
Debug
Known issues
gotchaThe `save()` method overwrites the SSH configuration file from which the `SshConfig` object was initially loaded. Use `write(filepath)` to save changes to a different file, or carefully use `save()` if overwriting is intended.
fix
Always use `config.write('/path/to/new_config')` unless you explicitly want to overwrite the source file. Consider backing up your `~/.ssh/config` before using `save()`.
affects: All versions
gotchaWhen `sshconf` reads a configuration file containing `Include` directives and then writes or saves it, the included files' contents are merged into the main output file. This flattens the configuration and removes the original `Include` structure.
fix
Be aware that `Include` directives will be expanded. If you rely on separate include files for modularity, you may need to manage them manually or use `sshconf` only for specific sections.
affects: All versions
gotchaOpenSSH clients process configuration options based on the first matching `Host` entry. If you have overlapping patterns (e.g., `Host *.example.com` and `Host specific.example.com`), the order matters. Less specific patterns appearing before more specific ones can lead to unexpected configurations being applied.
fix
Arrange `Host` blocks in `~/.ssh/config` from most specific to least specific. For example, place `Host *` as the very last entry to ensure it only applies if no other host matches.
affects: All versions (OpenSSH client behavior)
gotchaSSH client configuration files (`~/.ssh/config`) and private key files (`~/.ssh/id_rsa`) require strict file permissions. Incorrect permissions (e.g., world-readable) will cause the SSH client to ignore them for security reasons, leading to authentication failures or ignored configurations.
fix
Ensure `~/.ssh` directory has `chmod 700`, `~/.ssh/config` and private key files have `chmod 600`. Use `chmod 700 ~/.ssh` and `chmod 600 ~/.ssh/config` (and for private keys).
affects: All versions (OpenSSH client behavior)
Upgrade
Version history
0.2.7latest on PyPI · released Jun 29, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
8
Resources
sshconf — pip install sshconf · libregistry