Varsh
Varsh

Reputation: 465

Access and append local file from remote host in bash script

From my bash script, doing ssh login to the remote host. Sending my config file along with a script to the remote host to access the variables. Code is as below:

#!/bin/bash

source ~/shell/config.sh
script=$(cat <<\____HERE

   pubkeyarray=()
   privkeyarray=()
   for (( w=1; w<=3; w++))
   do
        cleos create key --file key[$w].txt
        privatekey=$(sed -ne 's/^Private key: \([^ ]*\).*/\1/p' ~/key[$w].txt)
        pubkey=$(sed -ne 's/^Public key: \([^ ]*\).*/\1/p' ~/key[$w].txt)
        pubkeyarray+=("$pubkey")
        privkeyarray+=("$privatekey")
   done
   echo "genesis_public_key=${pubkeyarray[0]}" >>config.sh
   echo "genesis_private_key=${privkeyarray[0]}" >>config.sh

____HERE
)

SCRIPT="$(cat ~/shell/config.sh) ; $script;"
for i in ${!genesishost[*]} ; do
        SCR=${SCRIPT/PASSWORD/${password}}
        sshpass -p ${password} ssh -l ${username} ${genesishost[i]} "${SCR}"
done

genesishost, password and username are in config.sh file. Doing some EOSIO operations.

What I need:

From the remote host, I want to append the config file with "genesis_public_key" and "genesis_private_key" which is on my local machine from which I am doing ssh.

This code generates a new config file on remote and appends these lines there. Do I need to ssh my local machine? If yes, the local machine cannot be the same always to add its ssh credentials. How can I accomplish this task? Please guide.

Upvotes: 0

Views: 882

Answers (1)

tripleee
tripleee

Reputation: 189417

The script obviously only has access to files on the remote server, but instead of having it write to a (nonxistent) file, simply have it write out the output you want to add to the local file, and have the caller do the append.

#!/bin/bash

source ~/shell/config.sh
script=$(cat <<\____HERE

   pubkeyarray=()
   privkeyarray=()
   for (( w=1; w<=3; w++))
   do
        # ... whatever ...
   done
   echo "genesis_public_key=${pubkeyarray[0]}"    # no redirect
   echo "genesis_private_key=${privkeyarray[0]}"  # no redirect

____HERE
)

SCRIPT="$(cat ~/shell/config.sh) ; $script;"
for i in ${!genesishost[*]} ; do
        SCR=${SCRIPT/PASSWORD/${password}}
        sshpass -p ${password} ssh -l ${username} ${genesishost[i]} "${SCR}"
done >>config.sh

Obviously, if multiple remote hosts print out assignments for variables with the same name, the last assignment in config.sh will replace all the earlier ones.

Upvotes: 1

Related Questions