Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. import sys
  2. import os
  3. import platform
  4. import subprocess
  5.  
  6. plat = platform.system()
  7. scriptDir = sys.path[0]
  8. hosts = os.path.join(scriptDir, 'hosts.txt')
  9. hostsFile = open(hosts, "r")
  10. lines = hostsFile.readlines()
  11. if plat == "Windows":
  12. for line in lines:
  13. line = line.strip( )
  14. ping = subprocess.Popen(
  15. ["ping", "-n", "1", "-l", "1", "-w", "100", line],
  16. stdout = subprocess.PIPE,
  17. stderr = subprocess.PIPE
  18. )
  19. out, error = ping.communicate()
  20. print out
  21. print error
  22.  
  23. if plat == "Linux":
  24. for line in lines:
  25. line = line.strip( )
  26. ping = subprocess.Popen(
  27. ["ping", "-c", "1", "-l", "1", "-s", "1", "-W", "1", line],
  28. stdout = subprocess.PIPE,
  29. stderr = subprocess.PIPE
  30. )
  31. out, error = ping.communicate()
  32. print out
  33. print error
  34.  
  35. hostsFile.close()
  36.  
  37. import sys
  38. import os
  39. import platform
  40. import subprocess
  41. import Queue
  42. import threading
  43.  
  44. def worker_func(pingArgs, pending, done):
  45. try:
  46. while True:
  47. # Get the next address to ping.
  48. address = pending.get_nowait()
  49.  
  50. ping = subprocess.Popen(ping_args + [address],
  51. stdout = subprocess.PIPE,
  52. stderr = subprocess.PIPE
  53. )
  54. out, error = ping.communicate()
  55.  
  56. # Output the result to the 'done' queue.
  57. done.put((out, error))
  58. except Queue.Empty:
  59. # No more addresses.
  60. pass
  61. finally:
  62. # Tell the main thread that a worker is about to terminate.
  63. done.put(None)
  64.  
  65. # The number of workers.
  66. NUM_WORKERS = 4
  67.  
  68. plat = platform.system()
  69. scriptDir = sys.path[0]
  70. hosts = os.path.join(scriptDir, 'hosts.txt')
  71.  
  72. # The arguments for the 'ping', excluding the address.
  73. if plat == "Windows":
  74. pingArgs = ["ping", "-n", "1", "-l", "1", "-w", "100"]
  75. elif plat == "Linux":
  76. pingArgs = ["ping", "-c", "1", "-l", "1", "-s", "1", "-W", "1"]
  77. else:
  78. raise ValueError("Unknown platform")
  79.  
  80. # The queue of addresses to ping.
  81. pending = Queue.Queue()
  82.  
  83. # The queue of results.
  84. done = Queue.Queue()
  85.  
  86. # Create all the workers.
  87. workers = []
  88. for _ in range(NUM_WORKERS):
  89. workers.append(threading.Thread(target=worker_func, args=(pingArgs, pending, done)))
  90.  
  91. # Put all the addresses into the 'pending' queue.
  92. with open(hosts, "r") as hostsFile:
  93. for line in hostsFile:
  94. pending.put(line.strip())
  95.  
  96. # Start all the workers.
  97. for w in workers:
  98. w.daemon = True
  99. w.start()
  100.  
  101. # Print out the results as they arrive.
  102. numTerminated = 0
  103. while numTerminated < NUM_WORKERS:
  104. result = done.get()
  105. if result is None:
  106. # A worker is about to terminate.
  107. numTerminated += 1
  108. else:
  109. print result[0] # out
  110. print result[1] # error
  111.  
  112. # Wait for all the workers to terminate.
  113. for w in workers:
  114. w.join()
  115.  
  116. import sys
  117. import os
  118. import platform
  119. import subprocess
  120.  
  121. plat = platform.system()
  122. scriptDir = sys.path[0]
  123. hosts = os.path.join(scriptDir, 'hosts.txt')
  124. hostsFile = open(hosts, "r")
  125. lines = hostsFile.readlines()
  126. for line in lines:
  127. line = line.strip( )
  128. if plat == "Windows":
  129. args = ["ping", "-n", "1", "-l", "1", "-w", "100", line]
  130.  
  131. elif plat == "Linux":
  132. args = ["ping", "-c", "1", "-l", "1", "-s", "1", "-W", "1", line]
  133.  
  134. ping = subprocess.Popen(
  135. args,
  136. stdout = subprocess.PIPE,
  137. stderr = subprocess.PIPE
  138. )
  139. out, error = ping.communicate()
  140. print out
  141. print error
  142.  
  143. hostsFile.close()
  144.  
  145. import sys
  146. import os
  147. import platform
  148. import subprocess
  149. import threading
  150.  
  151. plat = platform.system()
  152. scriptDir = sys.path[0]
  153. hosts = os.path.join(scriptDir, 'hosts.txt')
  154. hostsFile = open(hosts, "r")
  155. lines = hostsFile.readlines()
  156.  
  157. def ping(ip):
  158. if plat == "Windows":
  159. ping = subprocess.Popen(
  160. ["ping", "-n", "1", "-l", "1", "-w", "100", ip],
  161. stdout = subprocess.PIPE,
  162. stderr = subprocess.PIPE
  163. )
  164.  
  165. if plat == "Linux":
  166. ping = subprocess.Popen(
  167. ["ping", "-c", "1", "-l", "1", "-s", "1", "-W", "1", ip],
  168. stdout = subprocess.PIPE,
  169. stderr = subprocess.PIPE
  170. )
  171.  
  172. out, error = ping.communicate()
  173. print out
  174. print error
  175.  
  176. for ip in lines:
  177. threading.Thread(target=ping, args=(ip,)).run()
  178.  
  179. nmap -sP 192.168.100.0/24
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement