Best Python code snippet using localstack_python
post check.py
Source:post check.py
...30 )31 32 command_id = response['Command']['CommandId']33 time.sleep(2)34 output = ssm_client.get_command_invocation(35 CommandId=command_id,36 InstanceId=instance.id37 )38 data[i]["Kernel"] = output['StandardOutputContent']39 40 response = ssm_client.send_command(41 InstanceIds=[instance.id],42 DocumentName="AWS-RunShellScript",43 Parameters={'commands': ["uptime"]},44 )45 46 command_id = response['Command']['CommandId']47 time.sleep(2)48 output = ssm_client.get_command_invocation(49 CommandId=command_id,50 InstanceId=instance.id51 )52 data[i]["Uptime"] = output['StandardOutputContent']53 54 response = ssm_client.send_command(55 InstanceIds=[instance.id],56 DocumentName="AWS-RunShellScript",57 Parameters={'commands': ["date"]},58 )59 60 command_id = response['Command']['CommandId']61 time.sleep(2)62 output = ssm_client.get_command_invocation(63 CommandId=command_id,64 InstanceId=instance.id65 )66 data[i]["Date"] = output['StandardOutputContent']67 68 response = ssm_client.send_command(69 InstanceIds=[instance.id],70 DocumentName="AWS-RunShellScript",71 Parameters={'commands': ["systemctl status amazon-ssm-agent"]},72 )73 74 command_id = response['Command']['CommandId']75 time.sleep(2)76 output = ssm_client.get_command_invocation(77 CommandId=command_id,78 InstanceId=instance.id79 )80 if "active (running)" in output['StandardOutputContent']:81 data[i]["Amazon SSM Agent"] = "Yes"82 elif output['StandardOutputContent'] == "":83 data[i]["Amazon SSM Agent"] = ""84 else:85 response = ssm_client.send_command(86 InstanceIds=[instance.id],87 DocumentName="AWS-RunShellScript",88 Parameters={'commands': ["systemctl restart amazon-ssm-agent"]},89 )90 91 command_id = response['Command']['CommandId']92 time.sleep(2)93 response = ssm_client.send_command(94 InstanceIds=[instance.id],95 DocumentName="AWS-RunShellScript",96 Parameters={'commands': ["systemctl status amazon-ssm-agent"]},97 )98 99 command_id = response['Command']['CommandId']100 time.sleep(2)101 output = ssm_client.get_command_invocation(102 CommandId=command_id,103 InstanceId=instance.id104 )105 106 if "active (running)" in output['StandardOutputContent']:107 data[i]["Amazon SSM Agent"] = "Yes"108 else:109 data[i]["Amazon SSM Agent"] = "No"110 111 response = ssm_client.send_command(112 InstanceIds=[instance.id],113 DocumentName="AWS-RunShellScript",114 Parameters={'commands': ["/opt/splunkforwarder/bin/splunk status"]},115 )116 117 command_id = response['Command']['CommandId']118 time.sleep(2)119 output = ssm_client.get_command_invocation(120 CommandId=command_id,121 InstanceId=instance.id122 )123 if "running" in output['StandardOutputContent']:124 data[i]["Splunk"] = "Yes"125 elif output['StandardOutputContent'] == "":126 data[i]["Splunk"] = ""127 else:128 response = ssm_client.send_command(129 InstanceIds=[instance.id],130 DocumentName="AWS-RunShellScript",131 Parameters={'commands': ["/opt/splunkforwarder/bin/splunk restart"]},132 )133 134 command_id = response['Command']['CommandId']135 time.sleep(2)136 response = ssm_client.send_command(137 InstanceIds=[instance.id],138 DocumentName="AWS-RunShellScript",139 Parameters={'commands': ["/opt/splunkforwarder/bin/splunk status"]},140 )141 142 command_id = response['Command']['CommandId']143 time.sleep(2)144 output = ssm_client.get_command_invocation(145 CommandId=command_id,146 InstanceId=instance.id147 )148 149 if "running" in output['StandardOutputContent']:150 data[i]["Splunk"] = "Yes"151 else:152 data[i]["Splunk"] = "No"153 154 response = ssm_client.send_command(155 InstanceIds=[instance.id],156 DocumentName="AWS-RunShellScript",157 Parameters={'commands': ["rm /usr/bin/scp"]},158 )159 160 command_id = response['Command']['CommandId']161 time.sleep(2)162 output = ssm_client.get_command_invocation(163 CommandId=command_id,164 InstanceId=instance.id165 )166 if data[i]["Kernel"] == "":167 data[i]["SCP"] = ""168 elif output['StandardOutputContent'] == "":169 data[i]["SCP"] = "Removed"170 else:171 data[i]["SCP"] = "Not Found"172 except:173 print("Error checking for " + data[i]["Instance Name"] + " (" + instance.id + ")")174 175 i+=1176 ...
lambda-functions.py
Source:lambda-functions.py
...16 Comment=(event["User"]+": "+event["Id"] +","+ event["Port"]))17 18 commandId = response["Command"]['CommandId']19 time.sleep(2)20 output = ssm_client.get_command_invocation( 21 CommandId = commandId,22 InstanceId = ec2Id23 )24 25 return output['Status']26def remove_container(event, context):27 28 ec2Id = 'i-0496bbb980eb17cae' 29 30 ssm_client = boto3.client('ssm')31 response = ssm_client.send_command(32 InstanceIds=[ec2Id],33 DocumentName='AWS-RunShellScript',34 Parameters={35 'commands':[36 'docker kill {}'.format(event['Id']),37 'docker rm {}'.format(event['Id'])38 ],39 },40 Comment=("removing: "+event["Id"]))41 42 commandId = response["Command"]['CommandId']43 time.sleep(2)44 output = ssm_client.get_command_invocation( 45 CommandId = commandId,46 InstanceId = ec2Id47 )48 49 return output['Status']50def create_network(event, context):51 52 ec2Id = 'i-0496bbb980eb17cae' 53 54 ssm_client = boto3.client('ssm')55 response = ssm_client.send_command(56 InstanceIds=[ec2Id],57 DocumentName='AWS-RunShellScript',58 Parameters={59 'commands':[60 'docker network create -d bridge --subnet={0}.0/24 --gateway={0}.225 {1}'.format(event['Netmask'], event['Network'])61 ],62 },63 Comment=("creating: "+event["Network"]+","+event["Netmask"]))64 65 commandId = response["Command"]['CommandId']66 time.sleep(5)67 output = ssm_client.get_command_invocation( 68 CommandId = commandId,69 InstanceId = ec2Id70 )71 72 return output['Status']73def remove_network(event, context):74 75 ec2Id = 'i-0496bbb980eb17cae' 76 77 ssm_client = boto3.client('ssm')78 response = ssm_client.send_command(79 InstanceIds=[ec2Id],80 DocumentName='AWS-RunShellScript',81 Parameters={82 'commands':[83 'docker network rm {}'.format(event['Network'])84 ],85 },86 Comment=("removing: "+event["Network"]))87 88 commandId = response["Command"]['CommandId']89 time.sleep(3)90 output = ssm_client.get_command_invocation( 91 CommandId = commandId,92 InstanceId = ec2Id93 )94 95 return output['Status']96def add_container_to_network(event, context):97 98 ec2Id = 'i-0496bbb980eb17cae' 99 100 ssm_client = boto3.client('ssm')101 response = ssm_client.send_command(102 InstanceIds=[ec2Id],103 DocumentName='AWS-RunShellScript',104 Parameters={105 'commands':[106 'docker network connect {1} {0}'.format(event['Container'], event['Network'])107 ],108 },109 Comment=("Adding "+event["Container"] +" to "+ event["Network"]))110 111 commandId = response["Command"]['CommandId']112 time.sleep(2)113 output = ssm_client.get_command_invocation( 114 CommandId = commandId,115 InstanceId = ec2Id116 )117 118 return output['Status']119def remove_container_from_network(event, context):120 121 ec2Id = 'i-0496bbb980eb17cae' 122 123 ssm_client = boto3.client('ssm')124 response = ssm_client.send_command(125 InstanceIds=[ec2Id],126 DocumentName='AWS-RunShellScript',127 Parameters={128 'commands':[129 'docker network disconnect {1} {0}'.format(event['Container'], event['Network'])130 ],131 },132 Comment=("Removing "+event["Container"] +" to "+ event["Network"]))133 134 commandId = response["Command"]['CommandId']135 time.sleep(2)136 output = ssm_client.get_command_invocation( 137 CommandId = commandId,138 InstanceId = ec2Id139 )140 ...
aws_ssm.py
Source:aws_ssm.py
...16 def session_output(self, cmd_id, inst_id):17 """Checks the status of a command on an instance18 """19 try:20 result = self._ssm_client.get_command_invocation(21 CommandId=cmd_id,22 InstanceId=inst_id,23 )24 return result25 except self._ssm_client.exceptions.InvocationDoesNotExist:26 return None27 def cmd_stdout(self, cmd_id, inst_id):28 """Checks the status of a command on an instance29 """30 try:31 result = self._ssm_client.get_command_invocation(32 CommandId=cmd_id,33 InstanceId=inst_id,34 )35 return result['StandardOutputContent']36 except self._ssm_client.exceptions.InvocationDoesNotExist:37 return None38 def cmd_status(self, cmd_id, inst_id):39 """Checks the status of a command on an instance40 """41 try:42 result = self._ssm_client.get_command_invocation(43 CommandId=cmd_id,44 InstanceId=inst_id45 )46 return result['Status']47 except self._ssm_client.exceptions.InvocationDoesNotExist:48 return "Failed"49 def run_cmd_on_inst(self, cmd_list, inst_id):50 """ commands = [51 'echo "hello world" > /home/ec2-user/hello.txt', # demo comma is important!52 f'cd {repo_path}',53 'sudo git pull'54 # do stuff55 ]56 """...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!