欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python3多线程版TCP端口扫描器

程序员文章站 2022-11-15 23:46:21
本文实例为大家分享了python3多线程版tcp端口扫描器的具体代码,供大家参考,具体内容如下 使用命令 python bannerdemo.py -h 192...

本文实例为大家分享了python3多线程版tcp端口扫描器的具体代码,供大家参考,具体内容如下

使用命令

python bannerdemo.py -h 192.168.200.101 -p 22,3306

代码如下

import optparse
import socket
from socket import *
from threading import *

screenlock = semaphore(value=1)

def connscan(tgthost,tgtport):
  try:
    connskt = socket(af_inet,sock_stream)
    connskt.connect((tgthost,tgtport))
    connskt.send('violentpython\r\n'.encode('utf-8'))
    result = connskt.recv(1024)
    screenlock.acquire()
    print("[+] %d/tcp open"%tgtport)
    print("[+]"+str(result))
  except exception as e:
    screenlock.acquire()
    print(e)
  finally:
    screenlock.release()
    connskt.close()


def portscan(tgthost,tgtports):
  try:
    tgtip = gethostbyname(tgthost)
  except:
    print("[-] cannot reslove '%s' : unknown host" % tgthost)
    return

  try:
    tgtname = gethostbyaddr(tgtip)
    print("\n [+] scan result for: " + tgtname[0])
  except:
    print("\n [+] scan result for: " + tgtip)

  setdefaulttimeout(1)
  for tgtport in tgtports:
    print("scanning port "+ tgtport)
    t = thread(target=connscan,args=(tgthost,int(tgtport)))
    t.start()

def main():
  parser = optparse.optionparser("usage%prog -h <target host> -p <target port>")
  parser.add_option('-h',dest='tgthost',type='string',help='specify target host')
  parser.add_option('-p',dest='tgtport',type='string',help='specify target port[s] separated by comma')
  options,args = parser.parse_args()
  tgthost = options.tgthost
  tgtports = str(options.tgtport).split(',')
  if(tgthost == none or tgtports[0] == none):
    print("[-] you must specify a target host and port[s]")
    exit(0)
  portscan(tgthost,tgtports)

if __name__ == '__main__':
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。