2016-06-12 2 views
0

좋아요, 시작과 끝 IP를 입력하고 목록에있는 IP 주소를 포함하여 모든 IP 주소에 대해 ping을 수행하고 결과를 .txt 문서로 표시하려고합니다. 하나의 IP로 작동하도록 할 수는 있지만, 사용자가 2 개의 IP를 입력해야하고, 모든 IP를 성공적으로 ping 할 수 있도록하는 방법을 모르겠습니다. 감사. 이것은 현재 가지고있는 것이지만 문제가 있습니다.IP 핑핑 범위

def pingNetwork(): 


startingIP = raw_input("Enter starting IP address: ") 
response = os.system("ping -c 1 " + startingIP) 
for ip in range(1, 100): 

if response == 0: 
    with open('IP_LOG_TIMESTAMP.txt', 'w') as f: 
     f.write(startingIP + ' is up!') 
else: 
    with open('IP_LOG_TIMESTAMP.txt', 'w') as f: 
     f.write(startingIP + ' is down!') 

답변

0
import struct 
import socket 

def findIPs(start, end): 
    ipstruct = struct.Struct('>I') 
    start, = ipstruct.unpack(socket.inet_aton(start)) 
    end, = ipstruct.unpack(socket.inet_aton(end)) 
    return [socket.inet_ntoa(ipstruct.pack(i)) for i in range(start, end+1)] 

def pingNetwork(range_ips): 
    for ip in range_ips: 
     response = os.system("ping -c 1 " + ip) 
     if response == 0: 
      with open('IP_LOG_TIMESTAMP.txt', 'w') as f: 
       f.write(ip+ ' is up!') 
     else: 
      with open('IP_LOG_TIMESTAMP.txt', 'w') as f: 
       f.write(ip+ ' is down!') 

ips = findIPs('111.111.111.0', '111.111.111.3') 
pingNetwork(ips)