监听目标不同服务器不同端口,将可以tcp连接的服务器ip以及端口写入相应的文件

是一个小脚本!

服务端 处理需要自己去验证!

import os, sys,  pickle

import threading
import pexpect
import re
import logging
import socket

targetPath = ""
curPath = os.getcwd()
targetPath = curPath + '/'+'ip_name.log'

getServerIpList = []
getServerNameList = []
linkServerIpList = []
ServerNameList = []
tcpPortList =  [21000, 23000,22000,24000,25000,26000,27000]
portList = []
#获取所有的服务器IP和名字列表
def getServerIpByReadFiles():
    if os.path.isfile(targetPath):
        with open(targetPath, 'r+') as fd:
            while True:
                read_info = fd.readline()
                print(read_info)
                if len(read_info) > 0:
                    serverIp = re.search(r"(\d*?)\.(\d*?)\.(\d*?)\.(\d+)", read_info).group().strip()
                    
                    serverName = re.search(r'[a-z]{5,7}', read_info).group().strip()
                    
                    getServerIpList.append(serverIp)                
                    getServerNameList.append(serverName)
                    
                else:
                    print("读取服务器IP和名字完毕!")
                    break

#将所有服务器IP和名字写入文件
def ip_Name_File():
    print(getServerIpList)
    print(len(getServerIpList))
    
    openfile = open('ip-name.log', 'w')
    for index in range(len(getServerIpList)):
        
        
        openfile.write(getServerIpList[index] + ':' + getServerNameList[index] + '\n')
        
    
    print("文件写入成功!")    
    openfile.close()
        
#获取tcp服务器的IP列表
def tcpClientLinkTest():   
    print("tcp server link test Start###################")
    clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    clientSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    num = 0
    
    for index, serverIp in enumerate (getServerIpList):       
        try: 
            print(serverIp)
            print(tcpPortList[0])
            clientSocket.connect((serverIp, tcpPortList[0]))
              
            clientSocket.send(b"i am a tcp client!")

            recvdata = clientSocket.recv(2048)
            if len(recvdata):
                print("recvData from server ip: %s :架构:%s" % (serverIp, recvdata.decode("gb2312")))
            clientSocket.send(b"quit")
            if len(linkServerIpList) > 0:
                for i in range(len(linkServerIpList)):
                    if serverIp == linkServerIpList[i]:
                        continue
                        
                    else:
                        num += 1
                        if num == len(linkServerIpList):
                            linkServerIpList.append(serverIp)
                            ServerNameList.append(getServerNameList[index])

            else:
                linkServerIpList.append(serverIp)
                ServerNameList.append(getServerNameList[index])       
            tmpPort = recvdata.decode("gb2312") + '-' + serverIp[-2] + serverIp[-1]
            print(tmpPort)
            portList.append(tmpPort)                                 
            clientSocket.close()

        except Exception as e:
            print(e)
            continue
    

#将当前可TCP连接的服务器IP写入相应文档

def writeTCPServerLog():
    print("TCP Server can connect include %s"%linkServerIpList)
    with open('tcpserver.log', 'w') as openfd:
        for index in range(len(linkServerIpList)):
            openfd.write("<option value='server_value'>" + linkServerIpList[index] + "</option>")
        print("tcpserver.log write successfully!")

def writeSystemLog():
    print(portList)
    with open('sys.log', 'w') as fd:
        for index in range(len(portList)):
            fd.write('<option value="1">' + portList[index] + '</option>')
        print('system.log write successfully!!')
#遍历所有可以tcp连接的server
def viewLinkServerIpList():
    flags = False
    if len(linkServerIpList):

        print("These Server Ip can connect Now!")
        for index, serverIp in enumerate(linkServerIpList):
            print("serverIP:%s can be connected!"%serverIp)
        for attr in portList:
            print('the port can use:' + str(attr))
        
        return len(linkServerIpList)
        flags = True
        return flags
    else:
        print("No server can be connected!")
        return flags




if __name__ == "__main__":
    getServerIpByReadFiles()
    ip_Name_File()
    for i in range(len(tcpPortList)): //遍历端口
        tcpClientLinkTest()
        tcpPortList.remove(tcpPortList[0])

    viewLinkServerIpList()
    writeTCPServerLog()
    writeSystemLog()



结果类似于这样:

These Server Ip can connect Now!
serverIP:10.7.0.21 can be connected!
serverIP:10.7.0.20 can be connected!
the port can use:mips_32-21
the port can use:ARM-21
the port can use:PPC-20
TCP Server can connect include ['10.7.0.21', '10.7.0.20']
tcpserver.log write successfully!
['mips_32-21', 'ARM-21', 'PPC-20']
system.log write successfully!!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值