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¶
Connect with a password
In [ ]:
Copied!
# NOTE!: using `open` will cause collisions. It **must** be aliased.
import os
from fsspec import open as fs_open
from sshfs import SSHFileSystem
fs = SSHFileSystem(
os.environ["SSH_HOST"],
username=os.environ["SSH_USER"],
password=os.environ["SSH_PASSWORD"]
)
details = fs.info('/tmp')
# NOTE!: using `open` will cause collisions. It **must** be aliased.
import os
from fsspec import open as fs_open
from 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¶
Connect with a private key
In [ ]:
Copied!
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"]]
)
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¶
List contents in a remote directory via ssh
In [ ]:
Copied!
fs.ls(f"/home/{ssh_user}")
fs.ls(f"/home/{ssh_user}")
Example 4¶
Download the contents of a remote directory to local via ssh
In [ ]:
Copied!
from pathlib import Path
Path("/tmp/ganymede_demo").mkdir(parents=True, exist_ok=True)
# Change rpath to a file that exists on your remote host
fs.get(rpath=f"/home/{ssh_user}/example.jpeg", lpath="/tmp/ganymede_demo/example.jpeg")
from pathlib import Path
Path("/tmp/ganymede_demo").mkdir(parents=True, exist_ok=True)
# Change rpath to a file that exists on your remote host
fs.get(rpath=f"/home/{ssh_user}/example.jpeg", lpath="/tmp/ganymede_demo/example.jpeg")