We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide

How to do for loop in single ssh connection

I'm trying to pass the parameter from python to shell script. Inside shell script, I need to use the parameter to execute another python script.

What I need is:

1)Inside the shell script I need to get the passed parameter.

2)With a single ssh connection, I need to loop the parameter in for loop. (I don't want to open multiple ssh connection for each word)

3)Inside for loop, I need to execute another python script bypassing the variable as a parameter.

My error: Inside the shell script, I'm not able to loop that variable. Where $i is empty.

What I had tried:

test.py

import subprocess
words = ["abbc", "deef", "ghhi"]

join_word = ' '.join(words)
append_word = '"'+join_word+'"'

ssh_key = #Key
bastion_ip = #ip_address
subprocess.call(["sh", "test.sh",ssh_key, bastion_ip ,append_word])

test.sh

ssh -i $1 ubuntu@$2 -o StrictHostKeyChecking=no << EOF
   echo $3  
   #where $3 contains "abbc deef ghhi"
   for i in $3
   do 
     python3 hello.py $i
   done
EOF

Thanks in advance.

Comments

  • You may wish to try stackoverflow or similar, this isn't really a forum for programming or shell scripting problems.  More a forum for "what is the best gaming laptop for under £50" type questions.

    While it's not your problem with the while loop, I'm pretty sure you don't mean the two &lt; in test.sh , HTML entities have no place in a shell script. You could probably use awk to split up $3 with space as a delimiter, and a combination of sed and awk to figure out how many words $3 contains so you have a number to use in a for loop. Or maybe look into foreach .
    Proud member of the wokerati, though I don't eat tofu.Home is where my books are.Solar PV 5.2kWp system, SE facing, >1% shading, installed March 2019.Mortgage free July 2023
  • Ignore ^^ ,
    I know you are using &LT&LT to pass << EOT B)
    ssh -i $1 ubuntu@$2 -o StrictHostKeyChecking=no &lt;&lt; EOF
       echo $3  
       #where $3 contains "abbc deef ghhi"
    #all vars , cut 3-end , translate space to new-line then read i echo $* | cut -d" " -f 3- | tr " " "\n" | while read i
    do
    python3 hello.py $i done EOF
    quick and dirty I know but my pi is down at the mo

    4.8kWp 12x400W Longhi 9.6 kWh battery Giv-hy 5.0 Inverter, WSW facing Essex . Aint no sunshine ☀️ Octopus gas fixed dec 24 @ 5.74 tracker again+ Octopus Intelligent Flux leccy

    CEC Email energyclub@moneysavingexpert.com
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 353.5K Banking & Borrowing
  • 254.2K Reduce Debt & Boost Income
  • 455.1K Spending & Discounts
  • 246.6K Work, Benefits & Business
  • 602.9K Mortgages, Homes & Bills
  • 178.1K Life & Family
  • 260.6K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.