Created
March 23, 2012 02:17
-
-
Save thermosilla/2166236 to your computer and use it in GitHub Desktop.
Python port scaner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python2.7 | |
import argparse | |
import socket | |
def prueba_host(host, puerto): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.settimeout(10) | |
try: | |
s.connect((host, int(puerto))) | |
s.shutdown(2) | |
return True | |
except: | |
return False | |
def prueba_puertos(host, puertos): | |
listap = puertos.split(" ") | |
resultado = [] | |
for p in listap: | |
if prueba_host(host,p): | |
resultado.append(host + ":" + p + " up") | |
else: | |
resultado.append(host + ":" + p + " down") | |
return resultado | |
def lee_archivo(archivo): | |
f = open(archivo, "r") | |
resultado = [] | |
for l in f: | |
lista = l.replace("\n","").split(" ") | |
host = lista[0] | |
lista.remove(host) | |
resultado.extend(prueba_puertos(host,' '.join(lista))) | |
return resultado | |
def main(): | |
parser = argparse.ArgumentParser(description="Argumentos del script") | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('-a', '--archivo', default="", help="Archivo con listado de servicios a validar") | |
group.add_argument('-i', '--host', default="localhost", help="IP del host a revisar") | |
parser.add_argument('-p', '--puertos', default=['80','22'], nargs="*", help="puertos a comprobar") | |
args = parser.parse_args() | |
if args.archivo != "": | |
for l in lee_archivo(args.archivo): | |
print(l) | |
else: | |
for l in prueba_puertos(args.host, ' '.join(args.puertos)): | |
print(l) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment