Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. import socket
  2. import sys
  3. from subprocess import DEVNULL, Popen, PIPE
  4. from time import sleep
  5. import utils
  6. from utils import get_status, is_pingable
  7. from utils import send_talent_mail
  8. from utils import local_host
  9.  
  10. print(sys.argv)
  11.  
  12. try:
  13.     try_whois = Popen(["whois"], stdout=PIPE, stderr=DEVNULL)
  14.     print("WHOIS command was found!")
  15. except FileNotFoundError:
  16.     print("WHOIS command was not found. You need to install whois command on your distro")
  17.     exit(2)
  18.  
  19.  
  20. WAITING = 5
  21. TIMER = 10
  22. HOSTNAME_DNS = "8.8.8.8"
  23. HOSTNAME_DNS_PORT = 80
  24. FILE_ADDRESSES = "ip_addresses"
  25.  
  26. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  27. s.connect((HOSTNAME_DNS, HOSTNAME_DNS_PORT))
  28. my_ip = s.getsockname()[0]
  29. print("current host: " + str(my_ip))
  30. s.close()
  31. file = open(FILE_ADDRESSES, "r")  # read the files with the name of host
  32. lines = file.readlines()
  33. file.close()
  34.  
  35.  
  36. def broadcast():
  37.     down = 0
  38.     while True:
  39.         print(utils.HEADER + "Starting scan all ips..." + utils.ENDC)
  40.         sleep(WAITING)  # waiting some seconds
  41.  
  42.         counter = 0
  43.         for host in lines:
  44.             ip = host.strip()
  45.             if not is_pingable(ip):
  46.                 counter += 1  # how many hosts are down
  47.                 down_host = {str(ip): counter}
  48.         down += 1  # how many time the the loop is repeated
  49.  
  50.         for host in lines:
  51.             ip = host.strip()  # delete "\n" and other some shits in the strings
  52.             try:
  53.                 name_host = socket.gethostbyname(ip)
  54.             except socket.gaierror:  # If you can't ping, hostname won't exist...
  55.                 name_host = host
  56.  
  57.             if local_host(name_host):
  58.                 if is_pingable(ip):
  59.                     send_talent_mail(f"Local host {name_host} is pingable!", "", "tommydzepina@gmail.com")
  60.                 else:
  61.                     send_talent_mail(f"Local host {name_host} is not pingable!", "", "tommydzepina@gmail.com")
  62.                 #  if there is a local host but it's not pingable send the msg
  63.                 #  elif the host is not local and it's pingable send the msg
  64.             elif is_pingable(ip):
  65.                 print(utils.OKGREEN + "Success : " + ip + ": " + name_host + utils.ENDC)
  66.                 # else, if the host is not local and the public domain is unknown send the msg
  67.             else:
  68.                 if down == 1:  # only first time print
  69.                     print(utils.FAIL + "Fail : " + ip + ": " + name_host + utils.ENDC)
  70.                     res = send_talent_mail(f"{name_host} doesn't respond!", "", "tommydzepina@gmail.com")
  71.                     if not res:
  72.                         print(utils.WARNING + "Mail failed" + utils.ENDC)
  73.                 elif down == TIMER:  # reset the down counter and reopen the while loop with break
  74.                     down = 0
  75.                     break
  76.  
  77.                 if get_status(ip):
  78.                     send_talent_mail(f"{name_host} is AVAILABLE!", "", "tommydzepina@gmail.com")
  79.                     print(utils.OKGREEN + ip + " Status: is AVAILABLE.")
  80.  
  81.  
  82. if len(sys.argv) < 2:  # if you tipe nothing exit
  83.     print("Need more arguments")
  84.     exit(1)
  85. elif len(sys.argv) == 2 and sys.argv[-1] == "-f":  # if third element is empty exit
  86.     print("the third argument is missing.")
  87.     exit(11)
  88. elif len(sys.argv) == 3:  # check right element
  89.     if sys.argv[1] != "-f":
  90.         print("the frist argument -f is not correct.")
  91.     elif sys.argv[2] != FILE_ADDRESSES:
  92.         print("the third argument is incorrect.")
  93.     else:
  94.         file_da_aprire = sys.argv[2]  # if everything is ok ping...
  95.         broadcast()
  96.         exit(0)
  97.  
  98. elif len(sys.argv) == 2:  # ping just one ip
  99.     indirizzo_da_scandire = sys.argv[1]
  100.     print("scan this ip...")
  101.     if is_pingable(indirizzo_da_scandire):
  102.         send_talent_mail(f"{indirizzo_da_scandire} is pingable!", "", "tommydzepina@gmail.com")
  103.     else:
  104.         send_talent_mail(f"{indirizzo_da_scandire} is not pingable!", "", "tommydzepina@gmail.com")
  105. else:
  106.     print("Too many arguments")
  107.     exit(99)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement