轉(zhuǎn)自左手_wanggy
原文地址:[http://blog.chinaunix.net/uid-27050514-id-3993740.html]
最近有客戶反應(yīng),他們的呼叫中心系統(tǒng)經(jīng)常會(huì)出現(xiàn)彈屏后,電話無法轉(zhuǎn)接的情況。經(jīng)初步判斷排除是軟件問題,應(yīng)該是二個(gè)網(wǎng)絡(luò)連接有閃斷或延時(shí)的問題,但對(duì)方認(rèn)為是系統(tǒng)不可靠,自己的網(wǎng)絡(luò)沒有問題,要我們拿出證據(jù)來。
原來考慮隨便下載個(gè)軟件監(jiān)檢下,但考慮到在網(wǎng)系統(tǒng),最后還是考慮用python。
這個(gè)腳本參考了網(wǎng)上的一些資料,考慮到實(shí)際使用場(chǎng)景完善,可用于批量檢測(cè)本機(jī)及網(wǎng)絡(luò)端口開放情況。 僅供參考。
ip.txt 格式:
192.168.1.100 33001
192.168.1.101 33001
#!/usr/bin/env python
# -*- coding: gbk -*-
import socket,time
while 1:
file_obj = open('ip.txt')
for line in file_obj:
try:
sc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip = line.split()[0]
port = int(line.split()[1])
print ip,port
#設(shè)置超時(shí)時(shí)間(0.0)
sc.settimeout(2)
sc.connect((ip,port))
timenow=time.localtime()
datenow = time.strftime('%Y-%m-%d %H:%M:%S', timenow)
logstr="%s:%s 連接成功->%s \n" %(ip,port,datenow)
print logstr
sc.close()
except:
file = open("log.txt", "a")
timenow=time.localtime()
datenow = time.strftime('%Y-%m-%d %H:%M:%S', timenow)
logstr="%s:%s 連接失敗->%s \n" %(ip,port,datenow)
print logstr
file.write(logstr)
file.close()
print "sleep 10....."
time.sleep(10)