Org Babel

Print Error of Code Block


ls --foo
ls: unrecognized option '--foo'
Try 'ls --help' for more information.


Doesn't work:

echo "hi"
exit 1
set -u
echo $foo
echo "hi"
ssh -T github-work

Instead, this works though:

ssh -T github-work 2>&1 || true
Hi chrisc-ironnet! You've successfully authenticated, but GitHub does not provide shell access.


python -c 'bar=b"ОФИС"''

Emacs Lisp

To print data structure, use :results value pp

'(("DeploymentName" "webserver")
  ("KeyName" "cfc"))
(("DeploymentName" "webserver")
 ("KeyName" "cfc"))


Single Hop

:dir /ssh:ec2-user@

Double-Hop (Use a Bastion)

:dir /ssh:ec2-user@|ssh:ec2-user@

Copy File to Remote Machine

This leverages my host function.

(copy-file MyFile (host "ec2-user" "/tmp/my_file.txt" 'sudo) t)

IP Address Variable

Because you don't want to hard-code IP addresses in your source blocks like some kind of animal!

(defun host (user ip path &optional sudo)
  "Return a TRAMP string for SSHing to a remote host.
USER is a user name on the remote host IP. PATH is the path on
the remote host at which to execute the source block. If SUDO is
non-nil, use sudo on the remote host."
  (if sudo
      (s-lex-format "/ssh:${user}@${ip}|sudo:${ip}:${path}")
    (s-lex-format "/ssh:${user}@${ip}:${path}")))

Then set variables at the top of a file, and use something like:

#+begin_src bash :dir (host (getenv "HOST") (getenv "USER") "/home/ec2-user")