#!/bin/sh
# Convert list of Windows/Samba boxes in local network to .nsmbrc format
# Args, if any passed directly to findsmb - network address and broadcast address
# $Id: smb2nsmbrc,v 1.4 2006/05/12 08:14:13 shelton Exp $

outfile=".nsmbrctmp"		# Generated file name
nsmbfile="nsmb.conf.tmp"	# Generated additions for /etc/nsmb.conf

# Save normal IFS
saveifs=$IFS

# Check on presence network address
if [ $# -lt 1 ]; then
  echo "Specify network address in CIDR format or address space like A-B"
  echo "when A and B are IP's"
  exit
 else
   network=$1
fi

# Check on presence attendant utilites
wtools="nbtscan smbutil host"

for tool in $wtools
 do
  locator=`whereis -b  $tool`
  set $locator

  if [ -z $2 ]; then
    echo "Your system does not include $tool utility, install it first"
    exit 15
   else
     eval "util_$tool=$2"
  fi
 done

# Now we ask username
echo -n "Please enter username: "
read uname

# And translate it to UPPER case
username=`echo $uname | tr "[:lower:]" "[:upper:]"`

# Set temp file name
xtfile=`date "+%s"`

# Make temporarly repository
install -d -o $USER -g "$GROUP" -m 0700 /tmp/$xtfile
cd /tmp/$xtfile

# Ask about set codepage translation tables (that cannot be in .nsmbrc file!)
echo -n "Would you like to set translation tables? [y/n]: "
read settabl

# Ask about translation pairs (you sholud be know it)
if [ $settabl = "y" ] || [ $settabl = "Y" ]; then
  echo "Enter local and remote tables as <local>:<remote> pair"
  echo -n "Sample for Russian is koi8-r:cp1251: "
  read pairs

# Simply check on line format
  IFS=:
  set $pairs

  if [ ${#2} -eq 0 ]; then
    echo "Invalid format of translation tables, should be <local>:<remote>"
    exit
  fi
fi

# Now we ask password and crypt by smbutil
password=`$util_smbutil crypt`

# Now we detect all Windows/Samba boxes and take their NetBIOS names
splitifs='
'
fieldifs=:

IFS=$splitifs

netfull=`$util_nbtscan -s : $network 2> /dev/null`

for oneline in $netfull
 do
  IFS=$fieldifs

# Split one nbtscan report line
  set $oneline

# Add name to line
  netnames="$netnames $2"

# Take DNS name by correspond IP address
  dnsrespond=`$util_host -t PTR $1`
  IFS=$saveifs

# Split host respond
  set $dnsrespond

# Check on results. When unsuccesful, add NONE
  if [ ! $3 = "not" ]; then
    IFS=.
    dnsname=${5%*.}
   else
     dnsname=NONE
  fi

# When this is first run, DO NOT ADD separator at start line!
  if [ ${#dnsnames} -eq 0 ]; then
    dnsnames=$dnsname
   else
    dnsnames="$dnsnames:$dnsname"
  fi

  IFS=$splitifs

 done

IFS=$saveifs

# And at least we generating temporarly file with username, password and
# sections for all detected boxes
for netname in $netnames
 do

# Generate .nsmbrc part
  echo "[$netname:$username]" >> $outfile
  echo "password=$password" >> $outfile
  echo "" >> $outfile

  thisname=${dnsnames%%:*}
  newdnsnames=${dnsnames#*:}
  dnsnames=$newdnsnames

# When DNS name was detected
  if [ $thisname = "NONE" ]; then
    continue
   else
    echo "[$netname]" >> $nsmbfile
    echo "addr=$thisname" >> $nsmbfile

# When we should set codepage translations
    if [ $settabl = "y" ] || [ $settabl = "Y" ]; then
      echo "charsets=$pairs" >> $nsmbfile
    fi

    echo "" >> $nsmbfile
  fi

 done

# Final message
echo "In /tmp/$xtfile now generated additions for .nsmbrc and /etc/nsmb.conf files"
echo "You should add it to correspond files manually"


syntax highlighted by Code2HTML, v. 0.9.1