SHARE
TWEET

Untitled

a guest Sep 11th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. ##################################################################
  3. # A Project of TNET Services, Inc
  4. #
  5. # Title:     WiFi_Check
  6. # Author:    Kevin Reed (Dweeber)
  7. #            dweeber.dweebs@gmail.com
  8. # Project:   Raspberry Pi Stuff
  9. #
  10. # Copyright: Copyright (c) 2012 Kevin Reed <kreed@tnet.com>
  11. #            https://github.com/dweeber/WiFi_Check
  12. #
  13. # Purpose:
  14. #
  15. # Script checks to see if WiFi has a network IP and if not
  16. # restart WiFi
  17. #
  18. # Uses a lock file which prevents the script from running more
  19. # than one at a time.  If lockfile is old, it removes it
  20. #
  21. # Instructions:
  22. #
  23. # o Install where you want to run it from like /usr/local/bin
  24. # o chmod 0755 /usr/local/bin/WiFi_Check
  25. # o Add to crontab
  26. #
  27. # Run Every 5 mins - Seems like ever min is over kill unless
  28. # this is a very common problem.  If once a min change */5 to *
  29. # once every 2 mins */5 to */2 ...
  30. #
  31. # */5 * * * * /usr/local/bin/WiFi_Check
  32. #
  33. ##################################################################
  34. # Settings
  35. # Where and what you want to call the Lockfile
  36. lockfile='/var/run/WiFi_Check.pid'
  37. # Which Interface do you want to check/fix
  38. wlan='wlan0'
  39. pingip='192.168.1.1'
  40. ##################################################################
  41. echo
  42. echo "Starting WiFi check for $wlan"
  43. date
  44. echo
  45.  
  46. # Check to see if there is a lock file
  47. if [ -e $lockfile ]; then
  48.     # A lockfile exists... Lets check to see if it is still valid
  49.     pid=`cat $lockfile`
  50.     if kill -0 &>1 > /dev/null $pid; then
  51.         # Still Valid... lets let it be...
  52.         #echo "Process still running, Lockfile valid"
  53.         exit 1
  54.     else
  55.         # Old Lockfile, Remove it
  56.         #echo "Old lockfile, Removing Lockfile"
  57.         rm $lockfile
  58.     fi
  59. fi
  60. # If we get here, set a lock file using our current PID#
  61. #echo "Setting Lockfile"
  62. echo $$ > $lockfile
  63.  
  64. # We can perform check
  65. echo "Performing Network check for $wlan"
  66. /bin/ping -c 2 -I $wlan $pingip > /dev/null 2> /dev/null
  67. if [ $? -ge 1 ] ; then
  68.     echo "Network connection down! Attempting reconnection."
  69.     /sbin/ifdown $wlan
  70.     /bin/sleep 5
  71.     /sbin/ifup --force $wlan
  72.     device_name=$HOSTNAME
  73.     echo $device_name
  74.     IP=`hostname -I`
  75.     echo $IP
  76.     contents="$device_name has the IP of $IP"
  77.     url=''
  78.  
  79.     generate_post_data()
  80.     {
  81.         cat << EOF
  82.         {
  83.             "content": "$contents"
  84.         }
  85.         EOF
  86.     }
  87.  
  88.     echo $(generate_post_data)
  89.  
  90.  
  91.     curl -XPOST -H "Content-type: application/json" -d "$(generate_post_data)" $url
  92.  
  93. else
  94.     echo "Network is Okay"
  95. fi
  96.  
  97. echo
  98. echo "Current Setting:"
  99. /sbin/ifconfig $wlan | grep "inet addr:"
  100. echo
  101.  
  102. # Check is complete, Remove Lock file and exit
  103. #echo "process is complete, removing lockfile"
  104. rm $lockfile
  105. exit 0
  106.  
  107. ##################################################################
  108. # End of Script
  109. ##################################################################
  110.  
  111.  
  112.  
  113. #!/bin/bash
  114. device_name=$HOSTNAME
  115. echo $device_name
  116. IP=`hostname -I`
  117. echo $IP
  118. contents="$device_name has the IP of $IP"
  119. url='https://discordapp.com/api/webhooks/621116566012166144/v5gwVC8S2j5E5UrR_vpHMrv4HgIGM9Pk-saP5gRndkvZW0JyAkIADBACAvSbVdAHz3EB'
  120.  
  121. generate_post_data()
  122. {
  123.   cat <<EOF
  124.   {
  125.     "content": "$contents"
  126.   }
  127. EOF
  128. }
  129.  
  130. echo $(generate_post_data)
  131.  
  132.  
  133. curl -XPOST -H "Content-type: application/json" -d "$(generate_post_data)" $url
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top