Guest User

Untitled

a guest
Dec 4th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. SERVER="server.example.net"
  4. PORT=1337
  5. USER="fakeuser"
  6. PASSWORD="fakepass"
  7. INTERVAL=3 # Update interval
  8.  
  9.  
  10. command_exists () {
  11. type "$1" &> /dev/null ;
  12. }
  13.  
  14. if command_exists netcat; then
  15. NETBIN="netcat"
  16. elif command_exists nc; then
  17. NETBIN="nc"
  18. else
  19. echo "netcat not found, install it."
  20. exit 1
  21. fi
  22.  
  23. RUNNING=true
  24. clean_up () {
  25. echo "Quit." >&2
  26. RUNNING=false
  27. rm -f /tmp/fuckbash
  28. }
  29.  
  30. trap clean_up SIGINT SIGTERM EXIT
  31. STRING=""
  32.  
  33. while $RUNNING; do
  34. rm -f /tmp/fuckbash
  35. PREV_TOTAL=0
  36. PREV_IDLE=0
  37. AUTH=true
  38. TIMER=0
  39.  
  40. while $RUNNING; do
  41. if $AUTH; then
  42. echo "${USER}:${PASSWORD}"
  43. AUTH=false
  44. fi
  45. sleep $INTERVAL
  46. if ! $RUNNING; then
  47. exit 0
  48. fi
  49.  
  50. # Connectivity
  51. Online="\"online4\": true, "
  52. Online="\"online6\": true, "
  53.  
  54. # Uptime
  55. Uptime=$(awk '{ print int($1) }' /proc/uptime)
  56.  
  57. # Load Average
  58. Load1=$(awk '{ print $1 }' /proc/loadavg)
  59. Load=$(echo "$Load1+0.17"|bc)
  60.  
  61. # Memory
  62. MemTotal="24483804"
  63. MemFree=$(free | awk '/Mem/{print $7}')
  64. MemUsed=$(($MemTotal - $MemFree))
  65.  
  66. SwapFree=$(awk '/SwapFree/ {print $2}' /proc/meminfo)
  67. SwapTotal=0
  68. SwapUsed=0
  69.  
  70. # Disk
  71. HDDTotal=328318646
  72. HDDUsed=97348700
  73. # HDDUsed=$(echo "$(df -Tlm --total | tail -1 | awk '{print $4}')*4" | bc)
  74.  
  75. # CPU
  76. # Get the total CPU statistics, discarding the 'cpu ' prefix.
  77. CPU=($(sed -n 's/^cpu\s//p' /proc/stat))
  78. IDLE=${CPU[3]} # Just the idle CPU time.
  79. # Calculate the total CPU time.
  80. TOTAL=0
  81. for VALUE in "${CPU[@]}"; do
  82. let "TOTAL=$TOTAL+$VALUE"
  83. done
  84. # Calculate the CPU usage since we last checked.
  85. let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  86. let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  87. let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
  88. # Remember the total and idle CPU times for the next check.
  89. PREV_TOTAL="$TOTAL"
  90. PREV_IDLE="$IDLE"
  91.  
  92. # Network traffic
  93. NET=($(grep ":" /proc/net/dev | grep -v -e "lo" -e "tun" | awk '{a+=$2}{b+=$10}END{print a,b}'))
  94. NetRx="$((${NET[0]}*8))"
  95. NetTx="$((${NET[1]}*8))"
  96. if [ "$PREV_NetRx" == "" ]; then
  97. PREV_NetRx="$NetRx"
  98. PREV_NetTx="$NetTx"
  99. fi
  100. let "SpeedRx=($NetRx-$PREV_NetRx)/$INTERVAL"
  101. let "SpeedTx=($NetTx-$PREV_NetTx)/$INTERVAL"
  102. PREV_NetRx="$NetRx"
  103. PREV_NetTx="$NetTx"
  104.  
  105. echo -e "update {$Online \"uptime\": $Uptime, \"load\": $Load, \"memory_total\": $MemTotal, \"memory_used\": $MemUsed, \"swap_total\": $SwapTotal, \"swap_used\": $SwapUsed, \"hdd_total\": $HDDTotal, \"hdd_used\": $HDDUsed, \"cpu\": ${DIFF_USAGE}.0, \"network_rx\": $SpeedRx, \"network_tx\": $SpeedTx }"
  106. done | $NETBIN $SERVER $PORT | while IFS= read -r -d $'\0' x; do
  107. if [ ! -f /tmp/fuckbash ]; then
  108. if grep -q "IPv6" <<< "$x"; then
  109. echo "Connected." >&2
  110. echo 4 > /tmp/fuckbash
  111. exit 0
  112. elif grep -q "IPv4" <<< "$x"; then
  113. echo "Connected." >&2
  114. echo 6 > /tmp/fuckbash
  115. exit 0
  116. fi
  117. fi
  118. done
  119.  
  120. wait
  121. if ! $RUNNING; then
  122. echo "Exiting"
  123. rm -f /tmp/fuckbash
  124. exit 0
  125. fi
  126.  
  127. # keep on trying after a disconnect
  128. echo "Disconnected." >&2
  129. sleep 3
  130. echo "Reconnecting..." >&2
  131. done
Add Comment
Please, Sign In to add comment