File tree 6 files changed +101
-0
lines changed
6 files changed +101
-0
lines changed Original file line number Diff line number Diff line change
1
+ from es_ping import run_es_ping
2
+ run_es_ping ()
Original file line number Diff line number Diff line change
1
+ from cloudshell .helpers .scripts .cloudshell_dev_helpers import attach_to_cloudshell_as
2
+ from es_ping import run_es_ping
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_ping ()
Original file line number Diff line number Diff line change
1
+ import json
2
+
3
+ from cloudshell .helpers .scripts .cloudshell_scripts_helpers import get_reservation_context_details , get_api_session , \
4
+ get_resource_context_details
5
+ from cloudshell .logging .qs_logger import get_qs_logger
6
+ from cloudshell .helpers .sandbox_reporter .reporter import SandboxReporter
7
+ from ping_parser_helper import ping_target_ip , get_local_ip , PingFailedException
8
+
9
+
10
+ def run_es_ping ():
11
+ api = get_api_session ()
12
+ sb_details = get_reservation_context_details ()
13
+ sb_id = sb_details .id
14
+
15
+ resource_details = get_resource_context_details ()
16
+ resource_name = resource_details .name
17
+ logger = get_qs_logger (log_group = sb_id , log_category = resource_details .model , log_file_prefix = resource_name )
18
+ reporter = SandboxReporter (api , sb_id , logger )
19
+ ip = resource_details .address
20
+ es_ip = get_local_ip ()
21
+
22
+ reporter .info (f"Pinging '{ resource_name } ' at IP '{ ip } ' from Execution Server..." )
23
+
24
+ try :
25
+ stats = ping_target_ip (ip )
26
+ except PingFailedException as e :
27
+ msg = f"Failed ping to '{ resource_name } ' at IP '{ ip } ' from ES at IP '{ es_ip } '.\n { str (e )} "
28
+ reporter .error (msg )
29
+ raise
30
+ reporter .success (f"Successful ping from ES to '{ resource_name } ' at IP '{ ip } '" )
31
+ print (json .dumps (stats .as_dict (), indent = 4 ))
Original file line number Diff line number Diff line change
1
+ import socket
2
+ import json
3
+ import pingparsing
4
+
5
+
6
+ class PingFailedException (Exception ):
7
+ pass
8
+
9
+
10
+ def get_local_ip ():
11
+ """
12
+ Use socket to get the local IP, default to loopback 127.0.0.1
13
+ https://stackoverflow.com/a/28950776
14
+ :return:
15
+ """
16
+ s = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
17
+ s .settimeout (0 )
18
+ try :
19
+ # doesn't even have to be reachable
20
+ s .connect (('10.255.255.255' , 1 ))
21
+ IP = s .getsockname ()[0 ]
22
+ except Exception :
23
+ IP = '127.0.0.1'
24
+ finally :
25
+ s .close ()
26
+ return IP
27
+
28
+
29
+ def ping_target_ip (target_ip : str ) -> pingparsing .PingStats :
30
+ ping_parser = pingparsing .PingParsing ()
31
+ transmitter = pingparsing .PingTransmitter ()
32
+ transmitter .destination = target_ip
33
+ transmitter .count = 4
34
+ result = transmitter .ping ()
35
+ if result .returncode != 0 :
36
+ raise PingFailedException (f"Failed ping output:\n { result .stdout } " )
37
+ stats = ping_parser .parse (result )
38
+ if stats .packet_loss_rate > 0 :
39
+ stats_json = json .dumps (stats .as_dict (include_icmp_replies = True ), indent = 4 )
40
+ raise PingFailedException (f"Ping loss rate greater than 0.\n { stats_json } " )
41
+ return stats
42
+
43
+
44
+ if __name__ == "__main__" :
45
+ res = ping_target_ip ("google.com" ).as_dict ()
46
+ print (json .dumps (res , indent = 4 ))
Original file line number Diff line number Diff line change
1
+ # Get Port from Connector
2
+ This is a resource script that will find port of specified connector alias
3
+
4
+ ## Usage
Original file line number Diff line number Diff line change
1
+ cloudshell-automation-api
2
+ cloudshell-logging
3
+ pingparsing
4
+ cloudshell-sandbox-reporter
You can’t perform that action at this time.
0 commit comments