穆琪的博客

一个程序员的自传

扫描局域网中的树莓派脚本

使用自带库,python2,手机上的qpython软件可用

import os
import subprocess  
iplist=[]
def pingCheck(ip):
    p = subprocess.Popen("ping -c 1 -w 1 %s"%ip,\
    stdin = subprocess.PIPE,\
    stdout = subprocess.PIPE,\
    stderr = subprocess.PIPE,\
    shell = True)   
    out = p.stdout.read()  
    print out
    if "TTL" in str(out) or "ttl" in str(out):
        print "ping %s is ok"%ip
        iplist.append(ip)
    else:
        print "ping %s is fail"%ip
def main():
    for x in range(1,255):
        pingCheck("192.168.199."+str(x))
    print str(iplist)
if __name__=="__main__":
    main()
© 鲁ICP备18041558号
Powered by Z-BlogPHP & Yiwuku.com