Description:
- Interact with data via ssh and python
Resources:
- [Pypi] https://pypi.org/project/sshfs/
- [GitHub] https://github.com/ronf/asyncssh
- [Fsspec Docs] https://filesystem-spec.readthedocs.io/en/latest/api.html#fsspec.spec.AbstractFileSystem
Environment variables
Section titled “Environment variables”This notebook reads its settings from environment variables. Set them before starting Jupyter:
SSH_HOST: hostname of your SSH serverSSH_PORT: optional, defaults to22SSH_USER: SSH user nameSSH_PASSWORD: password for that userSSH_PRIVATE_KEY_PATH: path to that user’s private key file
SSH_PASSWORD is used by Example 1 only; SSH_PORT and SSH_PRIVATE_KEY_PATH by Examples 2-4.
Example 1
Section titled “Example 1”Connect with a password
# NOTE!: using `open` will cause collisions. It **must** be aliased.import os
from fsspec import open as fs_openfrom sshfs import SSHFileSystem
fs = SSHFileSystem( os.environ["SSH_HOST"], username=os.environ["SSH_USER"], password=os.environ["SSH_PASSWORD"])
details = fs.info('/tmp')Example 2
Section titled “Example 2”Connect with a private key
import os
from sshfs import SSHFileSystem
ssh_user = os.environ["SSH_USER"]
fs = SSHFileSystem( host=os.environ["SSH_HOST"], port=int(os.environ.get("SSH_PORT", "22")), username=ssh_user, client_keys=[os.environ["SSH_PRIVATE_KEY_PATH"]])Example 3
Section titled “Example 3”List contents in a remote directory via ssh
fs.ls(f"/home/{ssh_user}")Example 4
Section titled “Example 4”Download the contents of a remote directory to local via ssh
from pathlib import Path
Path("/tmp/ganymede_demo").mkdir(parents=True, exist_ok=True)
# Change rpath to a file that exists on your remote hostfs.get(rpath=f"/home/{ssh_user}/example.jpeg", lpath="/tmp/ganymede_demo/example.jpeg")ganymede is made by Eugo