telnet批量检查端口是否正常

telnet批量检查端口是否正常,第1张

一、本机测试目标端口是否正常:

  python调用的socket模块来实现:

#!/usr/bin/python3

import socket

#${ip+port},${描述}
ip_prot = '''
0.0.0.0 80,http的80端口
0.0.0.0 443,http的443端口
更多
'''

for i in ip_prot.strip().split('\n'):
    if not i:
        continue
    # print(i)
    ip = i.split(',')[0].split()[0]
    port = int(i.split(',')[0].split()[-1])
    desc = i.split(',')[-1]
    try:
        sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sk.settimeout(5)
        sk.connect((ip, port))
        print('%s [已连通!]' % i)

    except Exception as e:
        print('%s %s' % (i,e))
        #print("%s server port not connect!" % i)
    sk.close()
二、多个机器测试远程端口是否正常:

脚本是由os的库popen方法抓到关键字段判断是否是连通,由于telnet这个命令他交互式的,python调用会卡主很久,这个问题是telnet命令未退出导致,有2种方法:

1、这种方法只限于端口开放或未开放

echo "" | telnet ${ip} ${port}

2、当上面的方法出现time out时候,也会卡主很久等待超时,此时加了timeout命令超时就kill进程

# timout_second:超时时间,秒
# host: 域名
# port: 端口
echo -e '\x1dclose\x0d' | timeout --signal=9 {timout_second} telnet {host} {port}

  脚本如下 :

#!/usr/bin/python3
import os,sys
import time


def src_list_ip(src_ip):
    list_ip = []
    for line in src_ip.strip().split('\n'):
        line = line.strip()
        if not line:
            continue
        list_ip.append(line)
    return list_ip

def check(dict_ip,des_ip):
    for k,v in dict_ip.items():
        for i in v:
            #print(i)
            ip= i.split('#')[0].strip()
            Eip = i.split('#')[-1].strip()
            for line in des_ip.strip().split('\n'):
                line = line.strip()
                des_ip_port = line.split(',')[0]
                #desc = line.split(',')[-1]
                cmd = "ssh %s \"echo -e '\x1dclose\x0d' |timeout --signal=9 1 telnet %s\"" % (ip,des_ip_port)
                #print(cmd)
                result=os.popen(cmd).read()

                if 'Escape' in result:
                    msg = 'ip:%s,EIP:%s到%s 已连通!!!' % (ip,Eip,line)
                else:
                    msg = 'ip:%s,EIP:%s到%s 不通!!!' % (ip,Eip,line)
                print(msg)
                with open(logFile, 'a', encoding='utf-8') as f:
                    f.write(msg+'\n')
                time.sleep(1)




if __name__ == '__main__':
    
    # 0.0.0.0代表本机ip
    # #1.1.1.1代表外网ip,由于本机ip都是内网ip,发来检查的都是公网,所以对应加上去好分辨
    src_dev_ip = '''
    0.0.0.0 #1.1.1.1
    ...
    '''
    src_pre_ip = '''
    0.0.0.0 #1.1.1.1
    ...
    '''

    src_pro_ip = '''
    0.0.0.0 #1.1.1.1
    ...
    '''

    ip_port = '''
    0.0.0.0 80,http的80端口
    ...
    '''
    dev_dict = {'dev': src_list_ip(src_dev_ip)}
    pre_dict = {'pre': src_list_ip(src_pre_ip)}
    pro_dict = {'pro': src_list_ip(src_pro_ip)}
    # print(dev_dict)
    # print(pre_dict)
    # print(pro_dict)
    logFile = '%s.log' % sys.argv[0][:-3]
    if os.path.exists(logFile):
        os.system('rm -fr %s' % logFile)

    print('-------dev开发测试环境端口检测中......-------')
    check(dev_dict, ip_port)
    print('-------pre预生产环境端口检测中......-------')
    check(pre_dict, ip_port)
    print('-------pro生产环境端口检测中......-------')
    check(pro_dict, ip_port)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/736459.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存