Command Module

Convenience functions to run a command, optionally streaming stdin and stderr. Return code, stdout, and stderr are returned.

If the shell is used, via executeCommand(shell=True), or by using runCommand(), it is the application’s responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection vulnerabilities.

executeCommand(command,
stdin=None,
streamOutput=False,
shell=False)
Runs a command, optionally streaming the output to the notebook cell.
Parameters:
  • command – The command to run. If it is a list, it should contain the executable followed by any parameters. Strings will be parsed into a list, unless shell is set. If shell is set, the command will be executed throught the shell (normally bash).
  • stdin – A file that will be used as stdin for the command. Necessary to set stdin when shell is False.
  • streamOutput – Streams output to the cell as the command is executing.
  • shell – If shell is True, the command string is passed to the shell.
Returns:

A tuple containing the (return_code, stdout, stderr). return_code is 0 when executing completed successfully. stdout and stderr are bytestrings.

>>> executeCommand(['date'])
(0, 'Wed Jul  5 10:54:06 EDT 2017\n', '')
>>> executeCommand(['date'], streamOutput=True)
Wed Jul  5 10:54:08 EDT 2017
(0, 'Wed Jul  5 10:54:08 EDT 2017\n', '')
>>> executeCommand(['date', '--utc'], streamOutput=True)
Wed Jul  5 14:54:23 UTC 2017
(0, 'Wed Jul  5 14:54:23 UTC 2017\n', '')

runCommand(command, stream=True)
Runs a shell command, optionally streaming the output to the notebook cell. Internally calls executeCommand() with shell set to True.
Parameters:
  • command – The command to run. The command will be executed throught the shell (normally bash).
  • stream – Streams output to the cell as the command is executing.
Returns:

A tuple containing the (return_code, stdout, stderr). return_code is 0 when executing completed successfully. stdout and stderr are bytestrings.

>>> runCommand('date')
Wed Jul  5 10:59:54 EDT 2017
(0, 'Wed Jul  5 10:59:54 EDT 2017\n', '')
>>> runCommand('date', stream=False)
(0, 'Wed Jul  5 11:00:00 EDT 2017\n', '')
>>> runCommand('date --utc', stream=False)
(0, 'Wed Jul  5 15:00:12 UTC 2017\n', '')