Benutzer-Werkzeuge

Webseiten-Werkzeuge


Action disabled: source
scripting:bash:bashsmb

bashsmb.sh

Summary

I'm using smbnetfs to browse smb-shares in a network.
Mostly it is working but sometimes it doesn't and for these cases I've written a script in bash which mounts all shares in a network it is permitted to read/write.

Main

Warranty

Warranty

There is no warranty that this setup works for any system which provide the dependencies listed below.
I'm just providing these information because it worked for me.
If you have questions you can leave a message here but I decide whether I'll answer and help or not.

Security - Connection

Security issue

If you use the described setup you'll copy files using an unencrypted connection.
That is why it is possible to sniff data about you, your systems and your infrastructure.
You should only use it within a secure network and only if you trust everybody who is using it.

Dependencies:

  • extra/smbclient
  • core/bash


Version History:

0.1.5:

  • fixed bug for not config file if .domain is not set

0.1:

  • supports authorization file
  • u/mount smb-shares
  • builds directory structure domain/server/share
  • refreshing shares

Usage

bashsmb.bsh <command>
command:
         m <path> <option>: mount smb-shares
            Option:
                    d: with default share like C$ in Windows (experimental)
         r <path> <option>: update smb-shares
            Option:
                    d: with default share like C$ in Windows (experimental)
         u <path> <option>: umount smb-shares
            Option:
                    l: umount lazy share-mount e.g. due to connection-loss to a server (needs kernel > 2.4.10)
         p: print smb-shares

bashsmb supports authentication using a simple auth-file. This file is $HOME/.smb/bashsmb.auth. The structure is simple:

server.user=username
server.pass=password
 
domainname.user=username
domainname.pass=password
domainname.domain=domain

Script

bashsmb.sh
#!/bin/bash
 
######################################################################
#
#                  Author: Andrwe Lord Weber
#                  E-Mail: lord-weber-andrwe<at>renona-studios<dot>org
#                  Version: 0.1.5 
#                  Description:
#                       This script searchs smb-shares and mounts them
#                       if permitted to destination path
#
######################################################################
 
 
function getBashTmp
{
	server=${1}
	tmpdom=`echo ${server} | sed 's/.*\(.*\..*\)/\1/'`
	if [ "`grep ${server} ~/.smb/bashsmb.auth`" -o "`grep ${tmpdom} ~/.smb/bashsmb.auth`" ]
	then
		user=`grep ${server}.user ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
		pass=`grep ${server}.pass ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
		dom=`grep ${server}.domain ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
		if [ ! "${user}" -a ! "${pass}" ]
		then
			user=`grep ${tmpdom}.user ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
			pass=`grep ${tmpdom}.pass ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
			dom=`grep ${tmpdom}.domain ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
		fi
		smbclient -gL ${server} -U ${dom}/${user}%${pass} > /tmp/bashsmb.$$ 2>&1
	else
		smbclient -gNL ${server} > /tmp/bashsmb.$$ 2>&1
	fi
}
 
# Creates directory structure for found smb-shares and mounts all permitted shares
function m
{
	BASEDIR=`echo "${1}" | sed 's#/$##'`
	for server in `nmblookup -T '*' | grep -vG '^query' | awk -F ',' '{print $1}'`
	do
		if [[ ${server} =~ ^local.* ]]
		then
			continue
		fi
		getBashTmp ${server}
		domain=`grep -m 1 Domain /tmp/bashsmb.$$ | awk -F '[' '{print $2}' | awk -F ']' '{print $1}'`
		if [ ! ${domain} ]
		then
			domain=`echo ${server} | awk -F '.' '{print $2}'`
		fi
		if [ ! -d "${BASEDIR}/${domain}" ]
		then
			mkdir "${BASEDIR}/${domain}"
		fi
		if [ ! -d "${BASEDIR}/${domain}/${server}" ]
		then
			mkdir "${BASEDIR}/${domain}/${server}"
		fi
		for disk in `grep Disk /tmp/bashsmb.$$ | awk -F '|' '{print $2}' | sed 's/\ /\\\\ /g'`
		do
			if [[ ! ${2} =~ d ]]
			then
				if [[ ${disk} =~ .*\$$ ]]
				then
					continue
				fi
			fi
			if [ ! -d "${BASEDIR}/${domain}/${server}/${disk}" ]
			then
				mkdir "${BASEDIR}/${domain}/${server}/${disk}"
			fi
			if [ ! "`mount | grep ${BASEDIR}/${domain}/${server}/${disk}`" -a ! "`mount | grep ${BASEDIR}/${server}/${disk}`" ]
			then
				if [ "`grep ${server} ~/.smb/bashsmb.auth`"  -o "`grep ${tmpdom} ~/.smb/bashsmb.auth`" ]
				then
					user=`grep ${server}.user ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
					pass=`grep ${server}.pass ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
					dom=`grep ${server}.domain ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
					if [ ! "${user}" -a ! "${pass}" ]
					then
						user=`grep ${tmpdom}.user ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
						pass=`grep ${tmpdom}.pass ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
						dom=`grep ${tmpdom}.domain ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
					fi
					if [ "${dom}" ]
					then
						if [ "${domain}" ]
						then
							sudo mount //${server}/"${disk}" "${BASEDIR}/${domain}/${server}/${disk}" -o user=${dom}/${user}%${pass}
						else
							sudo mount //${server}/"${disk}" "${BASEDIR}/${server}/${disk}" -o user=${dom}/${user}%${pass}
						fi
					else
						if [ "${domain}" ]
						then
							sudo mount //${server}/"${disk}" "${BASEDIR}/${domain}/${server}/${disk}" -o user=${user}%${pass}
						else
							sudo mount //${server}/"${disk}" "${BASEDIR}/${server}/${disk}" -o user=${user}%${pass}
						fi
					fi
				else
					sudo mount //${server}/"${disk}" "${BASEDIR}/${domain}/${server}/${disk}" -o guest 2> /dev/null
				fi
			fi
			disk_esc=`echo ${disk} | sed 's/\$$//'`
			if [ "`mount | grep ${BASEDIR}/${domain}/${server}/${disk_esc}`" == "" ]
			then
				rm -r "${BASEDIR}/${domain}/${server}/${disk}"
			fi
		done
		rm /tmp/bashsmb.$$
	done
}
 
# Refresh smb-shares: mount new and remove old non-existing
function r 
{
	BASEDIR=`echo "${1}" | sed 's#/$##'`
	for server in `nmblookup -T '*' | grep -vG '^query' | awk -F ',' '{print $1}'`
	do
		if [[ ${server} =~ ^local.* ]]
		then
			continue
		fi
		getBashTmp ${server}
		for share in `mount | grep cifs | awk -F ' ' '{print $1}'`
		do
			if [ "`echo ${share} | grep ${server}`" ]
			then
				disk=`echo ${share} | awk -F '/' '{print $4}'`
				if [ ! "`grep Disk\|${disk}\| /tmp/bashsmb.$$`" ]
				then
					dir=`mount | grep ${share} | grep ${disk} | awk -F ' ' '{print $3}'`
					if [ ! -z "`sudo umount ${dir}`" ]
					then
						echo "Couldn't umount ${share} normally."
					fi
					if [ ! "`mount | grep ${dir}`" ]
					then
						rm -r ${dir}
						echo removed ${dir}
					fi
				fi
			fi
		done
	done
	rm /tmp/bashsmb.$$
	m ${BASEDIR} ${2}
}
 
# Unmount smb-shares and removes directories
function u
{
	BASEDIR=`echo "${1}" | sed 's#/$##'`
	for domain in ${BASEDIR}/*
	do
		for server in ${domain}/*
		do
			for disk in ${server}/*
			do
				disk_esc=`echo ${disk} | sed 's/\$$//'`
				if [ "`mount | grep ${disk_esc}`" ]
				then
					if [ ! -z "`sudo umount ${disk}`" ]
					then
						echo "Couldn't umount ${disk} normally."
					fi
					if [[ ${2} =~ l ]]
					then
						echo "Due to parameter 'l' try to force umount using -l argument."
						if [ ! -z "`sudo umount -l ${disk}`" ]
						then
							echo "Couldn't umount ${disk}."
						fi
					fi
				fi
			done
		done
		if [ ! "`mount | grep ${domain}`" ]
		then
			rm -r ${domain}
		fi
	done
}
 
# Prints existing Server and shares in network
function p
{
	for server in `nmblookup -T '*' | grep -vG '^query' | awk -F ',' '{print $1}'`
	do
		if [[ ${server} =~ ^local.* ]]
		then
			continue
		fi
		echo "Server: ${server}"
		tmpdom=`echo ${server} | sed 's/.*\(.*\..*\)/\1/'`
		if [ "`grep ${server} ~/.smb/bashsmb.auth`" -o "`grep ${tmpdom} ~/.smb/bashsmb.auth`" ]
		then
			user=`grep ${server}.user ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
			pass=`grep ${server}.pass ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
			dom=`grep ${server}.domain ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
			if [ ! "${user}" -a ! "${pass}" ]
			then
				user=`grep ${tmpdom}.user ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
				pass=`grep ${tmpdom}.pass ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
				dom=`grep ${tmpdom}.domain ~/.smb/bashsmb.auth | awk -F "=" '{print $2}'`
			fi
			smbclient -gL ${server} -U ${dom}/${user}%${pass} 2> /dev/null > /tmp/bashsmb.$$
		else
			smbclient -gNL ${server} 2> /dev/null > /tmp/bashsmb.$$
		fi
		for disk in `grep Disk /tmp/bashsmb.$$ | awk -F '|' '{print $2}'`
		do
			echo "   ${disk}"
		done
		rm /tmp/bashsmb.$$
	done
}
 
function usage
{
	echo "Usage: ${0} <command>"
	echo .
	echo "command:"
	echo "         m <path> <option>: mount smb-shares"
	echo "            Option:"
	echo "                    d: with default share like C$ in Windows (experimental)"
	echo "         r <path> <option>: update smb-shares"
	echo "            Option:"
	echo "                    d: with default share like C$ in Windows (experimental)"
	echo "         u <path> <option>: umount smb-shares"
	echo "            Option:"
	echo "                    l: umount lazy share-mount e.g. due to connection-loss to a server (needs kernel > 2.4.10)"
	echo "         p: print smb-shares"
}
 
case "$1" in
	m|r|u )
		case "${2}" in
			/* )
				${1} ${2} ${3}
			;;
			* )
				echo "Second argument has to be the destination path."
				exit 1
		esac
	;;
	p )
		p
	;;
	* )
		usage
esac

Comments



X A M N Q
scripting/bash/bashsmb.txt · Zuletzt geändert: 2012/08/30 12:09 von Andrwe Lord Weber