2016-06-12 11 views
0

さて、開始IPと終了IPを入力し、リスト内のIPアドレスを含めてすべてのIPアドレスにpingを実行し、その結果を.txtドキュメントに表示したいと考えています。私はそれを単一のIPのために働かせることができますが、私はそれを作る方法を知らないので、ユーザーは2つのIPを入力しなければなりません。ありがとう。これは現在私が持っているものの、問題を抱えています。IP pingの範囲

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) 
関連する問題