2014-12-02 6 views
1

나는 IP를 물어보고 많은 동시 PING을 수행 할 파이썬 스크립트를 만들려고 노력해 왔습니다.Python multithreading "ping"

하지만 난 단지 내가

import _thread 
import os 
import time 

def main(): 

    threadnbr = 0 

    ip = str(input("Input the ip adresse to play with? ")) 
    threads = int(input("Have many threads? ")) 

    check(ip) 

    if check(ip) == 0: 
     print("It is up") 
    else: 
     print("Is is down") 

    thread(ip, threads, threadnbr) 

def thread(ip, threads, threadnbr): 

    while threads > threadnbr: 

     _thread.start_new_thread(dos(ip)) 

     threadnbr = threadnbr + 1 

    else: 
     print(threadnbr, " started") 

def check(ip): 

    response = os.system("ping -c 1 " + ip) 

    return response 

def dos(ip): 

    os.system("ping -i 0.1 -s 8000 " + ip) 
    print("1") 

main() 

답변

1
_thread.start_new_thread(dos(ip)) 

당신은 제대로 여기에 인수를 제공하지 않습니다 OSX에서 실행 해요

한 번에 하나의 PING을 실행할 수 있습니다처럼 보인다 - 코드가 실행 주 스레드에서. 자세한 내용은 the documentation을 참조하십시오.

또한 thread 대신 threading을 사용해야합니다. 이 모듈은 더 이상 사용되지 않습니다.

dosDoS을 의미하는 경우, 저는 자신의 인프라에 대한 교육적 목적으로이 작업을 수행하기를 진심으로 바랍니다.

+0

내가-IT 기술을 연구, 우리는 너무 예는 교육 목적을위한, ICMP에 대한 있습니다. 하지만 프로그램을 작성해야합니까? –

+0

@JesperPetersen 당신은 아마 *'_thread.start_new_thread (dos, (ip,))'를 쓰고 싶었습니다. – goncalopp

+1

다시 그걸 멈추게할까요? –

1

내장 핑을 사용하는 대신 Scapy lib를 사용할 수 있습니다. 는 여기에 멀티 스레드 핑입니다 :

import threading 
from scapy.all import * 

def send_pkt(dst,padding=0): 
    pkt  = IP(dst=dst)/ICMP()/Padding('\x00'*padding) 
    ans,unans = sr(pkt) 
    ans.summary(lambda (s,r): r.sprintf("%IP.src% is alive")) 


def thread(dst, threads, threadnbr): 

    while threads > threadnbr: 
     t = threading.Thread(None, send_pkt, None, (dst,), {'padding':8000}) 
     t.start() 
     threadnbr = threadnbr + 1 
    else: 
     print(threadnbr, " started") 


def main(): 
    dst  = raw_input("Input the ip adresse to play with? ") 
    threads = int(raw_input("Have many threads? ")) 
    threadnbr = 0 

    send_pkt(dst) 

    thread(dst, threads, threadnbr) 

main()