Home > research > Python script that pings a large number of hosts

Python script that pings a large number of hosts

To address the problem that Gush may run of file descriptors, I have started the Crazy Stress Test in which all the planetlab nodes are added to williams_gush. It took forever to add all the nodes (more than 900 of them), so I had to write a little hack to handle-plcdb.pl.

pingtest.py

A python program that uses threading to ping a large number of hosts.

Usage: python pingtest.py [input file] [number of threads]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import subprocess
import threading
import sys
 
 
badHosts = []
hostQueue = []
lock = threading.Lock()
debug = False
count = 0
 
def parseHost(line):
    if line.find('node name') >= 0:
        return line[line.find('"') + 1 : len(line) - 4]
 
    return None
 
 
def ping(host):
    ret = subprocess.call("ping -c 1 %s" % host,
                          shell=True,
                          stdout=open('/dev/null', 'w'),
                          stderr=subprocess.STDOUT)
 
    return ret == 0
 
 
def popQueue():
    global lock
    global hostQueue
 
    host = None
 
    lock.acquire()
    if len(hostQueue) > 0:
        host = hostQueue.pop()
    lock.release()
 
    return host
 
 
 
def dequeue():
    global lock
    global badHosts
    global hostQueue
    global count
 
    while True:
        host = popQueue()
        if not host:
            return
 
        if not ping(host):
            lock.acquire()
            badHosts.append(host)
            lock.release()
 
        lock.acquire()
        count -= 1
        if debug:
            print "%d left" % count
        lock.release()
 
 
def main():
    global debug
    global badHosts
    global hostQueue
    global count
 
    try:
        file = sys.argv[1]
        n = int(sys.argv[2])
    except:
        print "Usage: python pingtest.py [input file] [number of threads] [optional debug]"
        return
 
    if len(sys.argv) > 3:
        debug = (sys.argv[3] == 'debug')
 
    f = open(file, 'r')
    lines = f.readlines()
    f.close()
 
    threads = []
    count = 0
 
    for line in lines:
        host = parseHost(line)
        if host:
            hostQueue.append(host)
            count += 1
 
    for i in range(n):
        t = threading.Thread(target = dequeue)
        t.start()
        threads.append(t)
 
    for t in threads:
        t.join()
 
    if debug:
        print "Writing to file..."
 
    f = open('static-list-of-pingable-nodes.txt', 'w')
    for line in lines:
        host = parseHost(line)
        if host:
            if not host in badHosts:
                f.write(line)
        else:
            f.write(line)
    f.close()
 
    if debug:
        print "Done. Nodes written to static-list-of-pingable-nodes.txt"
 
if __name__ == '__main__':
    main()

The following lines were added to the beginning of handle-plcdb.pl as a temporary hack, so that all nodes that are returned by the Perl script are pingable.

1
2
3
4
if ( -f "static-list-of-pingable-nodes.txt" ) {
    system("cat static-list-of-pingable-nodes.txt");
    exit(0);
}
Categories: research Tags:
  1. No comments yet.
  1. No trackbacks yet.