swdunlop / namap.py secret
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View namap.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#!/usr/bin/env python
 
import BeautifulSoup
import subprocess, threading, os, os.path, sys
 
class ShellException( Exception ):
def __init__( self, command, output, status ):
self.command = command
self.output = output
self.status = status
 
def printout( prefix, data ):
data = data.rstrip()
if not data: return ''
print prefix + data.replace( '\n', '\n' + prefix )
 
def sh( command, no_echo=False, no_fail=False, no_wait=False ):
if not no_echo:
printout( '>>> ', repr( command ) )
 
process = subprocess.Popen(
command,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
stdin = None,
shell = True if isinstance( command, str ) else False
)
if no_wait: return process
 
output, _ = process.communicate( )
status = process.returncode
 
if status:
if not no_echo: printout( '!!! ', output )
if not no_fail: raise ShellException( command, output, status )
else:
if not no_echo: printout( '::: ', output )
 
return output
 
def open_ports( data, addr ):
tag = data.find( addr = addr )
if not tag: return []
host = tag.parent
return list( x.parent["portid"] for x in host.findAll(
'state', state='open'
) )
 
def seq( *args ):
return args
 
def namap( path, *addrs ):
name = os.path.splitext( path )[0]
data = BeautifulSoup.BeautifulStoneSoup( open( path ).read() )
for addr in addrs:
ports = open_ports( data, addr )
scan = sh(seq( "amap", "-bq", addr, *ports ))
open( name + "-" + addr + ".amap", "w" ).write( scan )
 
if __name__ == "__main__":
if len( sys.argv ) > 2:
namap( *sys.argv[1:] )
else:
print >>sys.stderr, "USAGE: namap <nmap-xml> <addr1> <addr2> ..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.