python3 多线程ping当前网段主机是否存活

1. python3 多线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 主线程只负责生成工作线程
# 工作线程只做具体的工作
# 多线程共享进程里的内存块
# 多进程不共享
 
import threading
 
def Hello(world, tedu):
    print('Hello %s %s!' % (world,tedu))
 
if __name__ == '__main__':
    for i in range(3):
        th = threading.Thread(target=Hello, args=('world','tedu'))  # 创建工作线程
        th.start()  # 启动工作线程

  

2.. python3 多线程ping当前网段主机是否存活

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 多线程的方式ping当前11网段主机
import subprocess
import threading
 
def ping_host(hosts):
    result_ping = subprocess.run("ping %s -c 2 &> /dev/null" % hosts, shell=True)
    if result_ping.returncode == 0:
        print('%s is up' % hosts)
    else:
        print('%s is down' % hosts)
 
if __name__ == '__main__':
    iplist = [ '192.168.11.%s' % hosts for hosts in range(1,255)]
    for hosts in iplist:
        th = threading.Thread(target=ping_host, args=(hosts,))
        th.start()

  

3. python3 以OOB的方式,不绑定实例,多线程ping主机是否存活

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 以 OOB 的方式传参不绑定实例
# 进行11网段的主机IP地址ping,看主机是否存活
 
import subprocess
import threading
 
class Ping_Host():
    def __call__(self, hosts):
        result_ping = subprocess.run("ping -c 2 %s &>/dev/null" %hosts, shell=True)
        if result_ping.returncode == 0:
            print("%s is online" % hosts)
        else:
            print("%s is death" % hosts)
 
if __name__ == '__main__':
    ips = ["192.168.11.%s" % hosts for hosts in range(1,255)]
    for hosts in ips:
        th = threading.Thread(target=Ping_Host(), args=(hosts,))
        th.start()

  

4. python3 以OOB的方式,绑定实例,多线程ping主机是否存活

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 以OOB的方式绑定实例hosts
# ping 11 网段的主机,看是否存活
 
import subprocess
import threading
 
# 定义类
class Ping_Host:
    # 初始化实例
    def __init__(self, hosts):
        # 将hosts绑定实例
        self.hosts = hosts
 
    # 调用call方法,在调用类的时候,call方法被自动执行,而hosts又是绑定到类中的,所以调用Ping_Host的类的时候call方法被调用,下面的代码被执行。
    def __call__(self, *args, **kwargs):
        # 调用subprocess模块,执行系统命令
        ping_result = subprocess.run("ping -c 2 %s &> /dev/null" % self.hosts, shell=True)
        # 判断命令执行结果的返回码,返回码为0,则执行成功。
        if ping_result.returncode == 0:
            print("%s is up" % self.hosts)
        else:
            print("%s is down" % self.hosts)
 
if __name__ == '__main__':
    # 生成ip列表
    ips = ['192.168.11.%s' % hosts for hosts in range(1,255)]
    # 循环IP列表
    for hosts in ips:
        # 每次循环,创建工作线程,调用Ping_Host类,将hosts参数传给类
        thread_result = threading.Thread(target=Ping_Host(hosts))
        # 开启多线程,当前线程执行完毕后自行退出。
        thread_result.start()

  

5. python3 以OOB的方式,绑定实例,多线程ping主机是否存活,控制线程在5个

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 以OOB的方式绑定实例hosts
# ping 11 网段的主机,看是否存活
 
import subprocess
import threading
 
# 定义类
class Ping_Host:
    # 初始化实例
    def __init__(self, hosts):
        # 将hosts绑定实例
        self.hosts = hosts
 
    # 调用call方法,在调用类的时候,call方法被自动执行,而hosts又是绑定到类中的,所以调用Ping_Host的类的时候call方法被调用,下面的代码被执行。
    def __call__(self, *args, **kwargs):
        # 调用subprocess模块,执行系统命令
        ping_result = subprocess.run("ping -c 2 %s &> /dev/null" % self.hosts, shell=True)
        # 判断命令执行结果的返回码,返回码为0,则执行成功。
        if ping_result.returncode == 0:
            print("%s is up" % self.hosts)
        else:
            print("%s is down" % self.hosts)
 
if __name__ == '__main__':
    # 生成ip列表
    ips = ['192.168.11.%s' % hosts for hosts in range(1,255)]
 
    # 创建线程池
    thread_pool = []
    max_threads = 5
 
    for hosts in ips:
        # 控制线程数量,如果线程的数量大于等于5个,就等所有线程完成,完成之后再清空线程池
        if len(thread_pool) >= max_threads:
            # 等待所有线程完成
            for thread in thread_pool:
                thread.join()
            # 清空线程池
            thread_pool = []
 
        # 每次循环,创建工作线程,调用Ping_Host类,将hosts参数传给函数
        thread_result = threading.Thread(target=Ping_Host(hosts))
        # 将线程加入列表
        thread_pool.append(thread_result)
        # 开启多线程,当前线程执行完毕后自行退出。
        thread_result.start()

  

posted @   江戸川のコナン  阅读(72)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
……
点击右上角即可分享
微信分享提示