{"id":2850,"date":"2022-09-02T12:05:26","date_gmt":"2022-09-02T12:05:26","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2850"},"modified":"2024-10-19T17:13:33","modified_gmt":"2024-10-19T17:13:33","slug":"python-ec2","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/python-ec2","title":{"rendered":"Python script to check state of EC2 instances"},"content":{"rendered":"<p>In this blog, we will write a <a href=\"https:\/\/www.checkmateq.com\/python-development\">Python script<\/a> to check the <strong>state of EC2 instances in a region<\/strong> and if an instance is in a running state then get its uptime. In this script, we will use the <strong>Boto3<\/strong> and <strong>Paramiko<\/strong> modules.<\/p>\n<p>Boto3 is an AWS <a href=\"https:\/\/www.checkmateq.com\/node-js-development\">Software Development<\/a> Kit for Python using which we use various AWS services like EC2 and S3.<\/p>\n<p>To install the boto3 module use the following command:<\/p>\n<pre class=\"wp-block-preformatted\" data-slot-rendered-dynamic=\"true\"><code><strong>pip3 install boto3<\/strong><\/code><\/pre>\n<p>The Paramiko module helps us to establish SSH connections with servers. Paramiko is an implementation of the SSHv2 protocol.<\/p>\n<p>To install the paramiko module use the following command:<\/p>\n<p><code><br \/>\npip3 install paramiko<br \/>\n<\/code><\/p>\n<p>The following script can be broken down into the following steps:<\/p>\n<ol>\n<li>Connecting to <a href=\"https:\/\/www.checkmateq.com\/cloud\">AWS Cloud<\/a> using boto3 client.<\/li>\n<li>Listing instances and their state using boto3 describe_instances resource. The script will ask the user to enter the region name and will list elastic cloud compute instances in that region.<\/li>\n<li>Using the paramiko module to SSH to the instance which is running.<\/li>\n<li>Run the following command to get uptime. The uptime command in Linux is used to find out how long the system is active or running.<\/li>\n<\/ol>\n<pre>uptime -p\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2868\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-data-analytics-company-300x30.png\" alt=\"\" width=\"750\" height=\"75\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-data-analytics-company-300x30.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-data-analytics-company-1024x101.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-data-analytics-company-768x76.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-data-analytics-company.png 1044w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<pre><code>\r\nimport boto3\r\nimport paramiko\r\n\r\ntry:\r\n region=input(\"Enter region name: \\n\")\r\n print(\"\\n\")\r\n ec2_client=boto3.client('ec2',region_name=region)\r\n response=ec2_client.describe_instances()\r\n\r\n for instances in response['Reservations']:\r\n    for each in instances['Instances']:\r\n        if each['State']['Name'] == \"running\":\r\n           for x in each['NetworkInterfaces']:\r\n               i= (x['Association']['PublicIp'])\r\n               key = paramiko.RSAKey.from_private_key_file(\"\/home\/vagrant\/scripts\/dell-ohio-keypair.pem\")\r\n               print(f\"\\nInstanceId: {each['InstanceId']}, State:{each['State']['Name']}\")\r\n               client = paramiko.SSHClient()\r\n               client.set_missing_host_key_policy(paramiko.AutoAddPolicy())\r\n               client.connect(hostname= i, username='ubuntu', pkey=key)\r\n               cmd = \"uptime -p\"\r\n               stdin, stdout, stderr = client.exec_command(cmd)\r\n               stdout = stdout.readlines()\r\n               for line in stdout:\r\n                        print(\"The uptime of the instance is:\")\r\n                        print(line)\r\n               client.close()\r\n        elif each['State']['Name'] != \"running\":\r\n            print(f\"InstanceId: {each['InstanceId']}, State:{each['State']['Name']}\\n\")\r\n\r\nexcept Exception as e:\r\n        print (e)\r\n<\/code><\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2856\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-migration-services-300x156.png\" alt=\"EC2 instances\" width=\"769\" height=\"400\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-migration-services-300x156.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-migration-services-768x399.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-migration-services.png 1006w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><a href=\"https:\/\/www.checkmateq.com\/contact-us\">Please contact<\/a> us for <a href=\"https:\/\/www.checkmateq.com\/virtual-cto-services\">virtual CTO services in India<\/a> ,\u00a0<a href=\"https:\/\/www.checkmateq.com\/cloud\">cloud engineering services<\/a>, <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">IT Staff Augmentation Services<\/a> , <a href=\"https:\/\/www.checkmateq.com\/hire-developer\">Hire Developers in India<\/a> , and product development strategy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will write a Python script to check the state of EC2 instances in a region and if an instance is in a running state then get its uptime. In this script, we will use the Boto3 and Paramiko modules. Boto3 is an AWS Software Development Kit for Python using which we &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/python-ec2\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python script to check state of EC2 instances&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":869,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[2,70,69,68,23,14,6],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2850"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2850"}],"version-history":[{"count":18,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2850\/revisions"}],"predecessor-version":[{"id":4622,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2850\/revisions\/4622"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/869"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}