0

I am running an experiment with several instances - one server node with a floating IP address, and several client nodes without - in which I need to use the server node to access the client nodes. This is fine with SCP as it is a one-off process but SSH has given me more problems - especially considering that I need to do this particular SSH via Python.

Here is the script being used:

user='cc'
host_keyname='my_chameleon_key.pem'
port=22

script = '#!/bin/bash'   '\n' \
    'USER="' + user + '"'   '\n' \
    'KEY_NAME="' + host_keyname + '"'   '\n' \
    'PORT_NUMBER='+ str(port) +   '\n' \
    'CLIENT_IPS=(' + ip_string + ')'   '\n' \
    'for ip in "${CLIENT_IPS[@]}"'   '\n' \
    'do'   '\n' \
    '    ssh -i ~/.ssh/${KEY_NAME} ${USER}@${ip} << ENDHERE'   '\n' \
    '        git clone https://github.com/pari685/AStream.git'   '\n' \
    '    ENDHERE'   '\n' \
    'done'   '\n'

And the Paramiko call:

key = {
    "key_filename": key_path,
}

with Connection(host=host_ip, user="cc", connect_kwargs=key) as c:
    c.run(script)

All of the client IPs are stored space delimited in the string ip_string and host_ip is a string containing just my server node.

Unsurprisingly, Python is unhappy with my ENDHERE. I'm not sure how to exit the internal hop in the script's for loop. What should I be doing instead?

  • Also, I'm not proficient with python, but isn't there a better way to write multiline strings ? – Aserre Aug 10 '21 at 15:22
  • For instance, [this question](https://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string) combined with [this one](https://stackoverflow.com/questions/10112614/how-do-i-create-a-multiline-python-string-with-inline-variables) may do the trick – Aserre Aug 10 '21 at 15:25
  • @Aserre You can write multi-line comments using triple-quotations, but the issue is with my here-document. I'm sure the answer is buried in there somewhere but I'm having trouble understanding it... will take a closer look. – Mason Hicks Aug 10 '21 at 15:28
  • Try the duplicate question I linked : remove the spaces before `ENDHERE` – Aserre Aug 10 '21 at 15:29
  • 1
    @Aserre Wait, I got it. Thanks! E: Yeah you were right. – Mason Hicks Aug 10 '21 at 15:30

1 Answers1

0

As stated by Aserre in the threaded reply, ENDHERE must be at the very beginning of the line without any space/indentation. This was a misconception on my end as I thought it would be at the block-level indent within my for loop.

Here is the updated script:

user='cc'
host_keyname='my_chameleon_key.pem'
port=22

script = '#!/bin/bash'   '\n' \
    'USER="' + user + '"'   '\n' \
    'KEY_NAME="' + host_keyname + '"'   '\n' \
    'PORT_NUMBER='+ str(port) +   '\n' \
    'CLIENT_IPS=(' + ip_string + ')'   '\n' \
    'for ip in "${CLIENT_IPS[@]}"'   '\n' \
    'do'   '\n' \
    '    ssh -i ~/.ssh/${KEY_NAME} ${USER}@${ip} << ENDHERE'   '\n' \
    '        git clone https://github.com/pari685/AStream.git'   '\n' \
    'ENDHERE'   '\n' \
    'done'   '\n'

Thanks for the help.