Skip to content

Instantly share code, notes, and snippets.

@maestrojed
Last active January 29, 2019 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maestrojed/362f415ea740e1c335e0bfd1f8f0df36 to your computer and use it in GitHub Desktop.
Save maestrojed/362f415ea740e1c335e0bfd1f8f0df36 to your computer and use it in GitHub Desktop.
Redirect Output from remote connection using a heredoc
#!/usr/bin/env bash
# sets some vars so the editor doesn't report errors
deployID="1234" # A custom ID retrived in our script, for demo, hardcard
logFile=$(date +%b-%d-%Y-%H-%M-%S)-$deployID".log"
projectPath=${PWD}
sshUser="maestrojed"
sshHost="maestrojed.example.com"
set -x
# redirect all output to the log, while still being verbose on the screen
exec > >(tee -i "$projectPath/deployments/logs/$logFile")
# do some stuff, a shortened example for this demo
cd temp
ls
# Now runs some commands on an SSH connection using a heredoc.
# I also want to redirect the output of these commands, run on the remote server, to my log file. So I capture it in a var $serverOutput
ssh -tt $sshUser@$sshHost <<EOF
pwd
ls
cd myDir/
ls
exit
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment