Skip to content

Commit

Permalink
Update for support windows
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanNyschuk1 committed May 31, 2019
1 parent dad18bd commit 0341637
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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
-----------------------------
Expand Down
95 changes: 55 additions & 40 deletions traceroute.py
Expand Up @@ -2,6 +2,10 @@
import random
import struct
import time
import platform
import os
import re


__all__ = ['Tracer']

Expand All @@ -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):
"""
Expand Down
16 changes: 11 additions & 5 deletions traceroute_tags.py
Expand Up @@ -10,6 +10,7 @@
from xmljson import badgerfish as bf
import time
from traceroute import Tracer
import platform


logger = logging.getLogger('log')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit 0341637

Please sign in to comment.