0

I work on a remote server that I SSH into that has individual nodes that you can also SSH into. I'm working on a script hosted on the main remote server that can SSH into these individual nodes and clear out the /tmp directories for the user running it on every node. We tried bash initially but hit a snag and my main experience is in Python, so that's what I've fallen back on.

I have it set up to where the script will only attempt to access nodes where the user has no active jobs running (if we didn't need this constraint, using bash becomes much easier). Now, we've hit a new snag where, when I use subprocess to begin iterating over the nodes, it SSHs into the first node in our list and just sits there, even though my script tells it to exit once it gets there (we have yet to implement the deletion part). It seems that once it accesses the node it gets stranded from the original script and doesn't know what to do next. How can I allow my script to communicate with the node(s) in such a way that it executes?

(As I am not the owner of the remote server, I cannot import any package not already installed.)

import os, sys, re
import math
import subprocess as sp

def enter_node(node):
    sp.call(["ssh", node])
    print("entering")
    sp.call(["exit"])
    print("exiting")

print(usable_nodes)
num_nodes = len(usable_nodes)

j=0
while j < num_nodes:
    if usable_nodes[j] <= 99:
        node = "cn" + "0" + str(usable_nodes[j])
    else:
        node = "cn" + str(usable_nodes[j])
    enter_node(node)
    j += 1

The usable_nodes list (which I don't include here due to relevance) is successfully gotten.

subprocess.call and subprocess.run, as expected, don't produce different results, and defining a subroutine for iterating through the nodes also did not change anything.

  • The accepted answer [here](https://stackoverflow.com/questions/19900754/python-subprocess-run-multiple-shell-commands-over-ssh) shows how to ssh using Python. – niyaz Aug 30 '23 at 05:40

0 Answers0