Next mission was making scripts to execute pstools remotely. I started make one for each tool, but I found out there was a lot of shared code, so I decided to create an generic script, psexec.sh (honoring pstools), receiving the server and the tool, with its parameters. After that, we should only create a wrapper for every command to make our life easier.
The script must check if file credentials are valid, and asking for others if they aren’t. Once authenticated, it must check if there’s pstools installed or not, and copy them if not.
In the full story you can see the code of psexec.sh and an example wrapper, winps.sh. Keep in mind that they need some files, winvars.sh and cp_pstools.sh in order to work properly, as we saw in the rc=$?
if [ $rc -eq 0 ];then
  echo "Default credentials"
elif [ $rc -eq 1 ];then
  echo "$? Hola"
  echo "Default credentials doesn't authentify. Try others."
  read -p "Type username (DOMAIN/user):" user
  read -s -p "Type password:" pass
  PSCREDENTIALS="--user $user --password $pass"
  winexe //$1 "$TOOLS_UNIT:\$TOOLS_DIR\pstools\pslist.exe /accepteula -t" $PSCREDENTIALS
  rc=$?
  if [ $rc -eq 1 ];then
    echo "Credentials error or unit not available (check smbmount errors)" && exit 1
  elif [ $rc -eq 99 ];then
    echo "There's no pstools, I'll copy them"
    $PROGPATH/cp_pstools.sh $1 $user $pass
    [ $? -ne 0 ] && echo "There's no pstools and I couldn't copy them." && exit 1
    winexe //$1 "d:\scripts\pstools\pslist.exe /accepteula -t" $PSCREDENTIALS
    rc=$?
    if [ $rc -eq 99 ];then
      echo "Pstools are copied, but they don'y work, somethings going on." && exit 1
    fi
  fi
elif [ $rc -eq 99 ];then
  echo "There's no pstools, I'll copy them"
  $PROGPATH/cp_pstools.sh $1
  [ $? -ne 0 ] && echo "There's no pstools and I couldn't copy them." && exit 1
  winexe //$1 "d:\scripts\pstools\pslist.exe /accepteula -t" $PSCREDENTIALS
  rc=$?
  if [ $rc -eq 99 ];then
      echo "Pstools are copied, but they don'y work, somethings going on." && exit 1
  fi
fi
Wrapper example: winps.sh
#!/bin/bash
[ $# -ne 1 ] && echo "Error, I need one and only one argument" && exit 1
PROGPATH=echo $0 | /bin/sed -e 's,[\/][^\/][^\/]*$,,'
$PROGPATH/psexec.sh $1 pstools\pslist -t
