We're aware that some users are experiencing technical issues which the team are working to resolve. See the Community Noticeboard for more info. Thank you for your patience.
📨 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!

How to do for loop in single ssh connection

Options

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
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
  • 350.8K Banking & Borrowing
  • 253.1K Reduce Debt & Boost Income
  • 453.5K Spending & Discounts
  • 243.8K Work, Benefits & Business
  • 598.7K Mortgages, Homes & Bills
  • 176.8K Life & Family
  • 257.1K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.1K Discuss & Feedback
  • 37.6K 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.