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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibcpy 3.10–3.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.fixAlways 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.fixBe 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.fixArrange `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.fixEnsure `~/.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.