Advertisement
Terosami

ping_test.sh

Apr 16th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.96 KB | None | 0 0
  1. #/bin/bash
  2. #Beschreibung
  3. # Script für Randome Ping Test
  4. #by Gregor holzfeind <gholzfeind@heiniger-ag.ch>
  5. #Version: 0.2
  6. #Datum: 2018-04-16
  7.  
  8. #History
  9. #0.1 2018.04.16
  10. #   * Basisfunktionen erstellt
  11. #   * Umstellung auf whiptail
  12. #   * Funktionstest mit Endzeit und Durchgängen
  13. #0.2 2018.04.17
  14. #   * Sleep mit Fortschrittsbalken erweitert
  15. #   * Funktionstest mit Dauer
  16. #   * Ping mit Fortschrittsbalken erweitert
  17.  
  18.  
  19. # Funktionen
  20. function func_sleep_cale() {
  21.     start_d=$(date +%s)
  22.     zufall=$(echo $(($RANDOM % $wait + 1)))
  23.     end_s=$(echo "$start_d +$zufall" | bc)
  24.     func_check
  25. }
  26.  
  27. function func_sleep() {
  28.     sleep=$(echo "scale=5; $zufall / 100" | bc )
  29.     {
  30.         for ((i = 0 ; i <= 100; i+=1)); do
  31.             sleep $sleep
  32.             echo $i
  33.         done
  34.     } | whiptail --backtitle "Ping-Performant-Test" --gauge "Speer für den nächsten Test\nBitte warten..." 8 50 0
  35.     func_ping_gaug & func_ping
  36. }
  37.  
  38. function func_start() {
  39.     TERM=vt220 whiptail --backtitle "Ping-Performant-Test" --infobox "Willkommen zum Ping-performant-Tests\n\nEs werden nun die benötigten Informationen eingestellt" 8 78
  40.     sleep 2
  41.     type=$(whiptail --backtitle "Ping-Performant-Test"  --radiolist "Bitte Wählen Sie die Ziel-Bedinungen" 20 78 3 "Run" "Anzahl Durchgänge" ON   "Date" "Datum und Uhrzeit" OFF "Time" "Nach gewissen Zeitdauer" OFF 3>&1 1>&2 2>&3)
  42.     exitstatus=$?
  43.     if [ $exitstatus != 0 ]
  44.     then
  45.         func_cancel
  46.     fi
  47.     case $type in
  48.         Run)
  49.             max_run=$(whiptail --inputbox "Bitte Anzahl Durchgänge angeben" 8 78 10 --backtitle "Ping-Performant-Test"  3>&1 1>&2 2>&3)
  50.             exitstatus=$?
  51.             if [ $exitstatus != 0 ]
  52.             then
  53.                 func_cancel
  54.             fi
  55.             ;;
  56.         Date)
  57.             date_a=$(date +%Y.%m.%d)
  58.             time_a=$(date +%H:%M)
  59.             end_date=$(whiptail --inputbox "Bitte Endatum angeben\nNach dem Muster YYYY.MM.DD" 8 78 $date_a --backtitle "Ping-Performant-Test" 3>&1 1>&2 2>&3)
  60.             exitstatus=$?
  61.             if [ $exitstatus != 0 ]
  62.             then
  63.                 func_cancel
  64.             fi
  65.             end_time=$(whiptail --inputbox "Bitte Enzeit angeben\nNach dem Muster HH:MM" 8 78 $time_a --backtitle "Ping-Performant-Test" 3>&1 1>&2 2>&3)
  66.             exitstatus=$?
  67.             if [ $exitstatus != 0 ]
  68.             then
  69.                 func_cancel
  70.             fi
  71.             end_date=$(echo $end_date | tr . -)
  72.             end_date=$(date --date="$end_date $end_time" +%s)
  73.             ;;
  74.         Time)
  75.             time_r=$(whiptail --inputbox "Bitte Laufzeit in Minuten angeben" 8 78 10 --backtitle "Ping-Performant-Test" 3>&1 1>&2 2>&3)
  76.             exitstatus=$?
  77.             if [ $exitstatus != 0 ]
  78.             then
  79.                 func_cancel
  80.             fi
  81.             time_s=$(date +%s)
  82.             time_r=$(echo "$time_r * 60 + $time_s" | bc)
  83.             ;;
  84.     esac
  85.     ip_add=$(whiptail --inputbox "Bitte IP-Adresse angegebn" 8 78 192.168.1.1 --backtitle "Ping-Performant-Test" 3>&1 1>&2 2>&3)
  86.     exitstatus=$?
  87.             if [ $exitstatus != 0 ]
  88.             then
  89.                 func_cancel
  90.             fi
  91.     log=$(whiptail --inputbox "Bitte Log-Datei angeben" 8 78 ping_test.log --backtitle "Ping-Performant-Test" 3>&1 1>&2 2>&3)
  92.     exitstatus=$?
  93.             if [ $exitstatus != 0 ]
  94.             then
  95.                 func_cancel
  96.             fi
  97.     wait=$(whiptail --inputbox "Bitte max. Wartezeit (in Minuten)" 8 78 60 --backtitle "Ping-Performant-Test" 3>&1 1>&2 2>&3)
  98.     exitstatus=$?
  99.             if [ $exitstatus != 0 ]
  100.             then
  101.                 func_cancel
  102.             fi
  103.     wait=$(echo "$wait * 60" | bc)
  104.     echo "Datum;Uhrzeit;Gesendet;Empfangen;Verloren;min. Dauer; max. Dauer;durchschnittliche Dauer" > $log
  105.     func_ping_gaug & func_ping
  106. }
  107.  
  108. function func_ping() {
  109.     zufall=$(echo $(($RANDOM % 20 + 1)))
  110.     ping -c $zufall $ip_add > temp.$$
  111.     date=$(date +%x)
  112.     time=$(date +%X)
  113.     send=$(cat temp.$$ | tail -n2 | head -n1 | cut -d"," -f1 | cut -d" " -f1)
  114.     rec=$(cat temp.$$ | tail -n2 | head -n1 | cut -d"," -f2 | cut -d" " -f2)
  115.     lost=$(cat temp.$$ | tail -n2 | head -n1 | cut -d"," -f3 | cut -d" " -f2)
  116.     max=$(cat temp.$$ | tail -n1 | cut -d"=" -f2 | cut -d"/" -f3)
  117.     min=$(cat temp.$$ | tail -n1 | cut -d"=" -f2 | cut -d" " -f2 | cut -d"/" -f1)
  118.     avg=$(cat temp.$$ | tail -n1 | cut -d"=" -f2 | cut -d"/" -f2)
  119.     echo "$date;$time;$send;$rec;$lost;$min;$max;$avg" >> $log
  120.     rm temp.$$
  121. }
  122.  
  123. function func_ping_gaug() {
  124.     {
  125.     for ((i = 0 ; i <= 100; i+=5)); do
  126.         sleep 0.5
  127.         echo $i
  128.     done
  129.     } | whiptail --backtitle "Ping-Performant-Test" --gauge "Ping wird durchgeführt\nBitte warten..." 8 50 0
  130.     func_sleep_cale
  131. }
  132.  
  133. function func_check() {
  134.     if [ "$type" == "Date" ]
  135.     then
  136.         if [ "$end_s" -ge "$end_date" ]
  137.         then
  138.             func_end
  139.         else
  140.             func_sleep
  141.         fi
  142.     elif [ "$type" == "Run" ]
  143.     then
  144.         if [ "$max_run" -eq "1" ]
  145.         then
  146.             func_end
  147.         else
  148.             max_run=$(echo "$max_run - 1" | bc)
  149.             func_sleep
  150.         fi
  151.     elif [ "$type" == "Time" ]
  152.     then
  153.         if [ "$end_s" -ge "$time_r" ]
  154.         then
  155.             func_end
  156.         else
  157.             func_sleep
  158.         fi
  159.     fi
  160. }
  161.  
  162. function func_end() {
  163.     TERM=vt220 whiptail --infobox "Die Test sind abgeschlossen\nEs wurde folgende Log-Datei erstellt: $log" --backtitle "Ping-Performant-Test"   8 78
  164.     sleep 5
  165.     clear
  166.     exit
  167. }
  168.  
  169. function func_cancel() {
  170.     TERM=vt220 whiptail --infobox "Das Programm wurde abgebrochen\nEs werden alle vorhanden Daten gelöscht." --backtitle "Ping-Performant-Test"   8 78
  171.     sleep 5
  172.     clear
  173.     rm -f temp.$$ $log
  174.     exit
  175. }
  176.  
  177. func_start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement