#!/bin/ksh ####################################################### # # cp2nt - Copy files to an NT share using smbclient # # Author: Reinoud van Leeuwen (reinoud@xs4all.nl) # # # ####################################################### ####################################################### # user definiable settings (edit these to get the script working) # NT_BASE_DIR a directory path under the share # SAMBAPATH the location of smbclient # NT_SERVER the name of the NT server # NT_SHARE the sharename on the NT server # NT_SERVER_IP the IP address of the NT server # NT_USER a NT user that can write to the share # NT_PASS the password for this user ####################################################### export NT_BASE_DIR='dir_under_share' export SAMBAPATH=/usr/local/samba/bin export PATH=$PATH:$SAMBAPATH export NT_SERVER=my_NT_server export NT_SHARE=shared_dir export NT_SERVER_IP=192.168.1.1 export NT_USER=sybase_script export NT_PASS=password export THIS_UNIX=`hostname` export TIMESTAMP=`date +"%Y%m%d%H%M%S"` export TMP_PUT_SCRIPT=/tmp/cp2nt.script.$TIMESTAMP export TMP_DIRLIST=/tmp/cp2nt.dirlist.$TIMESTAMP export TMP_DIRSCRIPT=/tmp/cp2nt.dirscript.$TIMESTAMP export TMP_HOLD_DIR=/tmp/cp2nt.holdir.$TIMESTAMP export TMP_SMBOUT1=/tmp/cp2nt.smbout1.$TIMESTAMP export TMP_SMBOUT2=/tmp/cp2nt.smbout2.$TIMESTAMP export VERSION=`echo '$Revision: 1.5 $' | awk '{print $2}'` export DEBUG=0 export ERROR=0 if [ $DEBUG -ne 0 ] then echo cp2nt version $VERSION fi ################################### # check command-line parameters ################################### if [ $# -lt 1 ] then echo Error: no files specified exit 1 fi ################################### # check readability of specified files ################################### for FILE in $* do if [ ! -r $FILE ] then echo Error: $FILE cannot be read exit 2 fi done ################################### # check wheter files are ascii text files ################################### for FILE in $* do file $FILE | grep text > /dev/null if [ $? -eq 1 ] then echo Error: $FILE is not a text file: file $FILE exit 3 fi done ################################### # create temporary directory to hold DOS format files ################################### mkdir $TMP_HOLD_DIR if [ $? -eq 1 ] then echo Error: unable to create $TMP_HOLD_DIR exit 4 fi ################################### # write filenames and directories to temporary files # change '/' to '\' to create NT directory style # change filepath to uppercase so nasty combinations like \b won't occur ################################### echo lcd $TMP_HOLD_DIR > $TMP_PUT_SCRIPT for FILE in $* do let COUNT=1 OLDDIR=`pwd` # change to dir for each file so pwd will return the full path cd `dirname $FILE` CURDIR=`pwd` typeset -u FILEPATH=`echo $CURDIR/ | sed 'y?/?\?' ` BASENAME=`basename $FILE` HOLDNAME=$BASENAME # cd back to original dir because the next file can have a path relative to the original dir cd $OLDDIR # check whether filename already exists in the temporary directory. This will be the case when the same filename in # different directories is copied. When a duplicate is found, add a number to it to make it unique while [ -f $TMP_HOLD_DIR/$HOLDNAME ] do HOLDNAME=$BASENAME.$COUNT let COUNT=`expr $COUNT + 1` done TOTALPATH=\\$NT_BASE_DIR\\$THIS_UNIX$FILEPATH echo $FILEPATH >> $TMP_DIRLIST echo cd $TOTALPATH >> $TMP_PUT_SCRIPT # issue a del command so a previous version will be deleted when it exists (samba does not do this) echo del $BASENAME >> $TMP_PUT_SCRIPT echo put $HOLDNAME $BASENAME >> $TMP_PUT_SCRIPT # translate the file to DOS format (return/linefeeds etc) ux2dos $FILE > $TMP_HOLD_DIR/$HOLDNAME done ################################### # create a script to create all directories # the script uses nested loops to create the directories. # \dir1\dir2\dir3 will produce a script like # md \dir1 # md \dir1\dir2 # md \dir2\dir2\dir3 ################################### echo md \\$THIS_UNIX > $TMP_DIRSCRIPT cat $TMP_DIRLIST | sort | uniq | awk -F"\\" '{for (i=2; i < NF; i++){printf "md \\%s", ENVIRON["THIS_UNIX"]; for (j=2; j<=i;j++){printf "\\"tolower($j)}; printf "\n"}}' >> $TMP_DIRSCRIPT ################################### # run the created scripts ################################### smbclient \\\\$NT_SERVER\\$NT_SHARE -I $NT_SERVER_IP -U $NT_USER%$NT_PASS -D $NT_BASE_DIR < $TMP_DIRSCRIPT > $TMP_SMBOUT1 smbclient \\\\$NT_SERVER\\$NT_SHARE -I $NT_SERVER_IP -U $NT_USER%$NT_PASS -D $NT_BASE_DIR < $TMP_PUT_SCRIPT > $TMP_SMBOUT2 grep "ERRDOS" $TMP_SMBOUT2 > /dev/null if [ $? -eq 0 ] then echo Error while performing PUT echo output of smbclient: echo " " cat $TMP_SMBOUT2 export ERROR=5 fi ################################### # debug info ################################### if [ $DEBUG -ne 0 ] then echo ------ cat $TMP_DIRLIST echo ------ cat $TMP_DIRSCRIPT echo ------ cat $TMP_PUT_SCRIPT echo ------ cat $TMP_SMBOUT1 echo " " echo ------ cat $TMP_SMBOUT2 echo " " echo ------ fi ################################### # cleanup temporary files ################################### rm $TMP_DIRLIST rm $TMP_PUT_SCRIPT rm $TMP_DIRSCRIPT rm $TMP_HOLD_DIR/* rmdir $TMP_HOLD_DIR rm $TMP_SMBOUT1 rm $TMP_SMBOUT2 if [ $ERROR -ne 0 ] then return $ERROR fi