扫描端口
忘记了实验室的ip,但是知道网段。ps.速度有点慢
[python]
#! /usr/bin/env python
#-*- coding=utf-8 -*-
import socket
def scan(ipaddr,port):
if len(port)<1:
port=3389#默认端口
s=socket.socket()
for p in range(253,2,-1):
addr=ipaddr+"."+str(p)
try:
s.connect((addr,port))
print addr
except socket.error,e:
pass
if __name__=='__main__':
ipaddr=raw_input("please input the ip segment:")
port=raw_input("please input the port:")
scan(ipaddr,port)
输入:
192.168.0
80
这是开启多线程进行扫描:
[python]
#! /usr/bin/env python
#-*- coding=utf-8 -*-
import threading , time , queue , socket
port = [139,445,80,135,137,1433,3389,8080,9015,137,21,23,25,1502]#扫的常用端口。
class scanthread(threading.thread):
def run(self):
global queue#,q
ip = queue.get()
for p in port:#其实这里可以in range() 就可以添加大点的端口范围了。
s = socket.socket()
s.settimeout(3)
try:
s.connect((ip,int(p)))
print '\n========================================================='
print "ip:%s , port:%d , open" %( ip , int(p) )
print '=========================================================\n'
sp = "ip:%s , port:%d , open" %( ip , int(p) )
#q.put("%s %d",ip,int(p))
except:
pass
s.close()
queue.task_done()
target = raw_input("输入网段(192.168.12):")
x=target.split(".")
if len(x)>3:
target=x[0]+"."+x[1]+"."+x[2]
#q=queue.queue()
queue = queue.queue()
for ip in range(254,2,-1):
queue.put(target+".%d" % ip)
for p in range(254,2,-1):
scanthread().start()
queue.join()
上一篇: 从零学Python之入门(三)序列
下一篇: Python基类函数的重载与调用实例分析