def check_ip_availability(ip_list): ''' Функция проверяет доступность IP-адресов. Функция ожидает как аргумент список IP-адресов. IP-адреса могут быть в формате: * 10.1.1.1 * 10.1.1.1-10.1.1.10 * 10.1.1.1-10 Если адрес указан в виде диапазона, надо проверить доступность всех адресов диапазон а включая последний. ''' # print(ip_list) result=[] result2=[] for ip in ip_list: if is_it_ip(ip): result.append(ip) if '-' in ip: spip=ip.split('-') if '.' in spip[1]:# print('It is NOT ip ', ip) for i in range(int(spip[0].split('.')[3]),(int(spip[-1].split('.')[-1])+1)): result2.append('.'.join(spip[0].split('.')[:3])+'.'+str(i)) else: for i in range(int(spip[0].split('.')[3]),(int(spip[-1])+1)): result2.append('.'.join(spip[0].split('.')[:3])+'.'+str(i)) result.extend(result2) return check_ip_addresses(result) # input=['8.8.8.8-9','10.1.1.1-10.1.1.3','192.168.8.254','192.168.8.250','78.9.85.8'] # print(check_ip_availability(input))
def check_ip_availability(ip_range): if '-' in ip_range: ip_range_dashed = ip_range.split('-') ip_main = '.'.join(ip_range_dashed[0].split('.')[0:3]) if '.' in ip_range_dashed[1]: ip_start = ip_range_dashed[0].split('.')[-1] ip_stop = ip_range_dashed[1].split('.')[-1] list_of_ips = [ '{}.{}'.format(ip_main, last_octet) for last_octet in range(int(ip_start), int(ip_stop) + 1) ] elif '.' not in ip_range_dashed[1]: ip_start = ip_range_dashed[0].split('.')[-1] ip_stop = ip_range_dashed[1] list_of_ips = [ '{}.{}'.format(ip_main, last_octet) for last_octet in range(int(ip_start), int(ip_stop) + 1) ] else: list_of_ips = [ip_range] availability = check_ip_addresses(list_of_ips) return availability
def check_ip_availability(ip): ips = [] net = '.'.join(ip.split('.')[:3]) for i in range(int(ip.split('-')[0].split('.')[-1]), int(ip.split('-')[-1].split('.')[-1]) + 1): ips.append(net + '.' + str(i)) return check_ip_addresses(ips)
def check_ip_availability(list_of_ip): print("Please waiting...") rangeiplist = [] for ip in list_of_ip: if '-' in ip: first = ip.split('-')[0] last = ip.split('-')[1] firstip = ipaddress.ip_address(first) rangeip = int(last.split('.')[3]) - int(first.split('.')[3]) + 1 for i in range(rangeip): rangeiplist.append(str(firstip + i)) else: rangeiplist.append(ip) return check_ip_addresses(rangeiplist)
def check_ip_availabality(ip_range): if '-' in ip_range: first_ip, last_ip = ip_range.split('-') if '.' not in last_ip: last_ip = '.'.join(first_ip.split('.')[:3]) + '.' + last_ip first_ip_last_byte = int(first_ip.split('.')[-1]) first_3_octets = '.'.join(first_ip.split('.')[:3]) num_of_add = int(last_ip.split('.')[-1]) - first_ip_last_byte addresses = [] for last_octet in range(first_ip_last_byte, first_ip_last_byte + num_of_add + 1): addresses.append(first_3_octets + '.{}'.format(str(last_octet))) else: addresses = [ip_range] return (check_ip_addresses(addresses))
def check_ip_availability(ip): ip_list = [] length1 = len(ip.split('-')) if length1 == 1: ip_list.append(ip) elif length1 == 2: if len(ip.split('-')[1]) > 3: ip_list = [ '{}.{}.{}.{}'.format(*(ip.split('.')[0:3]), str(num)) for num in range(int(ip.split('-')[0].split('.')[-1]), int(ip.split('.')[-1]) + 1) ] else: ip_list = [ '{}.{}.{}.{}'.format(*(ip.split('.')[0:3]), str(num)) for num in range(int(ip.split('-')[0].split('.')[-1]), int(ip.split('-')[-1]) + 1) ] return check_ip_addresses(ip_list)
from task_12_1 import check_ip_addresses from task_12_2 import check_list_ip from tabulate import tabulate def ip_table(avail, unavail): ip_dict = {'Reachable': avail, 'Unreachable': unavail} return tabulate(ip_dict, headers='keys') if __name__ == '__main__': ip_list = ['8.8.4.4', '1.1.1.1-3', '172.21.41.128-172.21.41.132'] result = check_ip_addresses(check_list_ip(ip_list)) print(ip_table(result[0], result[1]))
str('.'.join(range_ip[0].split('.')[0:3])) + '.' + str(range_ip[1].split('.')[-1])) # конечный адрес for ip_int in range( int(start_ip), int(end_ip) + 1): # добавляем 1, т.к. последний адрес не включен в цикл list_ip.append(str(ipaddress.IPv4Address(ip_int))) else: list_ip.append(string) return list_ip #print (check_ip_availability(adress_list)) print_template = ''' Доступные адреса: {} Недоступные адреса: {} ''' if __name__ == "__main__": ip_list = check_ip_availability(adress_list) available, not_available = check_ip_addresses(ip_list) print(print_template.format(available, not_available))