v1.0.13 Mar 2024
Set of Jupyter Notebook "cell magic" functions to execute remote shell script typed in a notebook cell, with shell script output streaming back to the notebook.
This makes it possible to write a notebook with freeform text in markdown cells and shell constructs in code cells, producing shell script output in output cells. This makes this a good solution for documentation purposes, prototyping, teaching, demoing and explaining working with unix shell, as well as templating solution for repetitive script executions.
Each "cell magic" has a non-magic equivalent function with name ending with "_fn".
Similarly, there is also Oracle database-specific "cell magic" allowing to run sqlplus commands in a remote sqlplus.
This package uses paramiko library, which is distributed under GNU Lesser General Public License v2.1
βββ pbrun_as_oracle βββ> connects via paramiko ssh client with a password (no prior ssh
βββ pbrun_as_oracle_fn β keys setup is needed), then executes pbrun to switch to oracle account,
β then sets oracle environment according to provided "oracle_sid"
β and runs provided shell commands as oracle user.
βββ pbrun_as βββ> connects via paramiko ssh client with password (no prior ssh
βββ pbrun_as_fn β keys setup is needed), then executes pbrun to switch to another user,
β provided as a parameter. Then runs provided shell commands.
βββ exec_shell_script βββ> connects using paramiko ssh client. If password is provided, then
βββ exec_shell_script_fn β connects with password and no prior ssh keys setup is needed.
β If password is not provided, then attempts to connect with ssh keys.
β Then runs provided shell commands.
βββ exec_shell_script_ssh βββ> connects using local ssh client with previously setup ssh keys.
βββ exec_shell_script_ssh_fn β Useful in cases when paramiko will not connect.
βββ pbrun_sqlplus βββ> runs cell content via sqlplus on a remote host, after connecting
β to the remote host with ssh, becoming oracle with pbrun and
β setting some common Oracle environment variables.
βββ nbrshell_common βββ> common functions and variables.
βββ set_psw βββ> sets password in memory for use in subsequent cell executions.
βββ set_nbrshell_env βββ> saves nbr environment parameters for use in subsequent executions.
-
First load remote execution package:
import nbrshell as nbr # define jupyter python variable: jupyter_var="This is a string defined in Jupyter"
Then execute shell script on a remote server:
%%exec_shell_script user@host ssh_psw='password' echo "Running ping :" echo "--------------" ping -s www.oracle.com 56 3 echo "Running loop :" echo "--------------" for i in 1 2 3 4 5; do echo $i done echo "Here document :" echo "--------------" cat <<-EOF This is multiline here document EOF echo "Jupyter variable substitution :" echo "---------------------------" echo {jupyter_var} echo "escaping curly braces :" echo "---------------------------" echo '\{Curly braces\} need to be escaped to prevent Jupyter variable substitution'
This will stream following shell output in Jupyter output cell :
The ssh connection parameters can also be set once using
nbr.set_nbrshell_env()
function, in which case it will not be necessary to include them in subsequent cell magic commands, thus allowing for less cluttered notebook. -
-
First load remote execution function:
import nbrshell as nbr
Then run remote sqlplus commands with full command line options:
%%pbrun_sqlplus username@hostname ssh_psw='password1' oracle_sid='ORCL1' oracle_conn='/ as sysdba' select sysdate from dual; show user show parameters sga_target
which produces below output cell:
-
and then run remote sqlplus commands in multiple cells without command line parameters. Password can be hidden with
getpass
orstdiomask
module if needed:# set nbr environment : nbr.set_nbrshell_env( ssh_conn='username@hostname', ssh_psw='password1', pbrun_user='oracle', oracle_sid='ORCL1', oracle_conn='/ as sysdba' )
%%pbrun_sqlplus select sysdate from dual; show user
%%pbrun_sqlplus select 'aaa' from v$instance; show parameters sga_target
-
From PyPi:
python -m pip install nbrshell
or from Github URL:
python -m pip install nbrshell@git+https://github.com/abalbekov/nbrshell