2

I'm trying to ping a range of servers and I want to store the output of the ping. This is as far as I have got.

import subprocess

string_part = 'ping -W 2 -c 2  64.233.'

for i in range(160,165):            
     for j in range(0,5):   
          prompt = string_part + str(i) + '.' + str(j)
          result = subprocess.call(prompt, shell = True)    

I thought that if I give "print(result)" after this it would print the result. However, it only returns 1. I don't want to use threads as of now. I think I'm missing something! :(

3 Answers 3

1

subprocess.call() returns the exit code of the process. To get the stdout output of the ping command, use a pipe and Popen.communicate() instead:

string_part = 'ping -W 2 -c 2  64.233.{}.{}'

for i in range(160, 165):            
     for j in range(5):   
          prompt = string_part.format(i, j)
          proc = subprocess.Popen(prompt, shell=True, stdout=subprocess.PIPE)
          result, _ = proc.communicate()

You can and should avoid using the shell; just pass in the arguments in a list:

command = ['ping', '-W', '2', '-c', '2']  
ip_template = '64.233.{}.{}'

for i in range(160, 165):            
     for j in range(5):   
          ip_address = ip_template.format(i, j)
          proc = subprocess.Popen(command + [ip_address], stdout=subprocess.PIPE)
          result, _ = proc.communicate()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. This works! I read that I shouldn't use shell = True so I just decided not to add it but didn't know a workaround for it. I still don't understand how it works just because you passed it as a list?
@user165971: the shell would do the same; parse out the string into separate arguments, start the command and pass in the rest of the arguments as a list. Without the shell, we need to do that work directly, but in Python that usually just makes it easier.
0
import subprocess

string_part = 'ping -W 2 -c 2  64.233.'
result=[]
for i in range(160,165):            
     for j in range(0,5):   
          prompt = string_part + str(i) + '.' + str(j)
          result.append(subprocess.call(prompt, shell = True))
print result

Comments

0

Just store output in a dict with the ip as the key and Popen.communicate[0] as the value

string_part = 'ping -W 2 -c 2  64.233.{}.{}'
d = {}

for i in range(160, 165):
    for j in range(0, 5):
        prompt = string_part.format(i,j).split()
        proc = subprocess.Popen(prompt, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        d[prompt[-1]] = proc.communicate()[0]
print d

{'64.233.162.1': 'PING 64.233.162.1 (64.233.162.1) 56(84) bytes of data.\n\n--- 64.233.162.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.163.0': 'PING 64.233.163.0 (64.233.163.0) 56(84) bytes of data.\n\n--- 64.233.163.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.162.4': 'PING 64.233.162.4 (64.233.162.4) 56(84) bytes of data.\n\n--- 64.233.162.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.163.2': 'PING 64.233.163.2 (64.233.163.2) 56(84) bytes of data.\n\n--- 64.233.163.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.163.3': 'PING 64.233.163.3 (64.233.163.3) 56(84) bytes of data.\n\n--- 64.233.163.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.163.4': 'PING 64.233.163.4 (64.233.163.4) 56(84) bytes of data.\n\n--- 64.233.163.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1008ms\n\n', '64.233.162.0': 'PING 64.233.162.0 (64.233.162.0) 56(84) bytes of data.\n\n--- 64.233.162.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.162.3': 'PING 64.233.162.3 (64.233.162.3) 56(84) bytes of data.\n\n--- 64.233.162.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.162.2': 'PING 64.233.162.2 (64.233.162.2) 56(84) bytes of data.\n\n--- 64.233.162.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.164.3': 'PING 64.233.164.3 (64.233.164.3) 56(84) bytes of data.\n\n--- 64.233.164.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.164.2': 'PING 64.233.164.2 (64.233.164.2) 56(84) bytes of data.\n\n--- 64.233.164.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.164.1': 'PING 64.233.164.1 (64.233.164.1) 56(84) bytes of data.\n\n--- 64.233.164.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.164.0': 'PING 64.233.164.0 (64.233.164.0) 56(84) bytes of data.\n\n--- 64.233.164.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.164.4': 'PING 64.233.164.4 (64.233.164.4) 56(84) bytes of data.\n\n--- 64.233.164.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.160.4': 'PING 64.233.160.4 (64.233.160.4) 56(84) bytes of data.\n\n--- 64.233.160.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.160.1': 'PING 64.233.160.1 (64.233.160.1) 56(84) bytes of data.\n\n--- 64.233.160.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.163.1': 'PING 64.233.163.1 (64.233.163.1) 56(84) bytes of data.\n\n--- 64.233.163.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.161.2': 'PING 64.233.161.2 (64.233.161.2) 56(84) bytes of data.\n\n--- 64.233.161.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1009ms\n\n', '64.233.161.3': 'PING 64.233.161.3 (64.233.161.3) 56(84) bytes of data.\n\n--- 64.233.161.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.161.0': 'PING 64.233.161.0 (64.233.161.0) 56(84) bytes of data.\n\n--- 64.233.161.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.161.1': 'PING 64.233.161.1 (64.233.161.1) 56(84) bytes of data.\n\n--- 64.233.161.1 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 999ms\n\n', '64.233.160.3': 'PING 64.233.160.3 (64.233.160.3) 56(84) bytes of data.\n\n--- 64.233.160.3 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1006ms\n\n', '64.233.160.2': 'PING 64.233.160.2 (64.233.160.2) 56(84) bytes of data.\n\n--- 64.233.160.2 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.161.4': 'PING 64.233.161.4 (64.233.161.4) 56(84) bytes of data.\n\n--- 64.233.161.4 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1007ms\n\n', '64.233.160.0': 'PING 64.233.160.0 (64.233.160.0) 56(84) bytes of data.\n\n--- 64.233.160.0 ping statistics ---\n2 packets transmitted, 0 received, 100% packet loss, time 1008ms\n\n'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.