Sunday 29 September 2024


Query ssh config and build a dynamic menu using ratmen
======================================================

The file `~/.ssh/config' contains the configuration for how to connect
for a number of hosts.

For each host, it contains a block with a number of lines.
The general format of these blocks is:

    Host foo
            Hostname bar.example.com
            Port 123
            User baz
            IdentityFile ~/.ssh/id_rsa

`foo' is an alias for the host, you can choose anything here, as long
as it is an unique name.

Script to create a menu
-----------------------
Below follows a script that show the alias names in a menu, using
ratmen, to start a ssh session to the selected host.

The script takes the alias names from the `Host' lines (the first line
of each of these blocks) and dynamically creates a command that opens
a menu using ratmen.

Here's the script:

    #!/bin/sh

    awk 'BEGIN{ printf "ratmen --background blue "}  \
         /^Host/  {if ($2 != "\*") { \
            printf "\"%s\" \"urxvt -e ssh %s &\" " , $2, $2 }}' \
         ~/.ssh/config | sh

I saved this script as `~/bin/ratssh.sh'.

Perhaps there is a block for `Host *' in your ~/.ssh/config,
f.e., with a keep-alive statement, or a default user.
This is the reason for the `if'-statement in the awk command.

Ratpoisonrc
-----------
To call it from ratpoison, I added the following line to
`~/.ratpoisonrc':

    bind g exec ~/bin/ratssh.sh

Here, it uses a binding to key 'g' (mnemonic: "go").

Select a key binding that is not yet used,

My ratpoison prefix is the default Ctrl-t.

When I hit `Ctrl-t g', ratpoison calls the script, which creates the
menu on the fly. I select a host, and a new urxvt window is started
with ssh to the specific host.

ratmen or ratmenu
-----------------
There are two menu applications for ratpoison. `ratmen' and `ratmenu'.
They work more or less the same. If you can't find ratmen, try
to use ratmenu.

For `ratmenu', the script perhaps needs some tweaking.



Last edited: $Date: 2024/09/29 19:25:09 $