'Bash script to send commands to remote ssh session

Is it possible to write a bash script that opens a remote node (i.e. through ssh and/or slurm) and starts an interactive session there after running some commands? I'm trying to automate the process of starting a jupyter session on a remote computing cluster, which currently looks like this:

  1. ssh into a login node of the remote cluster, using a specific port
  2. use slurm to request an interactive session on one of the compute nodes, including x11 forwarding through that port
  3. change directory to the working directory
  4. activate conda environment for my project
  5. open jupyter from the command line, specifying the port I used previously

It's a lengthy process, and if I get something wrong at any step I usually have to go back and start from the beginning because the port I'm using is still tied up. So I'm looking for a way I can run a single script (possibly with arguments) from my local machine that jumps through all the hoops to get me a working jupyter session with a link I can paste to my browser.



Solution 1:[1]

Like @Diego Torres Milano said, you would need to write a script locally that could do the interactive part, then invoke that via a remote script.

But since your process is interactive, this gets tricky. Luckily, linux has a tool which can easily be installed via a package manager called expect which has the ability to write logic to execute multi-step interactive scripts.

So you would write an expect script which would "expect" certain prompts, then it can read those prompts and use conditional logic respond to those prompts appropriately.

Once you have this written and it works locally, it's just a matter of executing it via ssh from a remote server as:

ssh [email protected] /path/to/script.ex

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dean Householder