persistent_ssh_agent package

Module contents

Persistent SSH Agent for managing SSH keys and connections.

class persistent_ssh_agent.PersistentSSHAgent(config: SSHConfig | None = None, expiration_time: int = 86400, reuse_agent: bool = True)[source]

Bases: object

Handles persistent SSH agent operations and authentication.

This class manages SSH agent persistence across sessions by saving and restoring agent information. It also handles SSH key management and authentication for various operations including Git.

extract_hostname(url: str) str | None[source]

Extract hostname from SSH URL (public method).

This is a public wrapper around the extract_hostname function in utils.

Parameters:

url – SSH URL to extract hostname from (e.g., git@github.com:user/repo.git)

Returns:

Hostname if valid URL, None otherwise

Return type:

str

run_git_command_passwordless(command: List[str], username: str | None = None, password: str | None = None, prefer_ssh: bool = True) object | None[source]

Run any Git command with automatic passwordless authentication.

This method intelligently chooses between SSH and credential helper authentication to execute Git commands without requiring manual password input.

Authentication Strategy: 1. If prefer_ssh=True (default):

  • Try SSH authentication first (if SSH is set up for the detected host)

  • Fall back to credential helper if SSH fails

  1. If prefer_ssh=False: - Try credential helper first (if credentials are available) - Fall back to SSH if credential helper fails

Parameters:
  • command – Git command as list (e.g., [‘git’, ‘clone’, ‘repo_url’])

  • username – Git username (optional, uses GIT_USERNAME env var if not provided)

  • password – Git password/token (optional, uses GIT_PASSWORD env var if not provided)

  • prefer_ssh – Whether to prefer SSH over credential helper (default: True)

Returns:

Command result object or None if failed

Example

>>> agent = PersistentSSHAgent()
>>> # Clone with SSH (if available) or credentials
>>> result = agent.run_git_command_passwordless(['git', 'clone', 'git@github.com:user/repo.git'])
>>> # Force credential helper first
>>> result = agent.run_git_command_passwordless(
...     ['git', 'pull'], username='user', password='token', prefer_ssh=False
... )
setup_ssh(hostname: str) bool[source]

Set up SSH authentication for a host.

Parameters:

hostname – Hostname to set up SSH for

Returns:

True if setup successful

Return type:

bool

class persistent_ssh_agent.SSHAgentConstants[source]

Bases: object

Constants for SSH agent operations.

AGENT_INFO_FILE: ClassVar[str] = 'agent_info.json'
DEFAULT_EXPIRATION_TIME: ClassVar[int] = 86400
SSH_AGENT_PID_VAR: ClassVar[str] = 'SSH_AGENT_PID'
SSH_AUTH_SOCK_VAR: ClassVar[str] = 'SSH_AUTH_SOCK'
SSH_CONFIG_FILE: ClassVar[str] = 'config'
SSH_DEFAULT_KEY: ClassVar[str] = 'id_rsa'
SSH_DEFAULT_OPTIONS: ClassVar[List[str]] = ['-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'LogLevel=ERROR']
SSH_DIR_NAME: ClassVar[str] = '.ssh'
SSH_KEY_TYPES: ClassVar[List[str]] = ['id_ed25519', 'id_ecdsa', 'id_ecdsa_sk', 'id_ed25519_sk', 'id_rsa', 'id_dsa']
class persistent_ssh_agent.SSHConfig(identity_file: str | None = None, identity_content: str | None = None, identity_passphrase: str | None = None, ssh_options: Dict[str, str] = None)[source]

Bases: object

SSH configuration class.

identity_content: str | None = None
identity_file: str | None = None
identity_passphrase: str | None = None
ssh_options: Dict[str, str] = None

Submodules