So führen Sie mehrere Codezeilen in SSH in einem Bash-Skript aus

So führen Sie mehrere Codezeilen in SSH in einem Bash-Skript aus

Ich möchte den folgenden Code ausführen, aber der Fehler im Code wird in vi rot hervorgehoben. Es tritt ein Fehler auf nachsudo ssh -t root@$ip << EOFZeile. Wo habe ich falsch geskriptet?

#!/bin/bash
cassandra_home=$(python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"cassandra_home\"])")
iplist[@]=$(python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"iplist\"])")
for ip in ${iplist[@]}
do
  sudo ssh -t root@$ip << EOF
    for ip in ${iplist[@]} 
    do
      echo Checking $ip for ongoing repairs
      ${cassandra_home}nodetool -h $ip tpstats | grep Repair#
      response=$?
      if [ $response -eq 0 ]; then
        repair_ongoing=true
        echo "Ongoing repair on $ip"
      fi
    done 
    if ! [ $repair_ongoing ]; then
      ## echo "Taking a snapshot."
      ## ${cassandra_home}bin/nodetool -h $ip snapshot
      echo "Starting repair on $ip"
      start=$(date +%s)
      ${cassandra_home}bin/nodetool -h $ip repair -pr -inc -local metadata
      sleep 3
      ${cassandra_home}bin/nodetool -h $ip cleanup metadata 
      end=$(date +%s)
      #echo "ks.tab,st,et,last run,status">>repair_status.csv
      echo "Repair and cleanup completed for metadata in $((end - start)) seconds"
    fi
    exit 0
  EOF
done           

Antwort1

Verwendenhttps://www.shellcheck.net/(es gibt ein Vim-Plugin) Es würde Ihnen sagen

Line 18:
  EOF
 ^-- SC1039: Remove indentation before end token (or use <<- and indent with tabs).

Führen Sie anschließend viele weitere Probleme auf.

Antwort2

Sie versuchen, ein Array von Werten zu speichern, iplist[@]aber als statische Deklaration ...

Versuchen Sie Folgendes:

#!/bin/bash
cassandra_home=(`python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"cassandra_home\"])"`)
iplist[@]=(`python -c "import json; print \",\".join(json.load(open('${repair.json}','r'))[\"iplist\"])`)
for ip in ${iplist[@]}
do
  sudo ssh -t root@$ip "
    for ip in ${iplist[@]} 
    do
      echo Checking $ip for ongoing repairs
      ${cassandra_home}nodetool -h $ip tpstats | grep Repair#
      response=$?
      if [ $response -eq 0 ]; then
        repair_ongoing=true
        echo \"Ongoing repair on $ip\"
      fi
    done 
    if ! [ $repair_ongoing ]; then
      ## echo \"Taking a snapshot.\"
      ## ${cassandra_home}bin/nodetool -h $ip snapshot
      echo \"Starting repair on $ip\"
      start=`date +%s`
      ${cassandra_home}bin/nodetool -h $ip repair -pr -inc -local metadata
      sleep 3
      ${cassandra_home}bin/nodetool -h $ip cleanup metadata 
      end=`date +%s`
      #echo \"ks.tab,st,et,last run,status\">>repair_status.csv
      echo \"Repair and cleanup completed for metadata in $end - $start seconds\"
    fi
    exit 0"

done   

verwandte Informationen