Montag, August 27, 2007

Avahi-patch for bash_completion

I wrote a small patch to allow the programmable bash-completion to complete hosts found via Avahi for commands like ping, ssh, scp, etc.

Reactiontime suffers somewhat - but that seems to be the fault of avahi-browse, which takes a while to return all recognized hosts.

Requirements: avahi-daemon has to be running and avahi-browse has to be available via $PATH. And of course, the programmable bash-completion by Ian MacDonald has to be installed :)

Patch follows in clear text. Usage:
(as user root)
cd /etc/
patch < /pfad/zu/avahi-hosts.patch

That should do it. Please tell me if there are any problems :)

----8< Cut and save as avahi-hosts.patch >8----

--- bash_completion 2006-03-01 17:20:18.000000000 +0100
+++ bash_completion_patched 2007-08-26 17:24:12.000000000 +0200
@@ -2410,7 +2410,7 @@
_known_hosts()
{
local cur curd ocur user suffix aliases global_kh user_kh hosts i host
- local -a kh khd config
+ local -a kh khd config avahi_hosts avahi_available avahi_browse

COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
@@ -2522,6 +2522,24 @@
COMPREPLY=( $( compgen -A hostname -S "$suffix" -- $cur ) )
fi

+ # append recognized avahi hosts
+ # Check, whether avahi is running and avahi-browse is available
+ avahi_available=`pidof avahi-daemon`
+ avahi_browse=`which avahi-browse`
+ if [[ -n "$avahi_available" && -n "$avahi_browse" ]]; then
+ avahi_hosts=`avahi-browse -t _workstation._tcp -r | grep hostname | sed -e 's/.*\[//' | sed -e 's/\].*//'`
+ # convert to lower case to get unique hostnames
+ cur=`echo "$cur" | tr [:upper:] [:lower:]`
+ avahi_hosts=`echo "$avahi_hosts" | tr [:upper:] [:lower:]`
+ # Filter relevant results
+ avahi_hosts=`echo "$avahi_hosts" | grep $cur`
+ COMPREPLY=( ${COMPREPLY[@]} $avahi_hosts )
+ fi
+ # apply suffix
+ for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
+ COMPREPLY[i]=$user${COMPREPLY[i]}$suffix
+ done
+
return 0
}
complete -F _known_hosts traceroute traceroute6 tracepath tracepath6 \

----8< Cut and save as avahi-hosts.patch >8----

Have fun :)

Keine Kommentare: