fs-sshfs: PyFilesystem2 over SSH

1.0.2 · active · verified Fri Apr 17

fs-sshfs is a PyFilesystem2 extension that allows you to treat a remote SSH server's filesystem as a local `fs.FS` object, leveraging Paramiko for SSH connectivity. The current version is 1.0.2, and it receives updates as needed, particularly for compatibility with its core `paramiko` dependency, ensuring robust and secure remote file access.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a remote SSH server using `fs-sshfs` and list the contents of its root directory. It uses environment variables for secure credential handling and includes `paramiko.AutoAddPolicy` for simplified host key management during development. For production, a more secure host key policy is recommended.

import os
from fs_sshfs import SSHFS
from paramiko import AutoAddPolicy

# Set these environment variables or replace with actual values
# For password authentication: export SSH_HOST='your_host' SSH_USER='your_user' SSH_PASSWORD='your_password'
# For key-based authentication: export SSH_HOST='your_host' SSH_USER='your_user' SSH_PKEY_PATH='/path/to/your/key'

host = os.environ.get('SSH_HOST', '')
user = os.environ.get('SSH_USER', '')
password = os.environ.get('SSH_PASSWORD')
pkey_path = os.environ.get('SSH_PKEY_PATH')
port = int(os.environ.get('SSH_PORT', 22))

if not host or not user:
    print("Please set SSH_HOST and SSH_USER environment variables.")
    exit(1)

# Configure SSH key if path is provided
pkey = None
if pkey_path:
    try:
        from paramiko import RSAKey, Ed25519Key, ECDSAKey, DSSKey
        # Attempt to load common key types; more robust error handling might be needed
        try: pkey = RSAKey.from_private_key_file(pkey_path) 
        except Exception: pass
        try: pkey = Ed25519Key.from_private_key_file(pkey_path) 
        except Exception: pass
        try: pkey = ECDSAKey.from_private_key_file(pkey_path) 
        except Exception: pass
        try: pkey = DSSKey.from_private_key_file(pkey_path) 
        except Exception: pass

        if not pkey: raise ValueError("Could not load PKEY from path")
        print(f"Using SSH key from: {pkey_path}")

    except Exception as e:
        print(f"Warning: Could not load SSH key from {pkey_path}: {e}")
        print("Falling back to password or no authentication if key fails.")
        pkey = None


try:
    with SSHFS(
        host=host,
        user=user,
        passwd=password,
        port=port,
        pkey=pkey,
        # AutoAddPolicy is convenient for development but insecure for production
        missing_host_key_policy=AutoAddPolicy(),
    ) as remote_fs:
        print(f"Successfully connected to {host} as {user}")
        print("Listing root directory of the remote filesystem:")
        
        # Check if root is accessible and list contents
        if remote_fs.exists('/'):
            for info in remote_fs.scandir('/'):
                print(f"  {info.name} ({'directory' if info.is_dir else 'file'})")
        else:
            print("  Root directory '/' does not exist or is inaccessible.")

except Exception as e:
    print(f"Error connecting or listing remote files: {e}")
    print("Please ensure SSH_HOST, SSH_USER, and either SSH_PASSWORD or SSH_PKEY_PATH are set correctly.")
    print("Also, verify the SSH server is running and accessible.")

view raw JSON →