bengarrett

Linux Bash Shell Script For Simplifying Usage In Terminal

Recommended Posts

I created a bash script that I thought some Linux shell users of BitTorrentSync might find useful. It is designed to simplify some of the interactions with btsync when using it from the terminal. With it you can ...

  • Quickly edit the BitTorrent Sync configuration file.
  • Restart the daemon to apply any new configurations.
  • Display the configuration file on screen.
  • Display the tail of the log file on screen, both static and as it grows.
  • Trigger the debug logging mode.
  • Generate combined write and read only secrets.
  • Display the version of the daemon.

GitHub: https://github.com/bengarrett/btsynctool

Readme & screenshots: https://github.com/bengarrett/btsynctool/blob/master/README.md

 

Any suggestions, bug reports, etc welcomed.

#!/bin/bash# /usr/local/bin/btsynctool version 1.10# https://github.com/bengarrett/btsynctool## A number of short-cuts to interact with the BitTorrent Sync daemon.# Tested on Ubuntu 14.04 and BitTorrent Sync 1.3.*.# Optional support for ANSI colour output using the source-highlight package.# Use sudo apt-get install source-highlight to install.# Change these values to match your BitTorrent Sync installation# Path to the program or binary fileDAEMON=/opt/btsync/btsync# Path to the configuration fileCONFIG=/opt/btsync/btsync.conf# Path to the .sync settings folderSYNC=/opt/btsync/.sync# Path to the log fileLOG=$SYNC/sync.log# Path to your favourite text editorEDITOR=/usr/bin/nano# The following values should not be changed# The description and the process nameDESC="BitTorrent Sync"PROCESS="btsync"# BitTorrent Sync debug mode trigger file and body DEBUGFILE=$SYNC/debug.txtDEBUGTRIGGER="FFFF"# Script nameBASE="$(basename $0)"# getopt arguments# Tutorial http://linuxaria.com/howto/parse-options-in-your-bash-script-with-getoptPARSED_OPTIONS=$(getopt -n "$0" -o hscCrlL:fF:dDv --long "help,generate-secrets,config,restart,log::,follow,debug-logging,debug-off,version" -- "$@")# Bad arguments, something has gone wrong with getopt command.if [ $? -ne 0 ]; then  exit 1fi# Requirement for getopteval set -- "$PARSED_OPTIONS"# Check the existence of the btsync daemonif [ ! -f $DAEMON ]; then  echo "$BASE: could not find the daemon at $DAEMON"  exit 1fi# Check for ANSI colour support# http://superuser.com/questions/323930/how-to-check-terminal-color-capability-from-command-linecolorTerm() { if which tput > /dev/null 2>&1 && [[ $(tput -T$TERM colors) -ge 8 ]]; then  return 0 else  return 1 fi}# Check if source-highlight is installedsourceHighlight() { if hash source-highlight 2>/dev/null; then   return 0 else   return 1 fi}# Check if BitTorrent Sync is in debug logging modedebugLogging() {  # check if $DEBUGFILE exists  if [ -f "$DEBUGFILE" ]; then    # save content of $DEBUGFILE to local variable    local debugbody=$(<$DEBUGFILE)    # compare content of $DEBUGFILE to $DEBUGTRIGGER    if [ "$debugbody" = "$DEBUGTRIGGER" ]; then      return 0    else      return 1    fi else  return 1 fi}# Restart BitTorrent SyncrestartDaemon() {  local processid=$(pgrep -x $PROCESS)  echo "Restart $DESC:"  if [ ! $processid = "" ]; then    local args=$(ps -C $PROCESS --no-headers -o cmd)    echo "Running as process '$PROCESS'. pid = $processid"    echo "Will restart $PROCESS using the following arguments"    echo "$args"    echo ""    echo "* Stopping $DESC daemon $PROCESS"    pkill -9 -e -x $PROCESS    if ps $processid > /dev/null; then      echo "Could not shutdown $PROCESS"      echo "Maybe there is a permissions problem?"    else      echo "* Starting $DESC daemon $PROCESS"      echo ""      $args    fi  else    echo "$DESC is not running so it will be started in daemon mode"    echo ""    $DAEMON --config $CONFIG  fi}# Process script argumentscase "$1" in -h|--help)  printf "Tools to interact with the $DESC daemon."  printf "\nUsage:"  printf "\n  $BASE [OPTION]"  printf "\n\nOptions:"  printf "\n  -c, --config\t\t\t Edits the $DESC configuration file. *"  printf "\n  -C\t\t\t\t Prints the $DESC configuration file."  printf "\n  -r, --restart\t\t\t Restarts $DESC, needed to apply any configuration changes."  printf "\n  -l, --log\t\t\t Prints the last 10 lines of the log file."  printf "\n  -L=K\t\t\t\t Prints the last K lines of the log file."  printf "\n  -f, --follow\t\t\t Prints the last 10 lines of the log file and append data as the file grows."  printf "\n  -F=K\t\t\t\t Prints the last K lines of the log file and append data as the file grows."  printf "\n  -d  --debug-logging\t\t Switches $DESC into debug logging mode. *"  printf "\n  -D  --debug-off\t\t Switches $DESC into standard logging mode. *"  printf "\n  -s, --generate-secrets\t Generate a new write secret and display its read secret."  printf "\n  -v, --version\t\t\t Displays the version and process of $DESC."  printf "\n  -h, --help\t\t\t Print this message and exit."  printf "\n\n\t\t\t\t * $DESC will need to be restarted before the changes take effect.\n"  ;; -c|--config)  echo "Configure $DESC:"  $EDITOR "$CONFIG"  echo "It will need to be restarted before any saved changes take effect."  ;; -C)  echo "Configuration for $DESC:"  if colorTerm && sourceHighlight; then   source-highlight -f esc -s js -i $CONFIG | cat  else   cat "$CONFIG"  fi  ;; -r|--restart)  restartDaemon  ;; -l|--log)  echo "Print the last 10 lines of log:"  if colorTerm && sourceHighlight; then   source-highlight -f esc -s syslog -i $LOG | tail  else   tail $LOG  fi  ;; -L)  echo "Print the last $2 lines of log:"  if colorTerm && sourceHighlight; then   source-highlight -f esc -s syslog -i $LOG | tail -n $2 $LOG  else   tail -n $2 $LOG  fi  ;; -f|--follow)  echo "Print and follow 10 lines of log:"  echo "Press CTRL-C to exit"  if colorTerm && sourceHighlight; then   source-highlight -f esc -s syslog -i $LOG | tail -f $LOG  else   tail -f $LOG  fi  ;; -F)  echo "Print and follow $2 lines of log:"  echo "Press CTRL-C to exit"  if colorTerm && sourceHighlight; then   source-highlight -f esc -s syslog -i $LOG | tail -f -n $2 $LOG  else   tail -f -n $2 $LOG  fi  ;; -d|--debug-logging)  echo "Switch into debug logging mode:"  if debugLogging; then    echo "$DESC is already in debug logging mode"  else    echo "$DEBUGTRIGGER" > $DEBUGFILE    if debugLogging; then      echo "Now in debug logging mode"      echo "$DESC will need to be restarted before any saved changes take effect"    else      echo "Could not write '$DEBUGTRIGGER' to the file $DEBUGFILE"      echo "Maybe there is a permissions problem?"    fi  fi  ;; -D|--debug-off)  echo "Switch out of debug logging mode:"  : > $DEBUGFILE  if ! debugLogging; then    echo "Now in normal logging mode"    echo "$DESC will need to be restarted before any saved changes take effect"  else    echo "Could not write to the file $DEBUGFILE"    echo "Maybe there is a permissions problem?"  fi  ;; -s|--generate-secrets)  printf "Generate folder secret key:"  WSECRET=$($DAEMON --generate-secret)  RSECRET=$($DAEMON --get-ro-secret $WSECRET)  printf "\nFull access secret \t$WSECRET"  printf "\nRead only secret \t$RSECRET"  printf "\n"  ;; -v|--version)  $DAEMON --help | head -1  ps -C btsync -f  ;; *) echo "$BASE: invalid usage" echo "Try '$BASE --help' or '$BASE -h' for more information."  exit 1 ;;esacexit 0
Edited by bengarrett

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now