diff --git a/README.md b/README.md index e1defa7..39a5878 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Prior to using the script, it must be configured to connect to your Device42 ins ### Compatibility ----------------------------- -* Script runs on Linux as root privilege. +* Script runs on Linux as root privilege and Windows. ### Info ----------------------------- diff --git a/traceroute.py b/traceroute.py index 84eb5b2..82646d6 100644 --- a/traceroute.py +++ b/traceroute.py @@ -2,6 +2,10 @@ import random import struct import time +import platform +import os +import re + __all__ = ['Tracer'] @@ -28,48 +32,59 @@ def run(self): Raises: IOError """ - try: - dst_ip = socket.gethostbyname(self.dst) - except socket.error as e: - raise IOError('Unable to resolve {}: {}', self.dst, e) - - text = 'traceroute to {} ({}), {} hops max'.format( - self.dst, - dst_ip, - self.hops - ) - - print(text) - - while True: - startTimer = time.time() - receiver = self.create_receiver() - sender = self.create_sender() - sender.sendto(b'', (self.dst, self.port)) - - addr = None + if platform.system().lower() == "linux": try: - data, addr = receiver.recvfrom(1024) - entTimer = time.time() + dst_ip = socket.gethostbyname(self.dst) except socket.error as e: - print(str(e)) - pass - # raise IOError('Socket error: {}'.format(e)) - finally: - receiver.close() - sender.close() - - if addr: - timeCost = round((entTimer - startTimer) * 1000, 2) - if addr[0] == dst_ip: - return True - else: - print('{:<4} *'.format(self.ttl)) - - self.ttl += 1 - - if self.ttl > self.hops: - return False + raise IOError('Unable to resolve {}: {}', self.dst, e) + + text = 'traceroute to {} ({}), {} hops max'.format( + self.dst, + dst_ip, + self.hops + ) + + print(text) + + while True: + startTimer = time.time() + receiver = self.create_receiver() + sender = self.create_sender() + sender.sendto(b'', (self.dst, self.port)) + + addr = None + try: + data, addr = receiver.recvfrom(1024) + entTimer = time.time() + except socket.error as e: + print(str(e)) + pass + # raise IOError('Socket error: {}'.format(e)) + finally: + receiver.close() + sender.close() + + if addr: + if addr[0] == dst_ip: + return True + else: + print('{:<4} *'.format(self.ttl)) + + self.ttl += 1 + + if self.ttl > self.hops: + return False + else: # Windows + cmd = "tracert -d -h %d -w %d %s" % (self.hops, self.timeout, self.dst) + p = os.popen(cmd) + output = p.read() + lines = output.splitlines() + for line in lines: + x = re.search(r"^\s*\d+.*$", line) + if x is not None: + if line.find(self.dst) != -1: + return True + return False def create_receiver(self): """ diff --git a/traceroute_tags.py b/traceroute_tags.py index 9c13e61..830bfa9 100644 --- a/traceroute_tags.py +++ b/traceroute_tags.py @@ -10,6 +10,7 @@ from xmljson import badgerfish as bf import time from traceroute import Tracer +import platform logger = logging.getLogger('log') @@ -94,7 +95,7 @@ def task_execute(settings, device42): if "@no-device" in settings["ip-tags"]: device42.set_ipaddress_tags(ip_address, ipaddress_tags, settings["ip-tags"]["@no-device"]) if source["ipaddress_pk"] is None: - if "@no-ipaddress" in settings["ip-tags"]: + if "@no-ipaddress" in settings["device-tags"]: device42.set_device_tags(device_name, device_tags, settings["device-tags"]["@no-ipaddress"]) else: tracert_result = tracert(ip_address, settings) @@ -139,7 +140,12 @@ def main(): if __name__ == "__main__": - print('Running...') - ret_val = main() - print('Done') - sys.exit(ret_val) + system = platform.system().lower() + if system != 'windows' and system != 'linux': + print("This script runs on Linux or Windows.") + sys.exit() + else: + print('Running...') + ret_val = main() + print('Done') + sys.exit(ret_val)