fant0men

runnet_sender.sh

Nov 1st, 2019 (edited)
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # This script is meant to send commands over the network to my laptop,
  3. # which will then run those commands. The output will be displayed on
  4. # screen.
  5.  
  6. #dir1='/run/user/1000/gvfs/smb-share:server=buddha-hp-compaq-nw8440-rn039aw-ak8.local,share=buddha'
  7. dir1='/home/lucifer/buddha'
  8. dir2="${dir1}/run"
  9. bn='run_commands'
  10. list="${dir2}/${bn}_list.txt"
  11. stdout="${dir2}/${bn}_stdout.log"
  12. switch_f="${dir2}/${bn}_switch.txt"
  13.  
  14. count=0
  15. run=0
  16.  
  17. declare -a commands
  18.  
  19. # trap ctrl-c and call ctrl_c()
  20. trap ctrl_c INT
  21.  
  22. ctrl_c () {
  23.     echo "** Trapped CTRL-C"
  24.     exit
  25. }
  26.  
  27. # Creates the 'switch' function, which will wait if $switch_f is
  28. # set to '1'.
  29. switch () {
  30.     switch=$(cat "$switch_f")
  31.  
  32.     while [[ $switch -eq 1 ]]; do
  33.         sleep 1
  34.  
  35.         switch=$(cat "$switch_f")
  36.     done
  37. }
  38.  
  39. # Creates a function called 'spin', which will show a simple animation,
  40. # while waiting for the command output.
  41. spin () {
  42.     spinner=( '   ' '.  ' '.. ' '...' )
  43.  
  44.     while true; do
  45.         for s in "${spinner[@]}"; do
  46.             echo -ne "\rWait${s}"
  47.             sleep 0.5
  48.         done
  49.     done
  50. }
  51.  
  52. if [[ ! -f $list ]]; then
  53.     echo "Receiver list doesn't yet exist!"
  54.     exit
  55. fi
  56.  
  57. while true; do
  58.     echo -e "\nStart typing commands, and finish with 'run'!\n"
  59.  
  60.     until [[ $run -eq 1 ]]; do
  61.         read tmp
  62.  
  63.         if [[ $tmp != 'run' ]]; then
  64.             commands[${count}]="$tmp"
  65.         else
  66.             run=1
  67.         fi
  68.  
  69.         let count++
  70.     done
  71.  
  72.     count=0
  73.     run=0
  74.  
  75.     for (( i = 0; i < ${#commands[@]}; i++ )); do
  76.         command="${commands[${i}]}"
  77.  
  78.         if [[ -z $command ]]; then
  79.             continue
  80.         fi
  81.  
  82.         #stdbuf -o 0 echo "$command" >&3
  83.         echo "$command" >> "$list"
  84.  
  85.         clear
  86.  
  87.         spin &
  88.         spin_pid="$!"
  89.  
  90.         # Waiting for the receiver script to get a chance to
  91.         # flip the switch variable. Otherwise, output will be displayed
  92.         # too quickly.
  93.         sleep 0.1
  94.    
  95.         switch
  96.  
  97.         sync
  98.  
  99.         kill "$spin_pid"
  100.  
  101.         clear
  102.  
  103.         less +G "$stdout"
  104.  
  105.         unset -v command
  106.  
  107.     done
  108.  
  109.     unset -v commands
  110. done
  111.  
RAW Paste Data