PYTHON 测试服务器连通性

发布时间:2019-08-27 08:03:28编辑:auto阅读(1555)

    #-*-coding:utf-8-*-
    import os
    import sys
    import urllib2
    import pygame
    import re
    import socket
    import subprocess

    # 输入要测试的site值
    print "Hello"
    print pygame.ver
    site=raw_input("Pls Input Your Site:")
    req = urllib2.Request('http://192.168.1.88/cc.txt')
    fd = urllib2.urlopen(req)



    #http://192.168.1.88/cc.txt连接文件的第一列为site,第二列为电信IP,第三列为联通IP,第四列为域名
    for i in fd.readlines():
        i2 = re.match(site,i)
        if i2:
            SITE,DXIP,LTIP,DOMAIN=i.split()
            AGENT,ID=SITE.split('_')
            strinfo = re.compile('^0*')
            id = strinfo.sub('',ID)
            domain = 's'+id+"."+DOMAIN
            


    #端口测试
    for PORT in (1001,1002,1003):
                    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                    s.settimeout(1)
                    DXADDR = (str(DXIP),int(PORT))
                    dxstatus = s.connect_ex(DXADDR)
                    LTADDR = (str(DXIP),int(PORT))
                    ltstatus = s.connect_ex(LTADDR)
                    if dxstatus == 0:
                        print 'Test DX Connection to %s %s port [tcp] OK' % (DXIP,PORT)
                    else:
                        print 'Test DX Connection to %s %s port [tcp] FAIL' % (DXIP,PORT)
                        if ltstatus == 0:
                            print 'Test LT Connection to %s %s port [tcp] OK' % (LTIP,PORT)
                        else:
                            print 'Test lT Connection to %s %s port [tcp] FAIL' % (LTIP,PORT)  
                    continue
        
    #域名解析测试     
    domainping = subprocess.call("ping -n 1 %s" % domain, shell=True, stdout=open(r'ping.temp','w'), stderr=subprocess.STDOUT) 
    if domainping == 0:
        print "Domain name resolution %s: is OK"  %   domain
    else:
        print "Domain name resolution %s: is FAIL" %  domain
        
    #ping 测试 电信IP不行,就测试联通IP   
    dxping = subprocess.call("ping -n 1 %s" % DXIP, shell=True, stdout=open(r'ping.temp','w'), stderr=subprocess.STDOUT) 
    ltping = subprocess.call("ping -n 1 %s" % LTIP, shell=True, stdout=open(r'ping.temp','w'), stderr=subprocess.STDOUT)
    if dxping == 0:
        print "ping DX %s: is OK" %  DXIP
    else:
        print "ping DX %s is  FAIL"   % DXIP
    if ltping == 0:
        print "ping LT %s: is OK" % LTIP
    else:
        print "ping LT %s is  OK"   % LTIP



    #页面打开测试
    req1 = urllib2.Request('http://%s/aa.jsp' % domain)
    try:
        urllib2.urlopen(req1)
        print 'WebServer OK'
    except urllib2.URLError, e:
        print e.reason
    #i3=fd1.readlines()
    #m1=re.search(site,str(i3))
    raw_input("Pls Input Any Key.......")

关键字