Home > Blockchain >  Pass arguments for SQL statement in a shell file from another shell file through ssh command
Pass arguments for SQL statement in a shell file from another shell file through ssh command

Time:02-23

I am passing command line arguments to a shell file i.e assignRole.sh which contains an SQL command which will use these arguments like below

ssh -o StrictHostKeyChecking=no -T $key < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt

This gives me error and does not accept arguments of name and open_mode and gives error, but if I execute the statement outside of ssh command like:

/oracle/oracle_user/makhshif/./assignRole.sh name open_mode

This runs fine

What is the problem with ssh command and how should I adjust these parameters so these can be accepted for the shell script assignRole.sh

CodePudding user response:

 < /oracle/oracle_user/makhshif/./assignRole.sh

This commands sends a content of that file to stdin. So obviously it can't process variables that you haven't send to remote machine. Just preprocess your script or create a script on remote machine and call it with arguments

Though it's even easier to pass variables like this:

ssh -o StrictHostKeyChecking=no -T $key "var1=$var1 var2=$var2" < /oracle/oracle_user/makhshif/./assignRole.sh name open_mode >> /oracle/oracle_user/dftest.txt

For example my function for executing update scripts on all cluster nodes:

# functions:
ssh_exec(){
  local DESCR="$1"; shift
  local SCRIPT="$1"; shift
  local hosts=("$@")
  echo =================================================
  echo =   $DESCR
  echo = Going to execute $SCRIPT...
  read -a res -p "Enter 'skip' to skip this step or press Enter to execute: "
  if [[ $res = "skip" ]]
  then
    echo Skipping $SCRIPT...
  else
    echo Executing $SCRIPT...
    for host in "${hosts[@]}"
        do
          local cur=${!host}
          echo Executing $SCRIPT on $host - $cur...
          sshpass -p "$rootpass" ssh -o "StrictHostKeyChecking no" root@${cur} \
            "ns1=$ns1 ns2=$ns2 search=$search zoo1=$zoo1 zoo2=$zoo2 zoo3=$zoo3 node0=$node0 pass=$pass CURIP=$cur CURHOST=$host bash -s" \
            <$SCRIPT >log-$SCRIPT-$cur.log 2>&1
          echo Done.
        done
    echo =================================================
  fi
}

Then I use it like this:

read -p "Please check that Solr started successfully and Press [Enter] key to continue..."

#Solr configset and collections:
ssh_exec "Solr configset and collections" script06.sh zoo1 zoo2 zoo3

This command executes script06.sh on 3 servers (zoo1,zoo2,zoo3)

CodePudding user response:

As Sayan said, using < redirects the output of running the assignRole.sh script locally, but you want to execute that script on the remote host, with the arguments.

Pass the whole command as the final argument to ssh, in quotes:

ssh -o StrictHostKeyChecking=no -T $key "/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" >> /oracle/oracle_user/dftest.txt

or split into multiple lines for readability:

ssh -o StrictHostKeyChecking=no -T $key \ 
  "/oracle/oracle_user/makhshif/./assignRole.sh name open_mode" \
  >> /oracle/oracle_user/dftest.txt
  • Related