python调用linux命令行ping多个网站

import os
list = ["c31s1.jamjams.net", "c31s2.jamjams.net", "c31s3.jamjams.net", "c31s4.jamjams.net", "c31s5.jamjams.net"]
for url in list:
    cmd = "ping -c 2 " + url
    print(cmd)
    result = os.popen(cmd)
    result = result.read()
    for line in result.splitlines()[-2:]:
        print(line)
    print("\n")

os.popen 可以获得命令行的输出,而os.system只能获取命令行是否执行成功。

你可能感兴趣的:(python学习笔记)