Following the path we were… What if we want to use pstools in 50 servers? As an idea, we can creat a shared unit, and make all servers to execute pstools there. But if we have some in some networks, some in some other networks (including DMZ), in a domain or not… Couln’t be an easy way to copy them?
With this purpose I’ve made this little script, doing exactly that: copying pstools to the server we want. First of all it mounts a cifs unit (with smbmount), then copy the files and then umount it.
I’ve made it to be called from other scripts. For instance, if we make a “winps”, we can make it to check if pstools are installed first, and to copy them if they aren’t.
In the full article you can see the code an download the file.
winvars.sh
winvars.sh
#!/bin/bash
TOOLS_DIR=tools
TOOLS_UNIT=d
CREDENTIALS="/home/user/secretfile"
SMBCREDENTIALS="credentials=$CREDENTIALS"
PSCREDENTIALS="-A $CREDENTIALS"
Difference between SMBCREDENTIALS and PSCREDENTIALS is the way smbmount and winexe accept them.
cp_pstools.sh
cp_pstools.sh
#!/bin/bash
# We get the script path
PROGPATH=echo $0 | /bin/sed -e 's,[\/][^\/][^\/]*$,,'
# We load our vars
. $PROGPATH/winvars.sh
PSTOOLS_SRC=/home/user/pstools/
RAND_DIR=$1-$RANDOM
[ $# -lt 1 ] && echo "Error, too few parameters" && echo "Use: $0 server [unit]" && exit
[ ! -z "$2" ] && SMBCREDENTIALS="username=$2,password=$3"
mkdir /tmp/$RAND_DIR
smbmount //$1/$TOOLS_UNIT$ /tmp/$RAND_DIR -o $SMBCREDENTIALS
if [ $? -eq 0 ];then
echo "Default credentials"
else
echo "Default credentials doesn't authentify. Try others."
read -p "Type username (DOMAIN/user):" user
read -s -p "Type password:" pass
smbmount //$1/$TOOLS_UNIT$ /tmp/$RAND_DIR -o username=$user,password=$pass
[ $? -ne 0 ] && echo "Credentials error or unit not available (check smbmount errors)" && rmdir /tmp/$RAND_DIR && exit 1
fi
mkdir /tmp/$RAND_DIR/$TOOLS_DIR
cp -av $PSTOOLS_SRC /tmp/$RAND_DIR/$TOOLS_DIR/.
smbumount /tmp/$RAND_DIR
rmdir /tmp/$RAND_DIR
>