多线程判断用户是否在线(后台运行ping脚本)

#!/bin/bash
#writen by Jerry
for i in `seq 1 255`;
   do
     {
        ping 192.168.51.$i -c 2 >> /dev/null 2>&1    #不管ping到ping不到都不在前台显示
        tai=$(echo $?)
        if [ $tai == 0 ];
            then
                echo -e "\033[1;32m 192.168.51.$i is online \033[0m"    #加剧颜色显示online
            else
                echo -e "\033[1;35m 192.168.51.$i is offline \033[0m"    #同上
        fi
        }&
   done
    wait
    echo "all Finished!"
    
再上一个python版本的

#!/usr/bin/python
#Written By jerry
#date 2018-03-20
import subprocess
import threading

def ping(host):
    result = subprocess.call(
        'ping -c2 %s &> /dev/null' % host,
        shell=True
    )
    if result == 0:
        print "%s:up" % host
    else:
        print "%s:down" % host

if __name__ == '__main__':
    ips = ['192.168.12.%s' % i for i in range(1, 255)]
    for ip in ips:
        t = threading.Thread(target=ping, args=(ip,))
        t.start()