-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransipping.py
executable file
·61 lines (53 loc) · 2.13 KB
/
transipping.py
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
59
60
#!/usr/bin/python3
import subprocess
import re
def get_interfaces_and_ip_addresses():
interfaces = {}
result = subprocess.run(['ip', '-4', 'addr'], stdout=subprocess.PIPE, text=True)
addresses = result.stdout.splitlines()
current_interface = None
for line in addresses:
if line.startswith(' '):
if 'inet ' in line and current_interface:
match = re.search(r'inet (\d+\.\d+\.\d+\.\d+)/', line)
if match:
ip_address = match.group(1)
interfaces[current_interface].append(ip_address)
else:
match = re.search(r'^\d+: (\w+):', line)
if match:
current_interface = match.group(1)
interfaces[current_interface] = []
return interfaces
def get_gateway(interface):
result = subprocess.run(['ip', 'route', 'show', 'dev', interface, 'default'], stdout=subprocess.PIPE, text=True)
routes = result.stdout.splitlines()
for route in routes:
parts = route.split()
if 'default' in parts:
gateway = parts[2]
return gateway
return None
def ping_gateway(source_ip, gateway):
try:
result = subprocess.run(['ping', '-I', source_ip, '-c', '4', gateway], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
print(f"Ping to {gateway} from IP {source_ip} was successful.")
else:
print(f"Ping to {gateway} from IP {source_ip} failed.")
print(result.stdout)
print(result.stderr)
except Exception as e:
print(f"Error pinging {gateway} from IP {source_ip}: {e}")
def main():
interfaces = get_interfaces_and_ip_addresses()
for interface, ip_addresses in interfaces.items():
gateway = get_gateway(interface)
if gateway:
for ip_address in ip_addresses:
print(f"Pinging gateway {gateway} from IP {ip_address} on interface {interface}")
ping_gateway(ip_address, gateway)
else:
print(f"No gateway found for interface {interface}")
if __name__ == '__main__':
main()