-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_task.py
More file actions
58 lines (50 loc) · 1.92 KB
/
Copy path1_task.py
File metadata and controls
58 lines (50 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import subprocess
import requests
import json
import socket
import sys
jump_count = "20"
timeout = "200"
last_addr = ""
hlp = "Сероев Егор Игоревич МО-201 МЕН-282201\nИспользование: python " +sys.argv[0][:-3] + ".py [ip/address]"
def valid_addr(addr):
try:
ip = socket.gethostbyname(addr)
return ip
except socket.error:
return False
def clean_ip(ip):
if(ip[0] == "[" and ip[len(ip)-1] == "]"):
return ip[1:len(ip)-1]
return ip
def print_info(res):
if(len(res) != 0 and res[0].isdigit() and res[1] != "*"):
ip = clean_ip(res[len(res) - 1])
response = requests.get("http://ip-api.com/json/" + ip)
j = json.loads(response.text)
if (j["status"] == "success"):
print(res[0] + " " + ip + " " + j["as"][2:].split()[0] + " " + j["isp"] + " " + j["country"])
else:
print(res[0] + " " + ip + " " + "Неизвестный адрес")
global last_addr
last_addr = ip
if (len(sys.argv) == 2):
addr = sys.argv[1]
if(valid_addr(addr)):
process = subprocess.Popen(["tracert", "-w", timeout, "-h", jump_count, addr], stdout=subprocess.PIPE)
while True:
output = process.stdout.readline()
if output == b'' and process.poll() is not None:
break
if output:
res = output.strip().decode('cp866').split()
print_info(res)
if(valid_addr(last_addr) == valid_addr(addr)):
print("Трассировка завершена. Последний адрес совпадает с запросом")
else:
print("Трассировка не завершена. Последий адрес не совпадает с запросом")
else:
print("Неверный адрес")
print(hlp)
else:
print(hlp)