Example of Python3 Batch Scan Port

  • 2021-07-26 08:28:06
  • OfStack

Usage

This script is used to scan ports in batches

1. Create an input file in the same directory, and the attribute inputFile is the input file name

2. The attribute th is the number of threads

3. Attribute port is the target port for probing

4. Property arg is the default command

It is not recommended to scan all ports, which is very slow. There are common ports in the script, which can be used directly

The port corresponding service under the default command may be inaccurate. You can add-sV to detect the version in detail, or use this script to detect the output of this script specially

Code


# Dependency python-nmap,openpyxl Bag 
import nmap
import openpyxl
import IPy
from multiprocessing import Pool as ThreadPool

#  Create under the same directory 1 A input.txt, Put in ip Address column ( From xlsx Paste directly in )
expath=''
inputFile='2001_3000.txt'
th=20

# Scan port 
#port='1521,1158,2100,3306,1433,1434,5000,5432,27017,6379,11211,80,8080,443,8000,8081,7001,9080,9090,22,23,10027,3389,5631,111,6000,135,445,139,21,25,110,69,161,162,53,5901,5902,2601-2609 '
port='1-65535'
# Default command 
arg='-Pn -T4'
# Output file name 

import time
time_rq = time.strftime('%Y-%m-%d', time.localtime(time.time()))
outPutFile = time_rq + '2001_3000.txt'



def readfile(path):
  #  Return url List 
  file = open(path, 'r',encoding='utf-8')
  urlList=[]
  for eachline in file.readlines():
    # Get the address segment 
    url=eachline.rsplit()[0]
    # add=IPy.IP(url)# Address segment list 
    # for u in add:
    #   ip=u.__str__();
    #   urlList.append(ip)
    urlList.append(url);


  file.close()
  #  Make infoList
  infoList=[]
  for url in urlList:
    info={'hostname':url,'port':port,'arg':arg}
    infoList.append(info)

  return infoList

def scan(info):

  hostName=info['hostname']
  port=info['port']
  print(hostName+' Start of scanning ')

  scanner=nmap.PortScanner()
  scanner.scan(hosts=hostName,ports=port,arguments=arg)

  # print(res)
  out=open(outPutFile,'a+',encoding='utf-8')
  # out.write()
  state=scanner[hostName].state()
  tcp_ports=scanner[hostName].all_tcp()
  print(hostName+' '+state)
  for ports in tcp_ports:
    if scanner[hostName]['tcp'][ports]['state'] =='open' :
      st=hostName+' '+str(ports)+' '+scanner[hostName]['tcp'][ports]['name']+' '+scanner[hostName]['tcp'][ports]['state']+'\n'
      out.write(st)
      print(st)

  # print(hostName)
  out.close()


def makeEx():
  print('---------------- Start writing -------------------')
  wb=openpyxl.load_workbook(expath)
  sheet=wb.active
  c=open(outPutFile,'r',encoding='utf-8')
  i=1
  for line in c.readlines():
    linelist=line.split()
    ip=linelist[0]
    port=linelist[1]
    name=linelist[2]
    status=linelist[3]
    for j in range(4):
      if j==3:
        n=linelist[j]
        n=n.rsplit()
        n=n[0]
        sheet.cell(row=i, column=j + 1, value=n)
      else:
        sheet.cell(row=i,column=j+1,value=linelist[j])
    i=i+1
  c.close()
  wb.save(expath)
  print('---------------- Write complete -------------------')


if __name__ == '__main__':
  print('---------------- Start of scanning -------------------')
  start=time.time()
  pool = ThreadPool(th)
  infolist=readfile(inputFile)
  pool.map(scan,infolist)
  pool.close()
  pool.join()
  finish=time.time()
  print('---------------- Scan complete -------------------')
  t=finish-start
  print(' Time  %f'%t)
  out = open(outPutFile, 'a+', encoding='utf-8')
  out.write('\n Time  %f'%t)

  # makeEx()
  # info={'hostname':'135.233.115.55','port':'22,23,21,80,3389,5901,5902','arg':'-Pn -sV -T4'}

Sample input file


61.128.108.94
61.128.111.246
61.128.122.111
222.83.20.18
61.128.108.67
120.70.226.131

Sample output file


106.124.93.76 22 ssh open
106.124.93.76 23 telnet open
106.124.93.76 8000 http-alt open
106.124.93.83 23 telnet open

Related articles: