#!/bin/bash # # Helper program for subscribing NetBIOS names to resolvconf # Copyright (C) 2016 Daniel Boland # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA NMBLOOKUP=`which nmblookup` TESTPARM=`which testparm` DIG=`which dig` if ! [ $NMBLOOKUP ]; then echo "This script needs the 'nmblookup' program." exit 1 elif ! [ $TESTPARM ]; then echo "This script needs the 'testparm' program." exit 1 elif ! [ $DIG ]; then echo "This script needs the 'dig' program." exit 1 fi GROUP=`$TESTPARM -s --parameter-name=workgroup 2>/dev/null` MASTER="" ADDRESS="" NBTHOST="" function print_resolv { DOMAIN=`$DIG @$MASTER +noall +answer -x $MASTER | awk '{print $NF}' | sed 's/[^.]\+.//; s/.$//'` echo "search $DOMAIN" echo "nameserver $MASTER" } function lookup_master { MASTER=`$NMBLOOKUP $GROUP#1b | grep "$GROUP<1b>" | awk '{print $1}'` if ! [ $MASTER ]; then echo echo "Domain master for workgroup '$GROUP' not found." >&2 exit 1 fi } function lookup_nbthost { while read -r; do fields=($REPLY) case "${fields[1]}" in "<20>") echo " NetBIOS hostname is '${fields[0]}'" NBTHOST=${fields[0]} ;; "<1b>") echo " Host is a master browser for workgroup '${fields[0]}'" ;; "<1c>") echo " Host is a domain controller for workgroup '${fields[0]}'" ;; "<1d>") echo " Host is a computer browser for workgroup '${fields[0]}'" ;; esac done <<<"`$NMBLOOKUP -A "$1" | grep "" | awk '{print $1 " " $2}'`" } function lookup_host { info=(`host $1 | awk '{print $1 " " $NF}'`) if [[ ${info[1]} =~ "NXDOMAIN" ]]; then echo " Could not lookup host '$1'" else echo " ${info[0]} has address ${info[1]}" echo -n " " host ${info[1]} fi } function set_group { if ! [[ `$NMBLOOKUP $1 | grep "$1<00>" | awk '{print $1}'` ]]; then echo "$1: No such workgroup." >&2 exit 1 fi GROUP=`echo "$1" | awk '{print toupper($0)}'` } while [ $1 ]; do case $1 in -h|--help) COMMAND="help" ;; -A|--lookup-by-ip) ADDRESS="$2" COMMAND="lookup" shift ;; -*) echo "$1: No such option." exit 1 ;; status) COMMAND="$1" ;; *) set_group "$1" ;; esac shift done function do_help { printf 'Usage: %s [COMMAND] [options] [WORKGROUP]\n' `basename $0` echo echo "Commands:" printf -- " status\t\t\t\tPerform some status tests on server and client\n" echo echo "Options:" printf -- " -A,--lookup-by-ip [HOST]\tDo a node status on HOST as an IP Address\n" printf -- " -h,--help\t\t\tThis help text\n" } function do_add { lookup_master print_resolv | /sbin/resolvconf -a lo.$PROGRAM } function do_lookup { lookup_nbthost $ADDRESS } function do_status { lookup_master echo "Winbind:" echo -n " " && wbinfo -p 2>/dev/null echo -n " " && wbinfo -t 2>/dev/null echo "Server:" lookup_nbthost $MASTER lookup_host $NBTHOST echo "Client:" lookup_host `hostname -s` echo -n " " && net rpc testjoin 2>&1 | tail -1 } if [ $COMMAND ]; then do_$COMMAND else lookup_master print_resolv fi