Kristijan.org

I’m back!

Posted by Kristijan on Sunday, December 21st, 2008

How could I stay away? From the shear number of e-mails flooding my inbox daily for updates to my infamous blog, I couldn’t keep the fans in the dark no more!
*cough*
Back to reality…I’m not reviving my old blog, but starting a new one. I’ve been working as a systems administrator for about 4 years now, [...]

continue reading

Sending files to multiple Windows hosts

Posted by Kristijan on Sunday, December 21st, 2008

A few months ago at work, I needed to send a number of files to a little over 100 Windows workstations. I had administrator rights on all these workstations, and luckily for me, they all had the same administrator password.

I had access to a Red Hat AS4 server, so I wrote a shell script to read all the hostnames of the workstations from a text file, mount each C$ share via CIFS, copy the necessary files, unmount and repeat.

Over the past few weeks, I added a few other things which I were of use (ping check prior to send, file verification on remote workstation).

#!/bin/bash
#
# Script to send files to Windows workstations

# set -x

function runPingCheck ()
{
	grep -vE '^$|^#' callcentres/$name.txt | while read i
        do ping -c 1 `echo $i | awk '{print $1}'`.fqdn.com &> /dev/null
                if [ $? -gt 0 ]; then
                        echo -e "$i ... PING FAIL"
                fi

        done
	echo ""
	echo "Check complete"
	echo ""
	echo -n "Continue with send(Y/N) [Y]? "
	read contsend
		case "$contsend" in
			N|n) exit 1 ;;
			Y|y|*) runSendFiles ;;
		esac
}

function runSendFiles ()
{
	echo "Send started `date`" | tee -a $name.log

	grep -vE '^$|^#' callcentres/$name.txt | while read i
        do ping -c 1 `echo $i | awk '{print $1}'`.fqdn.com &> /dev/null
                if [ $? -gt 0 ]; then
                        echo "$i ... Host Offline" | tee -a $name.log
                        continue
                fi

                mount -t cifs //`echo $i | awk '{print $1}'`.fqdn.com/c$ /mnt/winmnt -o user=administrator,pass=PASSHERE
                if [ $? -gt 0 ]; then
                        echo "$i ... Failed CIFS Mount" | tee -a $name.log
                else
                        cp -R ./files/* /mnt/winmnt
                                if [ -e /mnt/winmnt/sent.tmp ]; then
                                        rm /mnt/winmnt/sent.tmp
                                        umount /mnt/winmnt
                                        echo "$i ... Complete" | tee -a $name.log
                                else
                                        umount /mnt/winmnt
                                        echo "$i ... Failed File Verification" | tee -a $name.log
                                fi
                fi
        done

	echo "Send completed `date`" | tee -a $name.log
}

ls ./callcentres | sed 's/.txt//g'
echo ""
echo -n "Type the name of the call centre you wish to upgrade: "
read name

echo ""
echo -n "Would you like to run a ping check before you send(Y/N) [Y]? "
read pingcheck

case "$pingcheck" in
	N|n) runSendFiles ;;
	Y|y|*) runPingCheck ;;
esac

exit 0

The basic directory structure looks like this:
/callcentres
– melbourne.txt
/files
/sendscript.sh

melbourne.txt just had a list of all the hostnames, each on its own line. (# can be used at the start of any line to skip a particular workstation).

host1
#host2
host3
...
host100

/files contains any file that you want to send to the remote Windows host. Take note that you need to provide the absolute path of the Windows location (e.g. /files/Program Files/)

Output of the script will be logged to STDOUT and to a log file in the root directory where sendscript.sh resides.

Once you have the file structure and sendscript.sh in order, simply run the script. You will be prompted for the name of the “call centre” you wish to send files to. In this example, you would enter melbourne. Then you will be asked if you’d like to run a ping check on each workstation prior to the file send.

Happy sending :)

Posted in: Shell Scripting.

One Response to “Sending files to multiple Windows hosts”

  1. Tony Brown Says:

    I don’t know If I said it already but …Cool site, love the info. I do a lot of research online on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks, :)

    A definite great read..Tony Brown

Leave a Reply