Akom's Tech Ruminations

Various tech outbursts - code and solutions to practical problems

mssh Bash completion Linux

Posted by Admin • Friday, February 28. 2014 • Category: Linux

Clusterssh (cssh) is great, but I was getting a little fed up with unmanageable terminal windows. They either go all over my monitors or get lost, and they are hard to move and resize.

So I switched to mssh, which solves all that because all the terminals are in one window.... but, it doesn't read my /etc/clusters file! In fact, nobody seemed to even know what file it does read.

What to do? First of all, for the record, it reads its aliases (-a) from ~/.mssh_clusters This file is exactly like /etc/clusters, except for a colon, like so:
alias1: host host host
alias2: host host host

Now that we got that straight, let's make a bash completion file

Now, stick this file into /etc/bash_completion.d/mssh (To apply immediately, run . /etc/bash_completion)

# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
# ex: ts=8 sw=8 noet filetype=sh
#
# mssh(1) completion by Akom (tech.akom.net), adapted from
# cssh by Aaron Spettl <aaron@spettl.de>, adapted from the
# Debian GNU/Linux dput(1) completion by Roland Mas <lolando@debian.org>
# Modified by James Mackie <jc.mackie@gmail.com> to enable node name matching.

have mssh &&
_mssh()
{
        local cur prev options paroptions clusters clusters_containing_cword

        COMPREPLY=()
        cur=${COMP_WORDS[COMP_CWORD]}
        prev=${COMP_WORDS[COMP_CWORD-1]}

        # all options understood by mssh
        options='-a --alias -h --help -V --version'

        # get the names of all defined clusters
        clusters=$(
        {
                sed -e 's/^\([a-z0-9.-]\+\):.*$/\1/i' ~/.mssh_clusters 2> /dev/null || /bin/true
        } | sort -u)

        # get the names of any clusters that contain $cur
        # this is so that you can use node names to retreive any clusters they might be in
        clusters_containing_cword=$(
        {
                grep -- "$cur" ~/.mssh_clusters | sed -e 's/^\([a-z0-9.-]\+\):.*$/\1/i' 2> /dev/null || /bin/true
        } | sort -u)

        # use options and clusters for tab completion, except there isn't yet
        # at least one character to filter by
        # reason: don't show options if the user types "mssh <tab><tab>"
        paroptions="$clusters"
        [ -n "$cur" ] && paroptions="$paroptions $options"

        case $prev in
        'mssh')
                if [ -z "$cur" ] ; then
                        COMPREPLY="--alias"
                else
                        COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "$paroptions" | grep -- "^$cur") )
                fi

                ;;
        #--cluster-file|-c|--config-file|-C)
        #       COMPREPLY=( $( compgen -o filenames -G "$cur*" ) )
        #       ;;
        --alias|-a)
                COMPREPLY=()

                # also use ssh hosts for tab completion if function _known_hosts is present
                # this function returns a lot of noise for me, so I've commented it out
                #[ "`type -t _known_hosts`" = "function" ] && _known_hosts_real -a

                COMPREPLY=( "${COMPREPLY[@]}" $( compgen -W "$paroptions" | grep -- "^$cur") ${clusters_containing_cword} )
                ;;
        esac

        return 0
}
[ "$have" ] && complete -F _mssh mssh

 

0 Trackbacks

  1. No Trackbacks

0 Comments

Display comments as (Linear | Threaded)
  1. No comments

Add Comment


You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

What is the primary language of this blog? (Anti-SPAM question)


Submitted comments will be subject to moderation before being displayed.