GitHub - mwilliamson/spur.py: Run commands and manipulate files locally or over SSH using the same interface (original) (raw)

spur.py: Run commands and manipulate files locally or over SSH using the same interface

To run echo locally:

import spur

shell = spur.LocalShell() result = shell.run(["echo", "-n", "hello"]) print(result.output) # prints hello

Executing the same command over SSH uses the same interface -- the only difference is how the shell is created:

import spur

shell = spur.SshShell(hostname="localhost", username="bob", password="password1") with shell: result = shell.run(["echo", "-n", "hello"]) print(result.output) # prints hello

Installation

$ pip install spur

Shell constructors

LocalShell

Takes no arguments:

SshShell

Requires a hostname. Also requires some combination of a username, password and private key, as necessary to authenticate:

Use a password

spur.SshShell( hostname="localhost", username="bob", password="password1" )

Use a private key

spur.SshShell( hostname="localhost", username="bob", private_key_file="path/to/private.key" )

Use a port other than 22

spur.SshShell( hostname="localhost", port=50022, username="bob", password="password1" )

Optional arguments:

Shell interface

run(command, cwd, update_env, store_pid, allow_error, stdout, stderr, encoding)

Run a command and wait for it to complete. The command is expected to be a list of strings. Returns an instance of ExecutionResult.

result = shell.run(["echo", "-n", "hello"]) print(result.output) # prints hello

Note that arguments are passed without any shell expansion. For instance, shell.run(["echo", "$PATH"]) will print the literal string$PATH rather than the value of the environment variable $PATH.

Raises spur.NoSuchCommandError if trying to execute a non-existent command.

Raises spur.CouldNotChangeDirectoryError if changing the current directory to cwd failed.

Optional arguments:

shell.run(*args, **kwargs) should behave similarly toshell.spawn(*args, **kwargs).wait_for_result()

spawn(command, cwd, update_env, store_pid, allow_error, stdout, stderr, encoding)

Behaves the same as run except that spawn immediately returns an object representing the running process.

Raises spur.NoSuchCommandError if trying to execute a non-existent command.

Raises spur.CouldNotChangeDirectoryError if changing the current directory to cwd failed.

open(path, mode="r")

Open the file at path. Returns a file-like object.

By default, files are opened in text mode. Appending "b" to the mode will open the file in binary mode.

For instance, to copy a binary file over SSH, assuming you already have an instance of SshShell:

with ssh_shell.open("/path/to/remote", "rb") as remote_file: with open("/path/to/local", "wb") as local_file: shutil.copyfileobj(remote_file, local_file)

close()

Closes and the shell and releases any associated resources.close() is called automatically when the shell is used as a context manager.

Process interface

Returned by calls to shell.spawn. Has the following attributes:

Has the following methods:

Classes

ExecutionResult

ExecutionResult has the following properties:

It also has the following methods:

result = shell.run(["some-command"], allow_error=True) if result.return_code > 4: raise result.to_error()

RunProcessError

A subclass of RuntimeError with the same properties asExecutionResult:

NoSuchCommandError

NoSuchCommandError has the following properties:

CouldNotChangeDirectoryError

CouldNotChangeDirectoryError has the following properties:

API stability

Using the the terminology from Semantic Versioning, if the version of spur is X.Y.Z, then X is the major version, Y is the minor version, and Z is the patch version.

While the major version is 0, incrementing the patch version indicates a backwards compatible change. For instance, if you're using 0.3.1, then it should be safe to upgrade to 0.3.2.

Incrementing the minor version indicates a change in the API. This means that any code using previous minor versions of spur may need updating before it can use the current minor version.

Undocumented features

Some features are undocumented, and should be considered experimental. Use them at your own risk. They may not behave correctly, and their behaviour and interface may change at any time.

Troubleshooting

I get the error "Connection refused" when trying to connect to a virtual machine using a forwarded port on localhost

Try using "127.0.0.1" instead of "localhost" as the hostname.

I get the error "Connection refused" when trying to execute commands over SSH

Try connecting to the machine using SSH on the command line with the same settings. For instance, if you're using the code:

shell = spur.SshShell( hostname="remote", port=2222, username="bob", private_key_file="/home/bob/.ssh/id_rsa" ) with shell: result = shell.run(["echo", "hello"])

Try running:

ssh bob@remote -p 2222 -i /home/bob/.ssh/id_rsa

If the ssh command succeeds, make sure that the arguments tossh.SshShell and the ssh command are the same. If any of the arguments to ssh.SshShell are dynamically generated, try hard-coding them to make sure they're set to the values you expect.

I can't spawn or run commands over SSH

If you're having trouble spawning or running commands over SSH, try passingshell_type=spur.ssh.ShellTypes.minimal as an argument to spur.SshShell. For instance:

import spur import spur.ssh

spur.SshShell( hostname="localhost", username="bob", password="password1", shell_type=spur.ssh.ShellTypes.minimal, )

This makes minimal assumptions about the features that the host shell supports, and is especially well-suited to minimal shells found on embedded systems. If the host shell is more fully-featured but only works withspur.ssh.ShellTypes.minimal, feel free to submit an issue.

Why don't shell features such as variables and redirection work?

Commands are run directly rather than through a shell. If you want to use any shell features such as variables and redirection, then you'll need to run those commands within an appropriate shell. For instance:

shell.run(["sh", "-c", "echo $PATH"]) shell.run(["sh", "-c", "ls | grep bananas"])