I'm trying to do something usefull with some itens in list in the following code:
IPS=['10.254.2开发者_如何学JAVA43.83','10.254.243.82']
def commands(cmd):
command = Popen(cmd, shell=True, stdout=PIPE)
command_strip = command.stdout.read().strip()
print command_strip
def main():
for ip in IPS:
ping = call('ping -c 3 %s' % ip, shell=True)
commands(ping)
if __name__ == "__main__":
main()
Then, it returns me: python teste.py
PING 10.254.243.83 (10.254.243.83) 56(84) bytes of data.
64 bytes from 10.254.243.83: icmp_seq=1 ttl=61 time=2.72 ms
64 bytes from 10.254.243.83: icmp_seq=2 ttl=61 time=2.05 ms
64 bytes from 10.254.243.83: icmp_seq=3 ttl=61 time=1.88 ms
--- 10.254.243.83 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 1.885/2.224/2.728/0.363 ms
Traceback (most recent call last):
File "teste.py", line 15, in ?
main()
File "teste.py", line 12, in main
commands(ping)
File "teste.py", line 5, in commands
command = Popen(cmd, shell=True, stdout=PIPE)
File "/usr/lib/python2.4/subprocess.py", line 543, in __init__
errread, errwrite)
File "/usr/lib/python2.4/subprocess.py", line 891, in _execute_child
args = ["/bin/sh", "-c"] + args
TypeError: can only concatenate list (not "int") to list
Can someone help me with this error ?
You pass the return value of
call('ping -c 3 %s' % ip, shell=True)
as cmd
argument to your commands()
function. The mentioned return value is an integer, which does not make any sense as a command, so trying to execute this integer using Popen()
fails as expected.
Have you tried replacing
ping = call('ping -c 3 %s' % ip, shell=True)
with
ping = 'ping -c 3 %s' % ip
?
精彩评论