-1
import pyping

server = ["jeff", "bob", "dave", "ryan", "drew"]

for i in server:
    online = 1
    try:
        result = pyping.ping(i)
        online = result.ret_code
        if len(i) > 7:
            print("|\tServer: "+ i +"\tIp: "+ result.destination_ip +"\tTime: "+ result.avg_rtt +"\t |")
        else:
            print("|\tServer: "+ i +"\t\tIp: "+ result.destination_ip +"\tTime: "+ result.avg_rtt +"\t |")

    except:
        print("|\tServer: "+ i +" Returned: OFFLINE!!!\t\t\t\t |")

please help me ping all of the servers at the same time to make it quicker

erip
  • 16,374
  • 11
  • 66
  • 121
  • 3
    [Multiple ping script in Python](http://stackoverflow.com/q/12101239/4279) – jfs May 23 '16 at 15:05
  • 1
    What about using [subprocess](https://docs.python.org/3/library/subprocess.html)? – Maximilian Peters May 23 '16 at 15:07
  • The subprocess script would do, however as this is I/O operation my approach would use Thread(s). I think some of the examples on the web that show how to deal with multiple threads (and use beautifulsoup) can be adapted to do exactly that – Miro Rodozov May 23 '16 at 15:29

1 Answers1

1

Try this

import threading

def worker(i):
    online = 1
    try:
        result = pyping.ping(i)
        online = result.ret_code
        if len(i) > 7:
            print("|\tServer: "+ i +"\tIp: "+ result.destination_ip +"\tTime: "+ result.avg_rtt +"\t |")
        else:
            print("|\tServer: "+ i +"\t\tIp: "+ result.destination_ip +"\tTime: "+ result.avg_rtt +"\t |")

    except:
        print("|\tServer: "+ i +" Returned: OFFLINE!!!\t\t\t\t |")

server = ["jeff", "bob", "dave", "ryan", "drew"]

for i in server:
    t = threading.Thread(target=worker, args=(i,))
    t.start()
hailong
  • 1,409
  • 16
  • 19