Emacs Tramp tricks
Table of Contents
Emacs org-mode literate programming
Ok this is a neat thing, something that is really useful.
First lets explain the tramp
, tamp is a feature in emacs which can be used to access remote
files from within emacs. consider that you want to edit a file on a
remote server, though since this server is solely a server, no one
has even consider installing emacs and even less consider
configuring it. You can access it through ssh, and as usual vi
is
installed, that's how I did it before. Then I came across tramp
,
I tried it with some luck and some failures and put it on the shelf
and added it to the "CHECK IT OUT" list for some rainy day. Well,
the rainy day arrived (quite quickly unfortunatly) and i started
fiddling with it, and to my surprise this was awsome. This is what
I've been looking for. Editing files with my local emacs on the
remote machine is just awesome. Though, I must admit that the
URL-parsing was a bit of a hassle. So lets explain it really simple
/connection:user@host
- connection
- .e.g. file,ssh,rsh,sudo and many more
- user
- system user name
- host
- host or ip.
these are the basic types…here is an example
/ssh:myname@192.168.8.1:/etc/passwd
Lets first consider using it on a local file. Usually we do C-x C-f
/home/user/mytext.org
, the same way we can connect to a remote
machine and doing exactly the same: C-x C-f
/ssh:user@192.168.8.2:~/mytext.org
Its now possible to edit the file
on the remote server the exact same way as it was on a local
machine. Quite useful, though in my case most of the time I need to
ssh into the machine, and then use sudo to become a superuser.
This of course is possible using tramp, though now the url parsing becomes somewhat
problematic. So lets explain it in steps. Lets first edit a localfile with "sudo".
/sudo:root:/etc/passwd
The first we want to do is to ssh into the machine, the output from that ssh session is then "piped" into sudo..
/sudo:user@host:/dir/file
when editing a local file with "sudo" you can ommit the "user@host"
so that the it becomes sudo::/etc/passwd
there is also a "sudoedit"
connection type which is similair to sudo , however it does not keep
the session running in the background , and is restricted to
localhost
/sudoedit::/etc/passwd
That means that using sudo with a user and remote will actually use an ssh session together with sudo.
Tramp Compination
So far we been looking at how to use tramp solely, it is also possible to combine both e.g. ssh and sudo this is done using the "|" command. Lets do an example.
/ssh:user@192.168.8.5|sudo:root@192.168.8.5:/etc/passwd
To go even further its possible to connect to a remote machine via ssh and hop to another, and there edit a file using sudo..
/ssh:user@192.168.8.5|ssh:user@192.168.8.2|sudo:root@192.168.8.2:/etc/passwd
In other words, its possible to do all kinds of different kombination, at least in theory.
Org-mode links
Of course the above urls can be used as org-links.
E.g.
[[/ssh:user@192.168.8.5|sudo:root@192.168.8.5:/etc/passwd][edit passwd @192.168.1.5]]
But another useful org feature is using src
fields creating
small scripts runing on local and even remote computer.
Again lets do an example.
#+NAME: debootstrap_search #+begin_src bash :dir /tmp :results drawer :exports results pacman -Ss debootstrap #+end_src
#+RESULTS: debootstrap_search :results: community/debootstrap 1.0.123-1 Bootstrap a basic Debian system :end:
For more information on results see result and evaluation.
The above code will execute pacman -Ss debootstrap
on the local machine
And the result will be added as a result drawer. The result has a name,
so we could use another source block and use the result as input.
e.g.
#+NAME: awkward #+begin_src awk :stdin debootstrap_search /debootstrap/ { split($1,a,"/"); print a[1],"->",a[2] } #+end_src #+RESULTS: awkward : community -> debootstrap
In the above example I used the output from the debootstrap_search
and feeded it into awk
where I search for the line debootstrap , and split it up into an array.
gcc |
clang |
emacs |
echo " Name Version URL" for var in "${apa[@]}"; do pacman -Qi ${var} | awk ' /(Name|Version|URL)\s+:/ { if ($1 == "URL" ) printf "%s\n",$3; else if($1 == "Name" || $1 == "Version") printf "%s ",$3; }' done
Name | Version | URL |
gcc | 12.1.0-2 | https://gcc.gnu.org |
clang | 13.0.1-2 | https://clang.llvm.org/ |
emacs | 28.1-7 | https://www.gnu.org/software/emacs/emacs.html |
This time I did not use a drawer, instead I want the output as a table.
Calling source block
Re-use of is always fun, so sometimes you want to reuse something previously defined.
hostname ip add | awk '/inet(.*)\/24/ {print $2}' uname -a | awk '{print $3}'
echo "${in[0]},${in[1]},${in[2]}" echo "banan,rumpa,pumba"
Now if I want to use that again, but maybe use another host, I could set the
dir
to that host.
alarm |
192.168.1.2/24 |
5.16.10-1-aarch64-ARCH |
unset apa declare -A apa apa['fisk']='pisk PUSK' apa['sump']='rump SNUSK' #echo $apa echo ${apa[fisk]} echo ${apa[sump]}
Run a docker
Ok, lets setup a docker image We use the results value, we are only intrested in the last command
Download archlinux
docker pull $name
Check docker images
#+
Final example
What I want to do is to setup a table with all the host. For each of the host in the table I want to get the current cpu load, and then in the final table I want to have them in a graph..
Let there be a table
This table consists of all the ip addresses that I want to use
The table is eassiest to create with C-|
and follow the instructions
Computers |
---|
192.168.1.2 |
192.168.1.8 |
192.168.1.10 |
# what i want to do here is to iterate over the ip numbers and then # call another function to perform the necessary evaluation for ip in ${IPS[@]}; do echo ${ip} done
cpu | 103757 | 4 | 27745 | 12296631 | 5124 | 2285 | 1491 | 0 | 0 | 0 |
cpu0 | 8212 | 0 | 2270 | 1024760 | 416 | 280 | 439 | 0 | 0 | 0 |
cpu1 | 8378 | 0 | 2147 | 1025321 | 377 | 113 | 77 | 0 | 0 | 0 |
cpu2 | 9110 | 0 | 2122 | 1024512 | 411 | 114 | 63 | 0 | 0 | 0 |
cpu3 | 10564 | 2 | 2244 | 1023232 | 337 | 143 | 60 | 0 | 0 | 0 |
cpu4 | 9859 | 0 | 2307 | 1023696 | 442 | 146 | 75 | 0 | 0 | 0 |
cpu5 | 9850 | 0 | 2048 | 1024010 | 398 | 120 | 52 | 0 | 0 | 0 |
cpu6 | 8228 | 0 | 1921 | 1025446 | 474 | 200 | 77 | 0 | 0 | 0 |
cpu7 | 7605 | 0 | 1932 | 1026277 | 376 | 107 | 54 | 0 | 0 | 0 |
cpu8 | 6971 | 0 | 1971 | 1026724 | 481 | 106 | 49 | 0 | 0 | 0 |
cpu9 | 9255 | 0 | 2262 | 1024160 | 576 | 154 | 109 | 0 | 0 | 0 |
cpu10 | 8041 | 0 | 2135 | 1025124 | 422 | 233 | 376 | 0 | 0 | 0 |
cpu11 | 7677 | 0 | 4381 | 1023363 | 409 | 564 | 56 | 0 | 0 | 0 |