Created
October 30, 2018 07:20
-
-
Save Asad-Khan-Aasanjobs/b612ca901d7df9ec1e1b8baca75b5b93 to your computer and use it in GitHub Desktop.
Scanner Class which takes a GCloud compute instance object and returns port scanning results
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
from nmap import PortScanner | |
from gcloud import ComputeInstance | |
PORT_RANGE = "1-65535" | |
class InstancePortScanner: | |
def __init__(self, instance: ComputeInstance): | |
self.instance = instance | |
self.scanner = PortScanner() | |
def print_info(self, msg): | |
print("[Instance:{}] INFO {}".format(self.instance.name, msg)) | |
def print_err(self, msg): | |
print("[Instance:{}] **ERROR** {}".format(self.instance.name, msg)) | |
def scan(self): | |
results = self.scanner.scan(self.instance.external_ip, PORT_RANGE) | |
self.print_info("Executed command: {}".format(self.scanner.command_line())) | |
if not results["scan"].get(self.instance.external_ip): | |
self.print_err("Failed to scan IP address.") | |
return None | |
tcp_results = [] | |
for port, result in results["scan"][self.instance.external_ip].get("tcp", {}).items(): | |
tcp_results.append({ | |
"port": port, | |
"status": result.get("state") | |
}) | |
return tcp_results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment