Pipe script and binary data for stdin via ssh

I want to remotely execute a bash script that consumes tarball and does some logic. The trick is that I want to use only one ssh command (instead of scpfor tarball, followed by sshfor script).

The bash script looks like this:

cd /tmp
tar -zx
./archive/some_script.sh
rm -r archive

I understand that I can just reformat this script into a single line and use

tar -cz ./archive | ssh $HOST bash -c '<commands>'

but my actual script is quite complicated, so I have to pass it to bashvia stdin. The problem here is that it sshprovides only one input channel (stdin), which I want to use for both the bash script and tarball.

+4
source share
1

, bash script tarball stdin.

1. base64-encoded tarball heredoc

bash script, tarball heredoc:

base64 -d <<'EOF_TAR' | tar -zx
<base64_tarball>
EOF_TAR

:

ssh $HOST bash -s < <(
# Feed script header
cat <<'EOF'
cd /tmp
base64 -d <<'EOF_TAR' | tar -zx
EOF

# Create local tarball, and pipe base64-encoded version
tar -cz ./archive | base64

# Feed rest of script
cat <<'EOF'
EOF_TAR
./archive/some_script.sh
rm -r archive
EOF
)

tar tarball , .

2. script

bash script stdin, tarball. bash tar, tar- stdin:

ssh $HOST bash -s < <(
# Feed script.
cat <<'EOF'
function main() {
  cd /tmp
  tar -zx
  ./archive/some_script.sh
  rm -r archive
}
main
EOF
# Create local tarball and pipe it
tar -cz ./archive
)

, tar tarball .

main, ? bash script, tar? , bash script, , tar tarfile, bash script. , main bash script tar.

+5

Source: https://habr.com/ru/post/1539830/


All Articles