[patch] cwm: filter duplicate hostnames in ssh menu

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[patch] cwm: filter duplicate hostnames in ssh menu

Bruno Flueckiger
Hi,

The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
them from ~/.ssh/known_hosts. This patch makes sure each hostname is
only displayed once to the menu.

Cheers,
Bruno


Index: app/cwm/kbfunc.c
===================================================================
RCS file: /cvs/xenocara/app/cwm/kbfunc.c,v
retrieving revision 1.165
diff -u -p -r1.165 kbfunc.c
--- app/cwm/kbfunc.c 7 Mar 2019 14:28:17 -0000 1.165
+++ app/cwm/kbfunc.c 12 Mar 2019 11:24:47 -0000
@@ -672,6 +672,7 @@ kbfunc_menu_ssh(void *ctx, struct cargs

  lbuf = NULL;
  len = 0;
+skip:
  while ((slen = getline(&lbuf, &len, fp)) != -1) {
  buf = lbuf;
  if (buf[slen - 1] == '\n')
@@ -686,6 +687,10 @@ kbfunc_menu_ssh(void *ctx, struct cargs
  if (p - buf + 1 > sizeof(hostbuf))
  continue;
  (void)strlcpy(hostbuf, buf, p - buf + 1);
+ /* skip duplicate hostnames */
+ TAILQ_FOREACH(mi, &menuq, entry);
+ if (strcmp(mi->text, hostbuf) == 0)
+ goto skip;
  menuq_add(&menuq, NULL, "%s", hostbuf);
  }
  free(lbuf);

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Okan Demirmen
On Fri 2019.04.26 at 07:15 +0200, Bruno Fl?ckiger wrote:
> Hi,
>
> The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
> them from ~/.ssh/known_hosts. This patch makes sure each hostname is
> only displayed once to the menu.

Sure, maybe; but why again do we even have this inside a window manager?
Really, the known_hosts parsing is incomplete at best; either the entire
parsing code needs to be lifted from ssh or this (mis)feature should be
removed from cwm. I prefer the latter.

> Cheers,
> Bruno
>
>
> Index: app/cwm/kbfunc.c
> ===================================================================
> RCS file: /cvs/xenocara/app/cwm/kbfunc.c,v
> retrieving revision 1.165
> diff -u -p -r1.165 kbfunc.c
> --- app/cwm/kbfunc.c 7 Mar 2019 14:28:17 -0000 1.165
> +++ app/cwm/kbfunc.c 12 Mar 2019 11:24:47 -0000
> @@ -672,6 +672,7 @@ kbfunc_menu_ssh(void *ctx, struct cargs
>
>   lbuf = NULL;
>   len = 0;
> +skip:
>   while ((slen = getline(&lbuf, &len, fp)) != -1) {
>   buf = lbuf;
>   if (buf[slen - 1] == '\n')
> @@ -686,6 +687,10 @@ kbfunc_menu_ssh(void *ctx, struct cargs
>   if (p - buf + 1 > sizeof(hostbuf))
>   continue;
>   (void)strlcpy(hostbuf, buf, p - buf + 1);
> + /* skip duplicate hostnames */
> + TAILQ_FOREACH(mi, &menuq, entry);
> + if (strcmp(mi->text, hostbuf) == 0)
> + goto skip;
>   menuq_add(&menuq, NULL, "%s", hostbuf);
>   }
>   free(lbuf);
>

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Marcus MERIGHI
Hello,

[hidden email] (Okan Demirmen), 2019.04.29 (Mon) 16:19 (CEST):

> On Fri 2019.04.26 at 07:15 +0200, Bruno Fl?ckiger wrote:
> > Hi,
> >
> > The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
> > them from ~/.ssh/known_hosts. This patch makes sure each hostname is
> > only displayed once to the menu.
>
> Sure, maybe; but why again do we even have this inside a window manager?
> Really, the known_hosts parsing is incomplete at best; either the entire
> parsing code needs to be lifted from ssh or this (mis)feature should be
> removed from cwm. I prefer the latter.

FWIW, i use "M-period" a lot... are there easy alternatives?

Marcus

> > Cheers,
> > Bruno
> >
> >
> > Index: app/cwm/kbfunc.c
> > ===================================================================
> > RCS file: /cvs/xenocara/app/cwm/kbfunc.c,v
> > retrieving revision 1.165
> > diff -u -p -r1.165 kbfunc.c
> > --- app/cwm/kbfunc.c 7 Mar 2019 14:28:17 -0000 1.165
> > +++ app/cwm/kbfunc.c 12 Mar 2019 11:24:47 -0000
> > @@ -672,6 +672,7 @@ kbfunc_menu_ssh(void *ctx, struct cargs
> >
> >   lbuf = NULL;
> >   len = 0;
> > +skip:
> >   while ((slen = getline(&lbuf, &len, fp)) != -1) {
> >   buf = lbuf;
> >   if (buf[slen - 1] == '\n')
> > @@ -686,6 +687,10 @@ kbfunc_menu_ssh(void *ctx, struct cargs
> >   if (p - buf + 1 > sizeof(hostbuf))
> >   continue;
> >   (void)strlcpy(hostbuf, buf, p - buf + 1);
> > + /* skip duplicate hostnames */
> > + TAILQ_FOREACH(mi, &menuq, entry);
> > + if (strcmp(mi->text, hostbuf) == 0)
> > + goto skip;
> >   menuq_add(&menuq, NULL, "%s", hostbuf);
> >   }
> >   free(lbuf);
> >
>

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Matthias Schmidt
Hi,

* Marcus MERIGHI wrote:
>
> FWIW, i use "M-period" a lot... are there easy alternatives?

While I really like the ssh-menu feature it does not work reliable for
me here.  For some servers I can establish a SSH connection, for others
a xterm launches that immediately closes again.  To be honest, I never
spend time debugging it.

Thus, I added all my important hosts as menu entries:

        command foo "xterm -e ssh foo.example.com"

There might be other solutions/launchers, however, that works for me :)

Cheers

        Matthias

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Bruno Flueckiger
In reply to this post by Marcus MERIGHI
On 01.05., Marcus MERIGHI wrote:

> Hello,
>
> [hidden email] (Okan Demirmen), 2019.04.29 (Mon) 16:19 (CEST):
> > On Fri 2019.04.26 at 07:15 +0200, Bruno Fl?ckiger wrote:
> > > Hi,
> > >
> > > The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
> > > them from ~/.ssh/known_hosts. This patch makes sure each hostname is
> > > only displayed once to the menu.
> >
> > Sure, maybe; but why again do we even have this inside a window manager?
> > Really, the known_hosts parsing is incomplete at best; either the entire
> > parsing code needs to be lifted from ssh or this (mis)feature should be
> > removed from cwm. I prefer the latter.
>
> FWIW, i use "M-period" a lot... are there easy alternatives?
>
> Marcus
>

I'm thinking about transforming the ssh menu into a standalone
application. This way the (mis)feature could be removed, but those who
like using would get an adequate replacement. Any opinions about that?

Bruno

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Landry Breuil-5
On Mon, May 06, 2019 at 08:04:02AM +0200, Bruno Fl├╝ckiger wrote:

> On 01.05., Marcus MERIGHI wrote:
> > Hello,
> >
> > [hidden email] (Okan Demirmen), 2019.04.29 (Mon) 16:19 (CEST):
> > > On Fri 2019.04.26 at 07:15 +0200, Bruno Fl?ckiger wrote:
> > > > Hi,
> > > >
> > > > The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
> > > > them from ~/.ssh/known_hosts. This patch makes sure each hostname is
> > > > only displayed once to the menu.
> > >
> > > Sure, maybe; but why again do we even have this inside a window manager?
> > > Really, the known_hosts parsing is incomplete at best; either the entire
> > > parsing code needs to be lifted from ssh or this (mis)feature should be
> > > removed from cwm. I prefer the latter.
> >
> > FWIW, i use "M-period" a lot... are there easy alternatives?
> >
> > Marcus
> >
>
> I'm thinking about transforming the ssh menu into a standalone
> application. This way the (mis)feature could be removed, but those who
> like using would get an adequate replacement. Any opinions about that?

there are already standalone applications that do this among other
things, https://github.com/davatorium/rofi comes to my mind (yes, it's in
ports).

Landry

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Thuban
* Landry Breuil <[hidden email]> le [06-05-2019 09:45:08 +0200]:

> On Mon, May 06, 2019 at 08:04:02AM +0200, Bruno Fl├╝ckiger wrote:
> > On 01.05., Marcus MERIGHI wrote:
> > > Hello,
> > >
> > > [hidden email] (Okan Demirmen), 2019.04.29 (Mon) 16:19 (CEST):
> > > > On Fri 2019.04.26 at 07:15 +0200, Bruno Fl?ckiger wrote:
> > > > > Hi,
> > > > >
> > > > > The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
> > > > > them from ~/.ssh/known_hosts. This patch makes sure each hostname is
> > > > > only displayed once to the menu.
> > > >
> > > > Sure, maybe; but why again do we even have this inside a window manager?
> > > > Really, the known_hosts parsing is incomplete at best; either the entire
> > > > parsing code needs to be lifted from ssh or this (mis)feature should be
> > > > removed from cwm. I prefer the latter.
> > >
> > > FWIW, i use "M-period" a lot... are there easy alternatives?
> > >
> > > Marcus
> > >
> >
> > I'm thinking about transforming the ssh menu into a standalone
> > application. This way the (mis)feature could be removed, but those who
> > like using would get an adequate replacement. Any opinions about that?
>
> there are already standalone applications that do this among other
> things, https://github.com/davatorium/rofi comes to my mind (yes, it's in
> ports).
>
> Landry
>

CWM is a window manager.
It should manage windows, not ssh IMHO.

I use the following (perfectible) shell script, bind to a key in any
window manager :


        #!/bin/sh
        # run ssh session in one hosts listed in
        #   ~/.ssh/config
        #   ~/.ssh/known_hosts
        # Require : x11/dmenu <https://tools.suckless.org/dmenu/>
        # Author : thuban <[hidden email]>
        # Licence : MIT

        # term ?
        TERMINAL='xterm'

        # few options for dmenu
        NB="#151515"
        NF="#e1e5ea"
        SF="#151515"
        SB="#75a9e8"
        FN="Liberation Mono-12"
        PROMPT="ssh host:"
        LINES=6

        C=~/.ssh/config
        K=~/.ssh/known_hosts

        HOST=$(
        (awk '/Host [^*]/ {print $2}' "${C}"
        cut -d' ' -f 1 "${K}" |\
                cut -d',' -f1 |\
                sed -e 's;\[;;' -e 's;\];;'
        ) | sort -u \
        | dmenu -l ${LINES} \
        -fn "${FN}" \
        -nb "${NB}" \
        -nf "${NF}" \
        -sb "${SB}" \
        -sf "${SF}" \
        -p "${PROMPT}")

        if [ -n "${HOST}"]; then
                case "${HOST}" in
                        *:*) "${TERMINAL}" -e "ssh ssh://${HOST}" ;;
                        *) "${TERMINAL}" -e "ssh ${HOST}" ;;
                esac
        fi

        exit


--
        thuban

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Bruno Flueckiger
In reply to this post by Marcus MERIGHI
On 01.05., Marcus MERIGHI wrote:

> Hello,
>
> [hidden email] (Okan Demirmen), 2019.04.29 (Mon) 16:19 (CEST):
> > On Fri 2019.04.26 at 07:15 +0200, Bruno Fl?ckiger wrote:
> > > Hi,
> > >
> > > The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
> > > them from ~/.ssh/known_hosts. This patch makes sure each hostname is
> > > only displayed once to the menu.
> >
> > Sure, maybe; but why again do we even have this inside a window manager?
> > Really, the known_hosts parsing is incomplete at best; either the entire
> > parsing code needs to be lifted from ssh or this (mis)feature should be
> > removed from cwm. I prefer the latter.
>
> FWIW, i use "M-period" a lot... are there easy alternatives?
>
> Marcus
>

I use dmenu[1] from suckless.org as a replacement. The following script
reads the host names from ~/.ssh/known_hosts, feeds them to dmenu and
executes ssh to the host I choose in an xterm:

#!/bin/ksh

expr="s/^@[a-z-]* //;s/[, ].*//;s/\[//;s/\]//"
file=~/.ssh/known_hosts
host=$(cat $file | sed "$expr" | sort | uniq | dmenu)
[[ -n "$host" ]] && xterm -T "[ssh] $host" -e ssh $host

I store the script in ~/bin/sshmenu.sh and add the following line to my
~/.cwmrc:

bind-key M-period bin/sshmenu.sh

Works like a charm for me.

Cheers,
Bruno

[1] https://suckless.org/dmenu/

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Bruno Flueckiger
In reply to this post by Okan Demirmen
On 29.04., Okan Demirmen wrote:

> On Fri 2019.04.26 at 07:15 +0200, Bruno Fl?ckiger wrote:
> > Hi,
> >
> > The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
> > them from ~/.ssh/known_hosts. This patch makes sure each hostname is
> > only displayed once to the menu.
>
> Sure, maybe; but why again do we even have this inside a window manager?
> Really, the known_hosts parsing is incomplete at best; either the entire
> parsing code needs to be lifted from ssh or this (mis)feature should be
> removed from cwm. I prefer the latter.
>

After trying some possible replacements from ports it turns out that
dmenu from suckless.org together with a small shell script works best
for me.

This patch removes menu-ssh completely from cwm.

Cheers,
Bruno

Index: app/cwm/calmwm.h
===================================================================
RCS file: /cvs/xenocara/app/cwm/calmwm.h,v
retrieving revision 1.371
diff -u -p -r1.371 calmwm.h
--- app/cwm/calmwm.h 7 Mar 2019 14:28:17 -0000 1.371
+++ app/cwm/calmwm.h 9 May 2019 08:22:14 -0000
@@ -304,7 +304,6 @@ struct conf {
  int xrandr;
  int xrandr_event_base;
  char *conf_file;
- char *known_hosts;
  char *wm_argv;
  int debug;
 };
@@ -517,7 +516,6 @@ void kbfunc_menu_cmd(void *, struct c
 void kbfunc_menu_group(void *, struct cargs *);
 void kbfunc_menu_wm(void *, struct cargs *);
 void kbfunc_menu_exec(void *, struct cargs *);
-void kbfunc_menu_ssh(void *, struct cargs *);
 void kbfunc_client_menu_label(void *, struct cargs *);
 void kbfunc_exec_cmd(void *, struct cargs *);
 void kbfunc_exec_lock(void *, struct cargs *);
Index: app/cwm/conf.c
===================================================================
RCS file: /cvs/xenocara/app/cwm/conf.c,v
retrieving revision 1.249
diff -u -p -r1.249 conf.c
--- app/cwm/conf.c 7 Mar 2019 12:54:21 -0000 1.249
+++ app/cwm/conf.c 9 May 2019 08:22:14 -0000
@@ -179,7 +179,6 @@ static const struct {

  { FUNC_SC(menu-cmd, menu_cmd, 0) },
  { FUNC_SC(menu-group, menu_group, 0) },
- { FUNC_SC(menu-ssh, menu_ssh, 0) },
  { FUNC_SC(menu-window, menu_client, CWM_MENU_WINDOW_ALL) },
  { FUNC_SC(menu-window-hidden, menu_client, CWM_MENU_WINDOW_HIDDEN) },
  { FUNC_SC(menu-exec, menu_exec, 0) },
@@ -210,7 +209,6 @@ static const struct {
  { "CM-Delete", "lock" },
  { "M-question", "menu-exec" },
  { "CM-w", "menu-exec-wm" },
- { "M-period", "menu-ssh" },
  { "M-Return", "window-hide" },
  { "M-Down", "window-lower" },
  { "M-Up", "window-raise" },
@@ -316,7 +314,6 @@ conf_init(struct conf *c)
  home = "/";
  }
  xasprintf(&c->conf_file, "%s/%s", home, ".cwmrc");
- xasprintf(&c->known_hosts, "%s/%s", home, ".ssh/known_hosts");
 }

 void
@@ -363,7 +360,6 @@ conf_clear(struct conf *c)
  free(c->color[i]);

  free(c->conf_file);
- free(c->known_hosts);
  free(c->font);
  free(c->wmname);
 }
Index: app/cwm/cwm.1
===================================================================
RCS file: /cvs/xenocara/app/cwm/cwm.1,v
retrieving revision 1.64
diff -u -p -r1.64 cwm.1
--- app/cwm/cwm.1 25 Feb 2019 18:07:48 -0000 1.64
+++ app/cwm/cwm.1 9 May 2019 08:22:14 -0000
@@ -140,15 +140,6 @@ Resize window by a large amount; see
 Spawn
 .Dq exec program
 dialog.
-.It Ic M-period
-Spawn
-.Dq ssh to
-dialog.
-This parses
-.Pa $HOME/.ssh/known_hosts
-to provide host auto-completion.
-.Xr ssh 1
-will be executed via the configured terminal emulator.
 .It Ic CM-w
 Spawn
 .Dq exec WindowManager
Index: app/cwm/cwmrc.5
===================================================================
RCS file: /cvs/xenocara/app/cwm/cwmrc.5,v
retrieving revision 1.72
diff -u -p -r1.72 cwmrc.5
--- app/cwm/cwmrc.5 25 Feb 2019 16:40:49 -0000 1.72
+++ app/cwm/cwmrc.5 9 May 2019 08:22:14 -0000
@@ -280,10 +280,6 @@ menu.
 Launch
 .Dq exec WindowManager
 menu.
-.It menu-ssh
-Launch
-.Dq ssh
-menu.
 .It group-toggle-[n]
 Toggle visibility of group n, where n is 1-9.
 .It group-only-[n]
Index: app/cwm/kbfunc.c
===================================================================
RCS file: /cvs/xenocara/app/cwm/kbfunc.c,v
retrieving revision 1.165
diff -u -p -r1.165 kbfunc.c
--- app/cwm/kbfunc.c 7 Mar 2019 14:28:17 -0000 1.165
+++ app/cwm/kbfunc.c 9 May 2019 08:22:14 -0000
@@ -644,72 +644,6 @@ out:
 }

 void
-kbfunc_menu_ssh(void *ctx, struct cargs *cargs)
-{
- struct screen_ctx *sc = ctx;
- struct cmd_ctx *cmd;
- struct menu *mi;
- struct menu_q menuq;
- FILE *fp;
- char *buf, *lbuf, *p;
- char hostbuf[HOST_NAME_MAX+1];
- char path[PATH_MAX];
- int l;
- size_t len;
- ssize_t slen;
- int mflags = (CWM_MENU_DUMMY);
-
- TAILQ_FOREACH(cmd, &Conf.cmdq, entry) {
- if (strcmp(cmd->name, "term") == 0)
- break;
- }
- TAILQ_INIT(&menuq);
-
- if ((fp = fopen(Conf.known_hosts, "r")) == NULL) {
- warn("%s: %s", __func__, Conf.known_hosts);
- goto menu;
- }
-
- lbuf = NULL;
- len = 0;
- while ((slen = getline(&lbuf, &len, fp)) != -1) {
- buf = lbuf;
- if (buf[slen - 1] == '\n')
- buf[slen - 1] = '\0';
-
- /* skip hashed hosts */
- if (strncmp(buf, HASH_MARKER, strlen(HASH_MARKER)) == 0)
- continue;
- for (p = buf; *p != ',' && *p != ' ' && p != buf + slen; p++)
- ;
- /* ignore badness */
- if (p - buf + 1 > sizeof(hostbuf))
- continue;
- (void)strlcpy(hostbuf, buf, p - buf + 1);
- menuq_add(&menuq, NULL, "%s", hostbuf);
- }
- free(lbuf);
- if (ferror(fp))
- err(1, "%s", path);
- (void)fclose(fp);
-menu:
- if ((mi = menu_filter(sc, &menuq, "ssh", NULL, mflags,
-    search_match_text, search_print_text)) != NULL) {
- if (mi->text[0] == '\0')
- goto out;
- l = snprintf(path, sizeof(path), "%s -T '[ssh] %s' -e ssh %s",
-    cmd->path, mi->text, mi->text);
- if (l == -1 || l >= sizeof(path))
- goto out;
- u_spawn(path);
- }
-out:
- if (mi != NULL && mi->dummy)
- free(mi);
- menuq_clear(&menuq);
-}
-
-void
 kbfunc_client_menu_label(void *ctx, struct cargs *cargs)
 {
  struct client_ctx *cc = ctx;

Reply | Threaded
Open this post in threaded view
|

Re: [patch] cwm: filter duplicate hostnames in ssh menu

Marcus MERIGHI
In reply to this post by Bruno Flueckiger
[hidden email] (Bruno Flueckiger), 2019.05.12 (Sun) 14:39 (CEST):

> On 01.05., Marcus MERIGHI wrote:
> > Hello,
> >
> > [hidden email] (Okan Demirmen), 2019.04.29 (Mon) 16:19 (CEST):
> > > On Fri 2019.04.26 at 07:15 +0200, Bruno Fl?ckiger wrote:
> > > > Hi,
> > > >
> > > > The ssh menu of cwm(1) doesn't filter duplicated hostnames when reading
> > > > them from ~/.ssh/known_hosts. This patch makes sure each hostname is
> > > > only displayed once to the menu.
> > >
> > > Sure, maybe; but why again do we even have this inside a window manager?
> > > Really, the known_hosts parsing is incomplete at best; either the entire
> > > parsing code needs to be lifted from ssh or this (mis)feature should be
> > > removed from cwm. I prefer the latter.
> >
> > FWIW, i use "M-period" a lot... are there easy alternatives?
> >
> I use dmenu[1] from suckless.org as a replacement. The following script
> reads the host names from ~/.ssh/known_hosts, feeds them to dmenu and
> executes ssh to the host I choose in an xterm:
 
Matthias, Landry, Thuban, Bruno - Thanks for your hints!

I settled for the script below. Thanks for the blueprint, Bruno!

Marcus

#!/bin/sh -eu
_h=$(while read _l; do print ${_l%%[ ,]*}; done < ~/.ssh/known_hosts | \
  sort -u | dmenu)
exec xterm -T "[ssh] $_h" -e "ssh $_h || read _a?'enter to continue'"