Advertisement
Guest User

wiss

a guest
Feb 20th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. from ipaddress import ip_network, ip_address
  2.  
  3. from openpyxl import load_workbook
  4.  
  5.  
  6. def get_hosts_adresses(line):
  7.     ip = line[2].value
  8.     mask = line[3].value
  9.     try:
  10.         cidr = sum(bin(int(x)).count('1') for x in mask.split('.'))
  11.         full_ip_address = ip + '/' + str(cidr)
  12.         net4 = ip_network(full_ip_address, False)
  13.         return net4.hosts()
  14.  
  15.     except ValueError as e:
  16.         print("The string you suplied is not a valid ip address :: {}".format(e))
  17.         return None
  18.  
  19. def ip_in_hosts(line, hosts):
  20.     try:
  21.         ip = ip_address(line[3].value)
  22.         return ip in hosts
  23.     except ValueError as e:
  24.         print("The string you suplied is not a valid ip address :: {}".format(e))
  25.         return False
  26.  
  27. if __name__ == "__main__":
  28.     filename = "wiss.xlsx"
  29.  
  30.     wb = load_workbook(filename)
  31.     msan_sheet = 'Msan'
  32.     ipran_sheet = 'IPRAN-MASAN'
  33.     result_sheet = 'result'
  34.  
  35.     msan_ranges = wb[msan_sheet]
  36.     ipran_ranges = wb[ipran_sheet]
  37.     result_ranges = wb[result_sheet]
  38.  
  39.     for msan_line in msan_ranges:
  40.         iprans = []
  41.         last_row = result_ranges.max_row
  42.         host_gen = get_hosts_adresses(msan_line)
  43.  
  44.         if host_gen:
  45.             host_list = list(host_gen)
  46.             for ipran_line in ipran_ranges:
  47.                 if ip_in_hosts(ipran_line, host_list):
  48.                     iprans.append(ipran_line)
  49.  
  50.             iprans_cells = [item for sublist in iprans for item in sublist]
  51.  
  52.             print(iprans_cells)
  53.             entry = list(map(lambda x: x.value, list(msan_line) + iprans_cells))
  54.             # #
  55.             print(entry)
  56.             result_ranges.append(entry)
  57.             wb.save("wiss2.xlsx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement