Advertisement
IssyPutchy

PingPlotter

May 17th, 2022
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.93 KB | None | 0 0
  1. #!/data/data/com.termux/files/usr/bin/bash
  2.  
  3. ### Simplw color outpur
  4. NoCol='\033[0m'
  5. Black='\033[0;30m'
  6. Red='\033[0;31m'
  7. Green='\033[0;32m'
  8. Yellow='\033[0;33m'
  9. Blue='\033[0;34m'
  10. Purple='\033[0;35m'
  11. Cyan='\033[0;36m'
  12. White='\033[0;37m'
  13. BBlack='\033[1;30m'
  14. BRed='\033[1;31m'
  15. BGreen='\033[1;32m'
  16. BYellow='\033[1;33m'
  17. BBlue='\033[1;34m'
  18. BPurple='\033[1;35m'
  19. BCyan='\033[1;36m'
  20. BWhite='\033[1;37m'
  21. UBlack='\033[4;30m'
  22. URed='\033[4;31m'
  23. UGreen='\033[4;32m'
  24. UYellow='\033[4;33m'
  25. UBlue='\033[4;34m'
  26. UPurple='\033[4;35m'
  27. UCyan='\033[4;36m'
  28. UWhite='\033[4;37m'
  29.  
  30.  
  31. ### Setu0 Plotping (install packages etc)
  32. setup-plotping () {
  33. echo "OK!"
  34. }
  35.  
  36. #setup-plotping
  37.  
  38. ############# Adds average RRT per ping and total transit time
  39. fixplot () {
  40.  
  41. TMP=$(mktemp)
  42. newfile="$1_avg.plot"
  43. tavg=1
  44. ttot=0
  45. ctot=0
  46. cavg=1
  47. tail -n+2 $1 > $TMP
  48. xrange=$(cat $TMP |wc -l)
  49. lpcount=$(tail -n 1 $TMP |awk '{print $2}')
  50. rtts=$(cat $TMP |awk '{printf "%.0f + ", $5}')
  51. yrange=$(cat $TMP |awk '{print $5}' |sort -rn |head -n+1)
  52. iylow=$(cat $TMP |awk '{print $5}' |sort -n |head -n+1)
  53. ylow=$(printf "%.0f" $iylow)
  54. ttot=$(expr $rtts 0)
  55. tavg=$(expr $ttot / $lpcount)
  56.  
  57. rm -f $newfile
  58.  
  59. stime=$(cat $TMP |head -n 1 |awk '{print $1}')
  60. orig=$(cat "$1" |wc -l)
  61. run=1
  62. xrange=$(cat $TMP |wc -l)
  63.  
  64. cat $TMP |while read line
  65.     do
  66.    rtt=$(echo $line|awk '{printf "%.0f", $5}')
  67.    cping=$(echo $line |awk '{print $2}')
  68.    ctot=$(( $ctot + $rtt ))
  69.    ctots=$(echo "$ctot" | awk '{printf "%.2f", $1 / 1000}')
  70.    cavg=$(( $ctot / $cping ))
  71.    dtime=$(echo $line |awk '{print $1}')
  72.    difftime=$(( $dtime - $stime ))
  73.    sed -e "s/$/\t$cavg\t$ctots/g"  <<< $line >> $newfile
  74.    left=$(( $xrange - $run ))
  75.    proc=$(awk -vn=$xrange -vrun=$run 'BEGIN{printf "%.0f", run*(100/n)}')
  76.    echo -ne "${White}Proc:${BGreen} $proc% ${White}|| Rem:${BGreen}$left ${White} || InFlight:${BGreen}$ctots s ${White}|| Length:${BGreen}$difftime s${White}"\\r
  77.    run=$(( $run + 1 ))
  78. done
  79.  
  80. plotfile "$newfile"
  81. }
  82. ######################## End of plotfix
  83.  
  84. ### Plot the file
  85. plotfile () {
  86.  
  87. newfile="$1"
  88. yrange=$(cat $newfile |awk '{print $5}' |sort -rn |head -n 1)
  89. lavg=$(cat $newfile |awk '{print $5}' |tail -n 1)
  90. avgrtt=$(printf "%.0f" $lavg)
  91. xrange=$(cat $newfile |wc -l)
  92. PLOT="$newfile"
  93. pngfile="/sdcard/Download/$PLOT.png"
  94. echo -e "${Green} Plotting to: ${BWhite}$pngfile"
  95. echo -e -n "${White}Scaling to:${BGreen} X:$xrange/Y:$yrange/Z:$avgrtt"
  96. start=$(date +'%s')
  97.  
  98. gnuplot << EOF
  99. set style data points
  100. #set style histogram columnstacked
  101. set boxwidth 0.5
  102. unset style line
  103. set style line 1  linetype 1 linewidth 1 pointsize 1 linecolor palette
  104. set style line 2  linetype 2 linewidth 2 linecolor rgb "#222222"
  105. set style line 3  linetype 3 linewidth 2 pointsize 3 linecolor rgb "#999999"
  106. set style line 4  linetype 4 linewidth 5 pointsize 5 linecolor palette
  107. set style line 5  linetype 5 linewidth 4 pointsize -1 linecolor rgb "#dddddd"
  108. set style increment userstyles
  109. set style fill   transparent border lt -1
  110. set palette model RGB maxcolors 256
  111. set palette defined ( -1 '#77ff77', 25 '#00dd000', 50 '#ffff00', 75 '#dabd00', 100 '#ff0000', 125 '#990000' )
  112. set format x '%.0f'
  113. unset grid
  114. set grid linecolor rgb 'white'
  115. set xtics nomirror out
  116. set ytics nomirror out
  117. set border 3 back
  118. unset key
  119. #set title auto
  120. set xlabel "$yrange pings" textcolor rgb 'white'
  121. set ylabel "RTT (ms)" textcolor rgb 'white'
  122. set xrange [1:500]
  123. set yrange [0:$yrange]
  124. set xtics rotate 90
  125. set mxtics 100
  126. set tics textcolor rgb 'white'
  127. set cbrange [0:125]
  128. #set autoscale y
  129. set terminal png size 1280,720 font 'Source Sans Pro' 16 butt background '#000000'
  130. set output "$pngfile"
  131. plot "$PLOT" u 2:6:6 with lines ls 0.25 palette cb $avgrtt, "" u 2:5:5 with boxes palette z lw 0.25
  132. EOF
  133.  
  134. end=$(date +'%s')
  135. echo -ne "${BWhite} done in " $(awk -va=$start -vb=$end 'BEGIN{print (a - b)}')
  136. echo " seconds."
  137. echo -e "${Green} Updated plot file:${BWhite} $newfile"
  138. echo -e "${Green} PNG location:${BWhite} $pngfile"
  139. }
  140. ###### End of plot functi9n
  141.  
  142. ####### Ping plot function
  143. plotpings () {
  144. OUT="$1.plot"
  145. echo -e "${Green} Plotting to:${BWhite} $OUT"
  146. ping -c $2 -W 1s -i 1 "$1" \
  147.  |sed -u -e 's/(\|):\|ttl=\|time=//g' \
  148.  | grep "bytes from" \
  149.  |awk 'BEGIN {print "Time\t\tPing\t\tHost\t\tTTL\t  RTT";}
  150.  {srand(); print srand(),"\t", NR,"\t",$5,"\t",$7,"\t",$8;}' \
  151.   > $OUT
  152.    echo "Done! Adding additional data, may take a while..."
  153.    fixplot "$OUT"
  154. }
  155.  
  156. if [ "$1" = "" ]; then
  157.  echo "Malfunction! Need input!"
  158.   exit 1
  159.  else
  160.   if [ "$2" == "" ] && [ -f "$1" ]; then
  161.     echo "File found!"
  162.     cols="$(tail -n 1 "$1" |wc -w)"
  163.       if [ "$cols" = "5" ]; then
  164.          echo "Processing plot file"
  165.      fixplot "$1"
  166.        elif [ "$cols" = "7" ]; then
  167.          echo "Plot file already updated!"
  168.      plotfile "$1"
  169.        else
  170.         echo "Yup, I didn't like that!"
  171.         exit 1
  172.       fi
  173.    elif [ $2 > 14 ]; then
  174.      echo "Attempting to plot pings on $1 for $2 seconds"
  175.      plotpings "$1" "$2"
  176.   fi
  177. fi
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement