PortScan
时间:2009-07-24 来源:leogeng1
老大给分配的第一个小项目,用python写一个扫描端口!
#!/usr/bin/env python
# Package definitions.
__program__ = 'PortScan'
__version__ = '0.1a'
__date__ = '2009/24/07'
__author__ = 'leogeng'
__licence__ = 'GPL'
__copyright__ = 'Copyright (C) 2009 Lars Strand' import socket
import sys
from time import time,ctime
from socket import * host = sys.argv[1]
start = int(sys.argv[2])
end = int(sys.argv[3])
def scan_tcp(host, start_port, end_port):
for port in range(start_port, end_port + 1):
try:
s = socket(AF_INET, SOCK_STREAM)
except:
print "socket error"
sys.exit(1)
s.settimeout(0.1)
try:
s.connect((host, port))
except:
pass
else:
s.close()
#serv = socket.getservbyport(port)
#print str(port) + "\t" + serv
print str(port) + ' tcp port is open'
def scan_udp(host, start_port, end_port):
for port in range(start_port, end_port + 1):
try:
s = socket(AF_INET, SOCK_DGRAM)
except:
print "socket error"
sys.exit(1)
s.settimeout(0.1)
try:
s.sendto("",(host,port))
s.recvfrom(10)
except timeout:
#serv = socket.getservbyport(port)
#print str(port) + "\t" + serv
print str(port) + ' udp port is open'
except error:
continue
finally:
s.close()
def main():
print "Start at " + ctime(time())
scan_tcp(host, start, end)
scan_udp(host, start, end)
print 'Stop at ' + ctime(time())
if __name__ == '__main__':
main()
__program__ = 'PortScan'
__version__ = '0.1a'
__date__ = '2009/24/07'
__author__ = 'leogeng'
__licence__ = 'GPL'
__copyright__ = 'Copyright (C) 2009 Lars Strand' import socket
import sys
from time import time,ctime
from socket import * host = sys.argv[1]
start = int(sys.argv[2])
end = int(sys.argv[3])
def scan_tcp(host, start_port, end_port):
for port in range(start_port, end_port + 1):
try:
s = socket(AF_INET, SOCK_STREAM)
except:
print "socket error"
sys.exit(1)
s.settimeout(0.1)
try:
s.connect((host, port))
except:
pass
else:
s.close()
#serv = socket.getservbyport(port)
#print str(port) + "\t" + serv
print str(port) + ' tcp port is open'
def scan_udp(host, start_port, end_port):
for port in range(start_port, end_port + 1):
try:
s = socket(AF_INET, SOCK_DGRAM)
except:
print "socket error"
sys.exit(1)
s.settimeout(0.1)
try:
s.sendto("",(host,port))
s.recvfrom(10)
except timeout:
#serv = socket.getservbyport(port)
#print str(port) + "\t" + serv
print str(port) + ' udp port is open'
except error:
continue
finally:
s.close()
def main():
print "Start at " + ctime(time())
scan_tcp(host, start, end)
scan_udp(host, start, end)
print 'Stop at ' + ctime(time())
if __name__ == '__main__':
main()
相关阅读 更多 +