檢測主機狀態

2021-08-25 17:36:29 字數 2487 閱讀 9618

(1)

import subprocess

import threading

def ping(host):

result = subprocess.call(

『ping -c2 %s &> /dev/null』 % host,

shell=true

) if result == 0: # result的值就是ping命令的退出碼,即$?

print(『%s』 % host)

# else:

# print(『%s:down』 % host)

ifname== 『main『:

ips = [『172.40.58.%s』 % i for i in range(1, 255)]

for ip in ips: # 主線程用於產生工作執行緒

t = threading.thread(target=ping, args=(ip,))

t.start() # 將會執行ping(ip),執行完就停止了,不會有殭屍程序

# 主線程不會等待工作執行緒結束,直接進入下次迴圈

(2)

import threading

import subprocess

class ping:

# definit(self, host):

# self.host = host

def __call__(self, host):

result = subprocess.call(

'ping -c2 %s &> /dev/null' % host,

shell=true

)if result == 0: # result的值就是ping命令的退出碼,即$?

print('%s:up' % host)

else:

print('%s:down' % host)

ifname== 『main『:

ips = [『172.40.58.%s』 % i for i in range(1, 255)]

for ip in ips:

t = threading.thread(target=ping(), args=(ip,))

t.start() # ping(ip)() <==> target()

(3)

import threading

import subprocess

class ping:

definit(self, host):

self.host = host

def __call__(self):

result = subprocess.call(

'ping -c2 %s &> /dev/null' % self.host,

shell=true

)if result == 0: # result的值就是ping命令的退出碼,即$?

print('%s:up' % self.host)

else:

print('%s:down' % self.host)

ifname== 『main『:

ips = [『172.40.58.%s』 % i for i in range(1, 255)]

for ip in ips:

t = threading.thread(target=ping(ip))

t.start() # ping(ip)() <==> target()

(4)

import subprocess

import os

def ping(host):

result = subprocess.call(

『ping -c2 %s &> /dev/null』 % host,

shell=true

) if result == 0: # result的值就是ping命令的退出碼,即$?

print(『%s:up』 % host)

else:

print(『%s:down』 % host)

ifname== 『main『:

ips = [『172.40.58.%s』 % i for i in range(1, 255)]

for ip in ips:

pid = os.fork() # 父程序負責生成子程序

if not pid: # 子程序負責ping

ping(ip)

exit() # 子程序ping完乙個位址後結束,不要再迴圈

shell 指令碼批量檢測主機存活狀態

liunx 伺服器管理中,有時需要檢測主機的存活狀態,當主機不多的時候,可以直接使用ping 命令,當主機很多的時候直接使用ping 命令管理起來就比較麻煩了,使用shell 指令碼編寫的ping 命令檢測主機存活狀態,是乙個很好的方法。1 2 3 4 5 6 7 8 9 10 11 12 13 1...

根據ip列表檢測主機狀態(shell指令碼)

根據ip位址列表監測主機狀態 根據公司伺服器ip位址列表檔案,監測各個主機ping的聯通性,輸出各個主機是否啟動 關閉。這其中伺服器數量並不固定,ip位址之間也無特殊規律。vi ipfor.sh bin bash hlist cat root iplist.txt for ip in hlist d...

如何去檢測主機網路的連線狀態 C 收藏

如何去檢測網路的連線狀態 c 收藏 在有些程式中,你可能希望能檢測網路是否連通的,而又不想銷耗過多的系統資源,下面的方法是直接呼叫系統的api去做到檢測。1.方法定義 dllimport wininet.dll private extern static bool internetgetconnec...