- import Queue, threading, subprocess
- import re;
- import sys;
- import os;
- import struct;
- ping_cmd = "ping";
- ping_host = "192.168.2.1";
- ping_waittime = "-W 1";
- ping_count_flag = "-c";
- ping_result = "packets transmitted, (\\d+) .+"
- halt_cmd = "halt";
- def check_windows_and_python():
- ok = -1;
- isX64 = 'PROGRAMFILES(X86)' in os.environ
- bits = struct.calcsize("P")*8;
- if isX64 == True and bits == 64:
- ok = 0;
- elif isX64 == False and bits == 32:
- ok = 0;
- if ok < 0:
- print("require", bits, "bits python for this system");
- return ok;
- def check_platform() :
- ret = -1;
- osinfo = sys.platform;
- if osinfo.find("win32") != -1:#windows platform
- ret = check_windows_and_python();
- if ret >= 0:
- global ping_waittime;
- global ping_count_flag;
- global ping_result;
- global halt_cmd;
- ping_waittime = "-w 1000";
- ping_count_flag = "-n";
- ping_result = "Received = (\\d+), Lost =";
- halt_cmd = "shutdown -f -s -t 60"
- ret = 1;
- elif osinfo.find("darwin") != -1:#Mac OS X
- ret = 2;
- elif osinfo.find("linux2") != -1:#ubuntu EXSi
- ret = 3;
- if ret == -1:
- print("Unknown Platform: ", osinfo);
- return ret;
- def load_host_list(host_file):
- f = open(host_file);
- host_list = f.readlines();
- return host_list;
- def ping_host_count(osinfo, host, c):
- ret = -1;
- ping_count = ' '.join([ping_count_flag, c]);
- if (osinfo == 1):#windows
- cmd_line = ' '.join([ping_cmd, ping_waittime, ping_count, host]);
- sub = subprocess.Popen(cmd_line, stdout=subprocess.PIPE);
- else:#other os system
- sub = subprocess.Popen([ping_cmd, ping_waittime, ping_count, host], stdout=subprocess.PIPE);
- output = sub.stdout.read().decode('utf-8');
- grep = re.search(ping_result, output);
- sed = re.search("\\d+", grep.group());
- ret = sed.group(0);
- missed = int(c) - int(ret);
- return missed;
- def ping_host_count_with_queue(osinfo, host, c, que):
- missed = 0;
- try: missed = ping_host_count(osinfo, host, str(c));
- finally: que.put( (host, missed));
- #ping-multi hosts, return min(missed_in_hosts)
- def ping_multi_hosts(osinfo, hosts, c):
- min_missed = c;
- threads_count = len(hosts);
- que = Queue.Queue();
- for host in hosts:
- ping_thread = threading.Thread(target = ping_host_count_with_queue, args = (osinfo, host, c, que, ))
- ping_thread.start();
- while threads_count > 0:
- host, missed = que.get();
- #print(host, missed);
- threads_count = threads_count - 1;
- if (min_missed > missed):
- min_missed = missed;
- return min_missed;
- #ping host once
- def ping_host_once(osinfo, host, time_out):
- missed = ping_host_count(osinfo, host, time_out);
- print("missed: ", missed);
- if(missed == int(time_out)):
- print("SYSTEM WILL HALT!!!");
- subprocess.call(halt_cmd);
- return missed;
- #ping host forever
- def ping_host_forever(osinfo, host, time_out):
- ret = -1;
- total_missed = 0;
- count = 3;
- while True:
- missed = ping_host_count(osinfo, host, str(count));
- if (missed == count):
- total_missed = total_missed + missed;
- else:
- total_missed = 0;
- if (total_missed >= int(time_out)):
- print("SYSTEM WILL HALT!!!");
- subprocess.call(halt_cmd);
- ret = 0;
- break;
- return ret;
- #ping multi-hosts once
- def ping_multi_hosts_once(osinfo, host_file, time_out):
- ret = -1;
- host_list = load_host_list(host_file);
- min_missed = ping_multi_hosts(osinfo, host_list, int(time_out));
- if(min_missed == int(time_out)):
- print("ping_multi_hosts timeout, SYSTEM WILL HALT!!!");
- subprocess.call(halt_cmd);
- ret = 0;
- return ret;
- #ping multi-hosts forever
- def ping_multi_host_forever(osinfo, host_file, time_out):
- ret = -1;
- total_missed = 0;
- count = 3;
- host_list = load_host_list(host_file);
- while True:
- missed = ping_multi_hosts(osinfo, host_list, int(count));
- if (missed == count):
- total_missed = total_missed + missed;
- else:
- total_missed = 0;
- if (total_missed >= int(time_out)):
- print("SYSTEM WILL HALT!!!");
- subprocess.call(halt_cmd);
- ret = 0;
- break;
- return ret;
- def usage():
- print("usage: sudo python hello.py [once|forever] 192.168.2.1 60");
- print("usage: sudo python hello.py [once_multi|forever_multi] hosts.list 60");
- print("host.list format: one ip / line");
- if __name__ == "__main__":
- if (len(sys.argv) >= 4):
- action = sys.argv[1];
- host = sys.argv[2];
- time_out = sys.argv[3];
- ret = check_platform();
- print("check_platform: ", ret);
- if (action == "once" and ret >= 0):
- ping_host_once(ret, host, time_out);
- elif (action == "forever" and ret >= 0):
- ping_host_forever(ret, host, time_out);
- elif (action == "once_multi" and ret >= 0):
- ping_multi_hosts_once(ret, host, time_out);
- elif (action == "forever_multi" and ret >= 0):
- ping_multi_host_forever(ret, host, time_out);
- else:
- usage();
- else:
- usage();
- #该片段来自于http://www.codesnippet.cn/detail/050820134974.html
来源: http://www.codesnippet.cn/detail/050820134974.html