python實現增強版ping

2021-06-07 11:28:50 字數 2014 閱讀 3568

由於定位網路問題時,經常要ping,並且有時候要長時間同時ping多位址,系統自帶的ping不夠用 ,所以自己用python實現乙個,用py2exe編譯為exe程式後可以方便發布。

import time

import string

import thread

import os

ping_ip =

times = -1

delay = 0

ping_cmd = 'ping [ip]'

result_file = 'result.txt'

thread_count = 0

def log(f, s):

fp = open(f, 'a')

fp.write(s)

fp.close()

return

def  doping(ip):

ping = ping_cmd

ping = ping.replace('[ip]', ip)

ping = ping.replace('[result_file]', result_file)

log_str = '\n' + ping + '\n' + time.asctime() + '\n'

line = os.popen(ping).read()

log_str = log_str + line + '\n'

print log_str

log(result_file, log_str)

time.sleep(delay)

return

def ping_thread(ip, times):

global thread_count

if times == -1:

while true:

doping(ip)

return

for t in range(0, times):

doping(ip)

thread_count = thread_count - 1

return

fp = open('exping.cfg')

for line in fp:

line = line.strip()

if line[0:1] == '#':

continue

t = line.split('=')

if len(t) <= 1:

continue

if 'ip' == t[0]:

elif 'times' == t[0]:

times = string.atoi(t[1])

elif 'delay' == t[0]:

delay = string.atoi(t[1])

elif 'cmd' == t[0]:

ping_cmd = t[1]   

elif 'result_file' == t[0]:

result_file = t[1]

fp.close()

for ip in ping_ip:

thread_count = thread_count + 1

thread.start_new_thread(ping_thread, (ip, times))

while true:

if thread_count == 0:

break

time.sleep(5)

配置檔案:

#執行次數

times=2

#每次執行後的時間延遲

delay=5

#結果輸出檔案

result_file=result.txt

#ping命令

cmd=ping -c 1 [ip]

#要ping的ip位址,可以任意多個,每個會啟用乙個執行緒

ip=192.168.1.1

ip=www.163.com

ip=218.17.167.160

python 巢狀字典取值增強版

def getdictvalue d,code result if isinstance d,dict try value d code except exception as e pass for valuedd in d.values if isinstance valuedd,dict yie...

增強版字典DictionaryEx

public class dictionaryex idictionary 初始化 dictionaryex key不存在時返回defaultvalue 預設值 比較鍵時要使用物件,如果為null則使用預設比較方法 public dictionaryex tvalue defaultvalue,ie...

IntelliJ IDEA 便攜增強版

idea 全稱 intellij idea,是j a語言開發的整合環境,intellij在業界被公認為最好的j a開發工具之一,尤其在智慧型 助手 自動提示 重構 j2ee支援 各類版本工具 git svn github等 junit cvs整合 分析 創新的gui設計等方面的功能可以說是超常的。i...