vineetbansal / wbi

0 stars 0 forks source link

Execute remote commands using paramiko #12

Closed vineetbansal closed 10 months ago

vineetbansal commented 11 months ago

Currently wbi remote is programmed to run slurm scripts remotely. This is not the only execution mode possible, and we should also be able to run command directly on the head node (e.g. hostname) instead of submitting them to slurm.

We can integrate this alternate mode of execution in wbi later, but in preparation for this, we will want to just run a script using paramiko to connect to Della, and execute a remote command, and capture its output (look in remote.py on how to do this).

Once we get simple command like hostname to work, the next step would be to tackle issue #13 .

anushka255 commented 10 months ago

Implemented a simple execution of running a command on head node. Connects to host in the same way as it does in remote.py. The only difference is the new script remote_head.py runs commands directly as shown in the example below:

cmd = 'echo "Hello $(hostname)"'
stdin, stdout, stderr = client.exec_command(cmd)
vineetbansal commented 10 months ago

In this example, you're implicitly assuming that what you're executing is a bash command ("echo" is not an executable). This assumption will not hold in the general case. The command we want to be able to execute is bash -i "echo Hello $(hostname)".

anushka255 commented 10 months ago

Changed the command into bash -c "echo Hello $(hostname)"

vineetbansal commented 10 months ago

What I meant to say is that the example you gave:

cmd = 'echo "Hello $(hostname)"'
stdin, stdout, stderr = client.exec_command(cmd)

Should not work, because if it does, it means that cmd is being passed to bash. We don't want to pass commands to bash, we want to pass them directly to the operating system.

You may find that exec_command has some flag like shell=True which is causing this to happen (exact argument names may vary), similar to functions in the subprocess module. We want to turn off this behavior.

Simply changing cmd to bash -c "echo Hello $(hostname)" will of course work in this case (for the same reason that copy-pasting bash -c "echo Hello $(hostname)" works on the command line), because then you're asking bash to open up another process with the bash executable and then run echo inside it.

To sum up - if the command:

cmd = 'bash -c "echo Hello $(hostname)"'

works, but

cmd = 'echo Hello $(hostname)'

does not, then we've managed to do the right thing here.