Mercurial > hg > Applications > virsh-wrapper
view port_scan.py @ 38:16840a2375ae
Adapt to CentOS7
author | atton |
---|---|
date | Fri, 06 Nov 2015 21:51:43 +0900 |
parents | 83d66786f576 |
children |
line wrap: on
line source
#!/usr/bin/python from optparse import OptionParser import socket def h2ip(host): try: ip = socket.gethostbyname(host) return ip except: return None def connect_to(host, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host,port)) return s except: s.close() return None def bgrabber(sock): try: sock.send("port scan now!") banner = sock.recv(1024) return banner except: return None def scan(host, port): sock = connect_to(host, port) socket.setdefaulttimeout(5) if sock: print("++ Connect %s:\t%d" %(host, port)) banner = bgrabber(sock) if banner: print("++ Grab banner :\n\t%s" %banner) else: print("-- Can't grab the target banner") sock.close() else: print("-- Not connect %s:\t%d" % (host, port)) if __name__=="__main__": parser=OptionParser() parser.add_option("-t", "--target", dest="host", type="string", help="enter host name", metavar="exemple.com") parser.add_option("-p", "--port", dest="ports", type="string", help="port you want to scan separated by comma", metavar="PORT") (options, args) = parser.parse_args() if options.host == None or options.ports == None: parser.print_help() else: host = options.host ports = (options.ports).split(",") try: ports = list(filter(int, ports)) ip = h2ip(host) if ip: print("++ Running scan on %s"%host) print("++ Target IP: %s"%ip) for port in ports: scan(host, int(port)) else: print("-- Invalid host") except: print("Invalid port list (e.g: -p 21,22,53,..)")