Skip to content

Commit 976180b

Browse files
committed
es traceroute resource script
1 parent 90eb83c commit 976180b

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

resource-scripts/es_ping/readme.MD

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from es_traceroute import run_es_traceroute
2+
run_es_traceroute()
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from cloudshell.helpers.scripts.cloudshell_dev_helpers import attach_to_cloudshell_as
2+
from es_traceroute import run_es_traceroute
3+
4+
LIVE_SANDBOX_ID = "87df6742-0fd6-43e4-b068-48764b97d7e5"
5+
TARGET_RESOURCE_NAME = "DUT mock 1"
6+
7+
attach_to_cloudshell_as(user="admin",
8+
password="admin",
9+
domain="Global",
10+
reservation_id=LIVE_SANDBOX_ID,
11+
server_address="localhost",
12+
resource_name=TARGET_RESOURCE_NAME)
13+
14+
run_es_traceroute()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from cloudshell.helpers.scripts.cloudshell_scripts_helpers import get_reservation_context_details, get_api_session, \
2+
get_resource_context_details
3+
from cloudshell.logging.qs_logger import get_qs_logger
4+
from cloudshell.helpers.sandbox_reporter.reporter import SandboxReporter
5+
from traceroute_helper import run_traceroute_to_ip, get_local_ip
6+
7+
8+
class TraceRouteException(Exception):
9+
pass
10+
11+
12+
def run_es_traceroute():
13+
api = get_api_session()
14+
sb_details = get_reservation_context_details()
15+
sb_id = sb_details.id
16+
17+
resource_details = get_resource_context_details()
18+
resource_name = resource_details.name
19+
logger = get_qs_logger(log_group=sb_id, log_category=resource_details.model, log_file_prefix=resource_name)
20+
reporter = SandboxReporter(api, sb_id, logger)
21+
ip = resource_details.address
22+
es_ip = get_local_ip()
23+
24+
reporter.info(f"Running traceroute to '{resource_name}' at IP '{ip}' from Execution Server...")
25+
26+
try:
27+
tr_output = run_traceroute_to_ip(ip)
28+
except Exception as e:
29+
msg = f"Failed ping to '{resource_name}' at IP '{ip}' from ES at IP '{es_ip}'.\n{str(e)}"
30+
reporter.error(msg)
31+
raise TraceRouteException(msg)
32+
33+
print(tr_output)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cloudshell-automation-api
2+
cloudshell-logging
3+
cloudshell-sandbox-reporter
4+
icmplib
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import socket
2+
from icmplib import traceroute
3+
4+
5+
def get_local_ip():
6+
"""
7+
Use socket to get the local IP, default to loopback 127.0.0.1
8+
https://stackoverflow.com/a/28950776
9+
:return:
10+
"""
11+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
12+
s.settimeout(0)
13+
try:
14+
# doesn't even have to be reachable
15+
s.connect(('10.255.255.255', 1))
16+
IP = s.getsockname()[0]
17+
except Exception:
18+
IP = '127.0.0.1'
19+
finally:
20+
s.close()
21+
return IP
22+
23+
24+
def run_traceroute_to_ip(target_ip: str) -> str:
25+
tr_hops = traceroute(address=target_ip)
26+
full_output = "\n".join([str(hop) for hop in tr_hops])
27+
return full_output
28+
29+
30+
if __name__ == "__main__":
31+
res = run_traceroute_to_ip("google.com")
32+
print(res)

0 commit comments

Comments
 (0)