├── .gitignore ├── LICENSE ├── README.md ├── awls └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Dan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # awls 2 | Simple tool to lookup EC2 instance information in your shell 3 | 4 | ## Usage 5 | 6 | ``` 7 | awls -h 8 | usage: awls [-h] [--profile PROFILE] [--region REGION] [--maintenance_tags] 9 | [N] 10 | 11 | Interrogate AWS EC2 and return pretty data 12 | 13 | positional arguments: 14 | N Search query 15 | 16 | optional arguments: 17 | -h, --help show this help message and exit 18 | --profile PROFILE, -p PROFILE 19 | Which AWS profile to use 20 | --region REGION, -r REGION 21 | Which AWS region to use 22 | --maintenance_tags, -m 23 | Output maintenance tags for instances 24 | ``` 25 | 26 | ## Example Usage 27 | 28 | ``` 29 | # Search for a specific query 30 | ./awls search_term -p AWS_PROFILE -r eu-central-1 31 | 32 | # Output 33 | +-----------------------------------------------------------+--------------+--------------+ 34 | | fqdn | private_ip | public_ip | 35 | |-----------------------------------------------------------+--------------+--------------| 36 | | test.example.com | 10.2.1.5 | 8.8.8.8 | 37 | +-----------------------------------------------------------+--------------+--------------+ 38 | ``` 39 | 40 | ## Setting default profile 41 | If you wish to set a default AWS profile you can edit line 57 `default=""` 42 | -------------------------------------------------------------------------------- /awls: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Maintainer: Dan Pilch 3 | 4 | import boto3 5 | import boto3.session 6 | from tabulate import tabulate 7 | import argparse 8 | 9 | 10 | class AWSInventoryTable(object): 11 | def __init__(self, aws_profile, aws_region): 12 | self.session = boto3.session.Session(profile_name=aws_profile, region_name=aws_region) 13 | 14 | def generate_table(self, query, maintenance_tags): 15 | ec2 = self.session.resource('ec2') 16 | instances = ec2.instances.filter() 17 | tbl_list = [] 18 | 19 | for instance in instances: 20 | # Using a flag so we don't add data to the list we don't actually want 21 | found = False 22 | if instance.tags is not None: 23 | for tag in instance.tags: 24 | if tag['Key'] == "Name": 25 | # if a query was passed, try to find instances that match 26 | if query: 27 | if query in tag['Value']: 28 | aws_name = tag['Value'] 29 | aws_private_ip = instance.private_ip_address 30 | aws_public_ip = instance.public_ip_address 31 | found = True 32 | else: 33 | found = False 34 | else: 35 | aws_name = tag['Value'] 36 | aws_private_ip = instance.private_ip_address 37 | aws_public_ip = instance.public_ip_address 38 | found = True 39 | 40 | if tag['Key'] == "Maintenance": 41 | aws_maintenance_tags = tag['Value'] 42 | 43 | if found: 44 | if maintenance_tags: 45 | tbl_list.append([aws_name, aws_private_ip, aws_public_ip, aws_maintenance_tags]) 46 | headers = ["fqdn", "private_ip", "public_ip", "maintenance_tags"] 47 | else: 48 | tbl_list.append([aws_name, aws_private_ip, aws_public_ip]) 49 | headers = ["fqdn", "private_ip", "public_ip"] 50 | 51 | # Return pretty data 52 | print(tabulate(tbl_list, headers=headers, tablefmt="psql")) 53 | 54 | 55 | def main(): 56 | parser = argparse.ArgumentParser(description="Interrogate AWS EC2 and return pretty data") 57 | parser.add_argument("--profile", "-p", help="Which AWS profile to use", default="") 58 | parser.add_argument("--region", "-r", help="Which AWS region to use", default="ap-southeast-2") 59 | parser.add_argument("--maintenance_tags", "-m", help="Output maintenance tags for instances", action="store_true", default=False) 60 | parser.add_argument("query", metavar="N", nargs='?', help="Search query", default=None) 61 | args = parser.parse_args() 62 | 63 | run = AWSInventoryTable(args.profile, args.region) 64 | run.generate_table(args.query, args.maintenance_tags) 65 | 66 | 67 | if __name__ == "__main__": 68 | main() 69 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | tabulate 2 | boto3 3 | --------------------------------------------------------------------------------