Access and append local file from remote host in bash script

非 Y 不嫁゛ 提交于 2021-01-29 08:12:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/64965726/access-and-append-local-file-from-remote-host-in-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!