Skip to content

Commit fa5c09d

Browse files
fixed #2 and updated command name.
1 parent c9cd5ab commit fa5c09d

3 files changed

Lines changed: 32 additions & 19 deletions

File tree

ip2trace.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@
4242
os.mkdir(default_path)
4343
copyfile(os.path.dirname(os.path.realpath(__file__)) + os.sep + "data" + os.sep + "IP2LOCATION-LITE-DB1.IPV6.BIN", default_path + "IP2LOCATION-LITE-DB1.IPV6.BIN")
4444
except PermissionError as e:
45-
sys.exit("Root permission is required. Please rerun it as 'sudo ip2trace'.")
45+
sys.exit("Root permission is required. Please rerun it as 'sudo ip2tracepy' in Linux, or obtain administrator permission in Windows.")
4646

4747
if sys.platform.startswith('win32'):
48-
timer = time.clock
48+
# timer = time.clock
49+
if sys.version_info > (3,2):
50+
timer = time.perf_counter
51+
else:
52+
timer = time.clock
4953
else:
5054
timer = time.time
55+
# timer = time.time
5156

5257
def calculate_checksum(packet):
5358
countTo = (len(packet) // 2) * 2
@@ -94,8 +99,14 @@ def to_ip(hostname):
9499

95100
def ip_to_domain_name(hostname):
96101
if is_valid_ip(hostname):
97-
return socket.gethostbyaddr(hostname)
98-
return hostname
102+
# return socket.gethostbyaddr(hostname)
103+
try:
104+
return socket.gethostbyaddr(hostname)
105+
except socket.herror:
106+
# return hostname
107+
return None
108+
# return hostname
109+
return None
99110

100111

101112
def create_parser():
@@ -113,8 +124,8 @@ def create_parser():
113124

114125
def print_usage():
115126
print(
116-
"Usage: ip2trace -p [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS]\n"
117-
" or: ip2trace [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS]\n\n"
127+
"Usage: ip2tracepy -p [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS]\n"
128+
" or: ip2tracepy [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS]\n\n"
118129
" -p, --ip\n"
119130
" Specify an IP address or hostname.\n"
120131
" The -p/--ip can be omitted if the IP address or hostname is defined in the first parameter.\n"
@@ -140,7 +151,7 @@ def print_usage():
140151

141152
def print_version():
142153
print(
143-
"IP2Location Geolocation Traceroute (ip2trace) Version 2.1.7\n"
154+
"IP2Location Geolocation Traceroute (ip2trace) Version 3.0.0\n"
144155
"Copyright (c) 2021 IP2Location.com [MIT License]\n"
145156
"https://www.ip2location.com/free/traceroute-application\n")
146157

@@ -204,7 +215,7 @@ def __init__(self, destination_server, database, max_hops, output, all):
204215
if (os.path.isfile(default_path + "IP2LOCATION-LITE-DB1.IPV6.BIN") != False):
205216
self.obj = IP2Location.IP2Location(default_path + "IP2LOCATION-LITE-DB1.IPV6.BIN")
206217
else:
207-
print("Missing IP2Location BIN database. Please enter ip2trace -h for more information.")
218+
print("Missing IP2Location BIN database. Please enter 'ip2trace -h' for more information.")
208219
sys.exit()
209220

210221
# check the output list
@@ -215,12 +226,14 @@ def __init__(self, destination_server, database, max_hops, output, all):
215226
sys.exit()
216227

217228
def print_start(self):
218-
print("IP2Location Geolocation Traceroute (ip2trace) Version 2.1.7\n"
229+
print("IP2Location Geolocation Traceroute (ip2trace) Version 3.0.0\n"
219230
"Copyright (c) 2021 IP2Location.com [MIT License]\n"
220231
"https://www.ip2location.com/free/traceroute-application\n\n")
221-
# "Traceroute to", self.destination_domain_name, "(", self.destination_ip, ")\n\n")
222-
# "Traceroute to", self.destination_domain_name[0], "(", self.destination_ip, ")\n\n")
223-
print("Traceroute to", self.destination_domain_name[0], "(", self.destination_ip, ")\n\n", end="")
232+
# print("Traceroute to", self.destination_domain_name[0], "(", self.destination_ip, ")\n\n", end="")
233+
if self.destination_domain_name is not None:
234+
print("Traceroute to", self.destination_domain_name[0], "(", self.destination_ip, ")\n\n", end="")
235+
else:
236+
print("Traceroute to", self.destination_ip, "\n\n", end="")
224237

225238
def print_unknownhost(self):
226239
print("traceroute: unknown host {}".format(self.destination_server))
@@ -481,4 +494,4 @@ def main():
481494
# sys.exit()
482495
traceroute(destination_server, database, max_hops, output, all)
483496
else:
484-
print("Missing parameters. Please enter 'ip2trace -h' for more information.")
497+
print("Missing parameters. Please enter 'ip2trace -h' for more information.")

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ You can install this tool by using pip in Windows or Linux. To install this tool
1313
## Usage
1414

1515
```
16-
Usage: ip2trace -p [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS]
17-
or: ip2trace [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS]
16+
Usage: ip2tracepy -p [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS]
17+
or: ip2tracepy [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS]
1818
1919
-p, --ip
2020
Specify an IP address or hostname.
@@ -45,7 +45,7 @@ Usage: ip2trace -p [IP ADDRESS/HOSTNAME] -d [IP2LOCATION BIN DATA PATH] [OPTIONS
4545
Traceroute an IP address.
4646

4747
```bash
48-
ip2trace 8.8.8.8 -d /usr/share/ip2location/DB3.BIN -a
48+
ip2tracepy 8.8.8.8 -d /usr/share/ip2location/DB3.BIN -a
4949
```
5050

5151
Example output:
@@ -71,7 +71,7 @@ Traceroute to dns.google ( 8.8.8.8 )
7171
Traceroute by hostname
7272

7373
```bash
74-
ip2trace google.com -d /usr/share/ip2location/DB3.BIN -a
74+
ip2tracepy google.com -d /usr/share/ip2location/DB3.BIN -a
7575
```
7676

7777
Example output:

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
setuptools.setup(
88
name="IP2Trace",
9-
version="2.1.7",
9+
version="3.0.0",
1010
description="A Python tool to display geolocation information in the traceroute.",
1111
long_description_content_type="text/markdown",
1212
long_description=long_description,
1313
entry_points={
1414
'console_scripts': [
15-
'ip2trace=ip2trace:main'
15+
'ip2tracepy=ip2trace:main'
1616
]
1717
},
1818
py_modules=['ip2trace'],

0 commit comments

Comments
 (0)