-
-
Save swdunlop/9c246ab58fbb29e73b7c to your computer and use it in GitHub Desktop.
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/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