酷帥王子'blog-思想激进者,一搞网络安全的市井小儿,随波逐流之辈也!

python2.7版多线程端口扫描

2018-8-9 9:19 作者:酷帥王子 | python网络安全 |

#coding:utf-8
#coding:gbk
#!/usr/bin/python
# Python:          2.7
# Platform:        Windows
# Program:         端口扫描
# History:         2017.1.22

import socket, time, thread
timeout=6
socket.setdefaulttimeout(timeout)

def socket_port(ip,port):
   """
   输入IP和端口号,扫描判断端口是否开放
   """
   try:
       if port>=65535:
           print '端口扫描结束'
       s=socket.socket()
       result=s.connect_ex((ip,port))
       if result==0:
           lock.acquire()
           print  ip,':',port,'端口开放'
           lock.release()
       s.close()
   except:
       print '端口扫描异常'

def ip_scan(ip):
   """
   输入IP,扫描IP的0-65534端口情况
   """
   try:
       print '开始扫描 %s' % ip
       start_time=time.time()
       for i in range(0,65534):
           thread.start_new_thread(socket_port,(ip,int(i)))
       print '扫描端口完成,总共用时 :%.2f' %(time.time()-start_time)
       raw_input("Press Enter to Exit")
   except:
       print '扫描ip出错'
       

if __name__=='__main__':
   url=raw_input('请输入要扫描的ip:\n')
   lock=thread.allocate_lock()
   ip_scan(url)

文章作者:酷帥王子
文章地址:https://6kb.org:443/post-6.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

发表评论:



Powered by 酷帥王子

CopyRight © 2009-2016 酷帥王子'blog.  All rights reserved.