Advertisement
DamagedDolphin

Untitled

May 4th, 2020
2,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. mport os
  2. import socket, struct
  3.  
  4. from colorOptions import *
  5. from fontOptions import *
  6. from renderImg import renderImg
  7. from runImages import *
  8.  
  9. ### Get IP address of gateway / router
  10. def localGateway():
  11.     with open("/proc/net/route") as file:
  12.         for line in file:
  13.             fields = line.strip().split()
  14.             if fields[1] != '00000000' or not int(fields[3], 16) & 2:
  15.                 continue
  16.             return socket.inet_ntoa(struct.pack("<L", int(fields[2], 16)))
  17.  
  18. ### Call this module to run program
  19. def networkCheck():
  20.     global gatewayIP
  21.     global hostName
  22.     global dnsIP
  23.  
  24.     gatewayIP = localGateway()
  25.     hostName = "connectivitycheck.gstatic.com"
  26.     dnsIP = "172.217.4.131"
  27.  
  28.     networkTest(gatewayIP, "gateway")
  29.     networkTest(dnsIP, "internet")
  30.     networkTest(hostName, "dns")
  31.  
  32. ### This performs a series of ping tests against the addresses and should return a result of 0
  33. ### Any non 0 result is an error and it will continue to attempt to restart the network interface
  34. ### If after 5 attempts the network does not come back up a reboot will be initiated
  35. def networkTest(hostAddress, testName):
  36.     testResponse = 1
  37.     failCount = 0
  38.     while testResponse is not 2:
  39.         testResponse = os.system("ping -c 1 " + hostAddress)
  40.         if testResponse is not 2:
  41.             if failCount is 5:
  42.                 rebootSystem()
  43.             errorMessage(testName)
  44.             os.system('/sbin/ip link set eth0 down && /sbin/ip link set eth0 up')
  45.             failCount = failCount + 1
  46.     failCount = 0
  47.  
  48. ### This scrolls the error message of what part of the network is not responding based on the
  49. ### results of the ping test.
  50. def errorMessage(errorMessage):
  51.     if errorMessage is "dns":
  52.         errorMessage = "The DNS is down ... please check the DNS server.      "
  53.     if errorMessage is "internet":
  54.         errorMessage = "The Internet is down ... please check the router and modem.      "
  55.     if errorMessage is "gateway":
  56.         errorMessage = "The local network is down ... please check the router and modem.      "
  57.     renderImg("networkError.png", errorMessage, newsFont, backgroundColor, 0, 0)
  58.     scrollImgDisplay('networkError.png', 40)
  59.  
  60. ### This scrolls a message twice a reboot is about to occur and then forces a reboot
  61. def rebootSystem():
  62.     networkError = "The network did not come back up - attempting to reboot ... please stand by          "
  63.     renderImg("networkError.png", networkError, newsFont, backgroundColor, 0, 0)
  64.     for x in range(0, 2):
  65.         scrollImgDisplay('networkError.png', 40)
  66.     os.system('/sbin/shutdown -r -f now')
  67.  
  68. ### This is temporary for testing outside of the rest of the program
  69. networkCheck()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement