Guest User

Untitled

a guest
Aug 15th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.44 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4. set -u
  5. #set -x
  6.  
  7. if [ $# -lt 3 -o $# -gt 6 ]; then
  8.   echo "Usage: $0 username password host [query_port=10011] [output_file=snapshot.txt] [virtual server ID=1]"
  9.   exit -1
  10. fi
  11.  
  12. USER="$1"
  13. PASS="$2"
  14. HOST="$3"
  15. PORT="10011"
  16. OUTPUT_FILE="snapshot.txt"
  17. SID=1
  18. if [ $# -gt 3 ]; then
  19.   PORT="${4}"
  20. fi
  21. if [ $# -gt 4 ]; then
  22.   OUTPUT_FILE="${5}"
  23. fi
  24. if [ $# -gt 5 ]; then
  25.   SID="${6}"
  26. fi
  27.  
  28. if [ -e ${OUTPUT_FILE} ]; then
  29.   echo "output file alread exists, aborting"
  30.   exit -2
  31. fi
  32. touch "${OUTPUT_FILE}"
  33. ( echo "use ${SID}"; echo "login ${USER} ${PASS}"; echo "serversnapshotcreate"; echo "quit" ) | nc ${HOST} ${PORT} | tr -d "\r" | grep . > ${OUTPUT_FILE}
  34.  
  35. if [ ! "$(wc -l ${OUTPUT_FILE} | sed 's/ .*//')" = "7" ]; then
  36.   echo "Invalid number of lines from snapshot retrieval, aborting"
  37.   echo "View ${OUTPUT_FILE} for details"
  38.   exit -3
  39. fi
  40.  
  41. line_number=0
  42. while IFS=$'\n' read -r "line"; do
  43.   if [ "$line_number" == "0" ]; then
  44.     if [ ! "$line" = "TS3" ]; then  
  45.       echo "Invalid greeting line, looking for \"TS3\", got ${line}"
  46.       exit -4
  47.     fi
  48.   elif [ "$line_number" == "1" ]; then
  49.     if [ ! "$line" = 'Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.' ]; then
  50.       echo "Invalid greeting line, looking for \"This is the TeamSpeak3 Server Query\"...."
  51.       exit -5
  52.     fi  
  53.   elif [ "$line_number" == "2" ]; then
  54.     if [ ! "$line" = "error id=0 msg=ok" ]; then
  55.       echo "Didnt get error ok on \"use 1\", got ${line}"
  56.       exit -6
  57.     fi
  58.   elif [ "$line_number" == "3" ]; then
  59.     if [ ! "$line" = "error id=0 msg=ok" ]; then
  60.       echo "Didnt get error ok on \"login serveradmin password\", got ${line}"
  61.       exit -7
  62.     fi
  63.   elif [ "$line_number" == "4" ]; then
  64.     echo "$line" > real_${OUTPUT_FILE}
  65.   elif [ "$line_number" == "5" ]; then
  66.     if [ ! "$line" = "error id=0 msg=ok" ]; then
  67.       echo "Didnt get ok on serversnapshotcreate, got ${line}"
  68.       exit -8
  69.     fi
  70.   elif [ "$line_number" == "6" ]; then
  71.     if [ ! "$line" = "error id=0 msg=ok" ]; then
  72.       echo "Didnt get ok on \"quit\", got ${line}"
  73.       exit -10
  74.     fi
  75.   else
  76.     echo "snapshot.txt file being written to as we speak? Something is very wrong here..."
  77.     exit -9
  78.   fi
  79.   ((++line_number))
  80. done < "${OUTPUT_FILE}"
  81.  
  82. mv real_${OUTPUT_FILE} ${OUTPUT_FILE}
  83.  
  84. echo "Successfully retrieved the image, it is now in ${OUTPUT_FILE}"
Add Comment
Please, Sign In to add comment