Showing the Network Neighbourhood in console mode script

Xavier ROCHE roche at serianet.com
Fri Apr 27 14:07:23 GMT 2001


Hi,

Here is a script that does list all machines in a neighbourhood
Feel free to comment it, it is a draft script only at this time, and I'm
still searching for cleaner solutions

-------------- next part --------------
#!/bin/sh
#
# Name: smblist [ output_file | '-' [ interface | broadcast_address | default [ -ip ]]]
# Desc: Network Neighbourhood list generator for samba
# Version: 1.0, April 2001 (roche at serianet.com)
# Usage: to run in the crontab:
# 0,10,20,30,40,50 * * * * root /usr/local/bin/smblist 2>/dev/null
# or as a script, like in:
# for i in `ifconfig | grep "^[^ ]" | grep -v "lo" | cut -d' ' -f1`; do /usr/local/bin/smblist "-" "$i"; done

# Main default network card
ETH=
BCMASK=
# File generated by default
FILE="/var/run/smbmch"

###

# Parameters if any ?
OK=-
if test "$#" -ge 1; then
	if test "$1" = "--help" -o "$1" = "-h"; then
		echo -e "usage: $0 [ output_file | '-' [ interface | broadcast_address | default [ -ip ]]]"
		echo -e "example:\t$0 - eth0"
		echo -e "        \t$0 - default -ip"
		echo -e "        \t$0 /tmp/foo 192.168.0.255"
		exit
	fi
	FILE="$1"
	# Stdout
	if test "$FILE" = "-"; then
		FILE=
	fi
fi
if test "$#" -ge 2; then
	if test "$2" = "default"; then
		BCMASK=
		ETH=
	# Broadcast mask	
	elif echo -e "$2"|grep -E "\.">/dev/null; then
		BCMASK="$2"
	else
	# Interface
		ETH="$2"
	fi
fi
SHOWIP=
if test "$#" -ge 3; then
	if test "$3" = "-ip"; then
		SHOWIP=-
	fi
fi

# Cleanup output file if necessary
if test -n "$FILE"; then
	rm -f "$FILE"
	rm -f "$FILE.tmp"
	# (Avoid security problems)
	if test -f "$FILE"; then
		OK=
	elif test -f "$FILE.tmp"; then
		OK=
	fi
	if test -n "$OK"; then
		>"$FILE.tmp"
		# Empty file successfully created
		if ! test -f "$FILE.tmp"; then
			OK=
		else
			# Set access rights to everyone (+ id nobody)
			chown nobody "$FILE.tmp"
			chmod 644 "$FILE.tmp"
		fi
	fi
fi

# Params are OK
if test -n "$OK"; then
	# Network Bcast mask?
	BCMASKCMD=
	if test -n "$ETH"; then
		BCMASK=`ifconfig $ETH | grep "Bcast" | sed -e 's/.*Bcast:\([0-9\.]*\).*/\1/'`
		if ! test -n "$BCMASK"; then
			echo "an error occured, bad interface name $ETH" 2>&1
			exit 1
		fi
	fi
	if test -n "$BCMASK"; then
		BCMASKCMD="-B $BCMASK"
	fi
	# Scan all responding hosts
	for i in `nmblookup $BCMASKCMD '*' | grep -E "^[0-9.][0-9.]*" | cut -d' ' -f1`; do
		# Reverse-lookup through nmblookup and get what we want
		if NBN=`nmblookup -A "$i" 2>/dev/null | grep -E "<00>" | head -1 | tr '\t' ' ' | sed -e 's/[ ]*\([^ ]*\).*/\1/'`; then
			# Not empty (you never know)
			if test -n "$NBN"; then
				# Also show IP address
				SIP=
				if test -n "$SHOWIP"; then
					SIP=" $i"
				fi
				# Add entry in file/stdout
				if test -n "$FILE"; then
					echo "$NBN$SIP" >> "$FILE.tmp"
				else
					echo "$NBN$SIP"
				fi
			fi
		fi
	done
	if test -n "$FILE"; then
		# File created - move it
		mv -f "$FILE.tmp" "$FILE"
	fi
else
	echo "an error occured" 2>&1
	exit 1
fi

###



More information about the samba mailing list