Package: nbrshell
nbrshellv1.0.7 Feb 2024Set of Jupyter Notebook "cell magic" functions to execute remote shell script commands typed in a notebook cell,
as if they were typed in a terminal. Shell output is streaming back to the notebook.This makes it possible to create a notebook with shell constructs in code cells, shell output in output cells, and freeform text in markdown cells,
making this a good solution for documentation purposes as well as a templating solution for repetitive executions.Each "cell magic" has a non-magic equivalent function with name ending with "_fn".Similarly, there is also Oracle-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.1Package structure :├── 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.

├── 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_sqlplus_env           └──> saves sqlplus environment parameters for use in subsequent executions.
    └── set_nbr_env               └──> saves nbr environment parameters for use in subsequent executions.Usage examples:To run shell commands on a remote server:First load remote execution function:importnbrshellasnbr# define jupyter python variable:jupyter_var="This is a string defined in Jupyter"Then execute shell script on a remote server:%%exec_shell_scriptuser@hostssh_psw='password'echo"Running ping :"echo"--------------"ping-swww.oracle.com563echo"Running loop :"echo"--------------"foriin12345;doecho$idoneecho"Here document :"echo"--------------"cat<<-EOFThis is multilinehere documentEOFecho"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 usingnbr.set_nbrshell_env()function, in which case it will not be necessary
to include them in subsequent cell magic commands, thus allowing cleaner notebook.To run Oracle sqlplus on a remote serverOne option is to give all connection parameters on cell command line:First load remote execution function:importnbrshellasnbrThen run remote sqlplus commands with full command line options:%%pbrun_sqlplususername@hostnamessh_psw='password1'oracle_sid='ORCL1'oracle_conn='/ as sysdba'selectsysdatefromdual;showusershowparameterssga_targetwhich produces below output cell:Another option is to set connection parameters once withnbr.set_nbrshell_env(), and then run remote sqlplus commands in multiple cells without command line parameters.
Password can be hidden withgetpassorstdiomaskmodule 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_sqlplusselectsysdatefromdual;showuser%%pbrun_sqlplusselect'aaa'fromv$instance;showparameterssga_target