Skip to content

Instantly share code, notes, and snippets.

@chadyred
Last active May 22, 2018 15:02
Show Gist options
  • Save chadyred/d5882e328cb5922375c7c638c9f6db3c to your computer and use it in GitHub Desktop.
Save chadyred/d5882e328cb5922375c7c638c9f6db3c to your computer and use it in GitHub Desktop.
East sniffer
#!/usr/bin/python3
import abc
import time, json, sys
from typing import Callable, IO, cast
import shutil
from subprocess import Popen as popen_call # Execute a shell command
from subprocess import call as system_call # Execute a shell command
import attr
@attr.s
class NetworkValue(object):
"""Network side to analyse"""
ip = attr.ib(default=[])
@attr.s
class NmapValue(object):
"""Network side to analyse"""
opts = attr.ib(default=attr.Factory(list))
class Sniffer(metaclass=abc.ABCMeta):
@abc.abstractmethod
def ping_network_with_then_do(self, network_value: 'NetworkValue', action: Callable[ [str], None] ) -> 'Sniffer':
"""could ping a network"""
@abc.abstractmethod
def scan_network_with_then_do(self, nmap_value: 'NmapValue', network_value: 'NetworkValue', action: Callable[ [str], None] ) -> 'Sniffer':
"""nmap please start show message"""
class NetworkSniffer(Sniffer):
def ping_network_with_then_do(self, network_value: 'NetworkValue', action: Callable[ [str], None] ) -> 'Sniffer':
"""when start show message"""
action(system_call(['ping', '-c 4', network_value.ip]))
return self
def scan_network_with_then_do(self, nmap_value: 'NmapValue', network_value: 'NetworkValue', action: Callable[ [str], None] ) -> 'Sniffer':
"""when start show message"""
action(popen_call('nmap ' + nmap_value.opts + ' ' + network_value.ip, shell=True))
return self
class MessageTemplating(metaclass=abc.ABCMeta):
@abc.abstractmethod
def format_message(self, message: 'Messagerable') -> str:
"""Message factoring"""
class Messagerable(metaclass=abc.ABCMeta):
@abc.abstractmethod
def print_with_on(self, message: str, messagerTemplate : 'MessageTemplating', stream: IO[str]) -> 'Messagerable':
"""Message factoring"""
class Messager(Messagerable):
def print_with_on(self, message: str, messagerTemplate : 'MessageTemplating', stream: IO[str]) -> 'Messagerable':
messagerTemplate.format_message(
message,
lambda message_formatted: stream.write(message_formatted)
)
return self
class MessagerTemplate(MessageTemplating):
def format_message(self, message: str, action: Callable[[str], None] = None) -> str:
"""Show notice"""
action("Notice : " + message + '\n')
return self
if __name__ == '__main__':
NetworkSniffer().ping_network_with_then_do(
NetworkValue(ip="ip.or.your.domain"),
lambda result: Messager().print_with_on(
str(result),
MessagerTemplate(),
sys.stdout
)
)
NetworkSniffer().scan_network_with_then_do(
NmapValue(opts="-sV -p-"),
NetworkValue(ip="ip.or.your.domain"),
lambda result: Messager().print_with_on(
str(result),
MessagerTemplate(),
sys.stdout
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment