<s> import argparse <EOL> import boto3 <EOL> from botocore . exceptions import ProfileNotFound <EOL> import sys <EOL> import random <EOL> import os <EOL> def get_filters ( args ) : <EOL> """<STR_LIT>""" <EOL> filters = [ <EOL> { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> ] <EOL> if args . tag : <EOL> for t in args . tag : <EOL> k , v = t . split ( '<STR_LIT:=>' ) <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' . format ( k ) , <EOL> '<STR_LIT>' : [ v ] <EOL> } ) <EOL> if args . has_tag_key : <EOL> for k in args . has_tag_key : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ k ] <EOL> } ) <EOL> if args . has_tag_value : <EOL> for v in args . has_tag_value : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ v ] <EOL> } ) <EOL> if args . availability_zone : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ args . availability_zone ] <EOL> } ) <EOL> if args . image_id : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ args . image_id ] <EOL> } ) <EOL> if args . instance_id : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ args . instance_id ] <EOL> } ) <EOL> if args . instance_type : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ args . instance_type ] <EOL> } ) <EOL> if args . security_group : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ args . security_group ] <EOL> } ) <EOL> if args . key_name : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ args . key_name ] <EOL> } ) <EOL> if args . subnet_id : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ args . subnet_id ] <EOL> } ) <EOL> if args . vpc_id : <EOL> filters . append ( { <EOL> '<STR_LIT:Name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ args . vpc_id ] <EOL> } ) <EOL> return filters <EOL> def parse_args ( ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT>' , const = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = argparse . REMAINDER , help = '<STR_LIT>' ) <EOL> return parser . parse_args ( ) <EOL> def main ( ) : <EOL> args = parse_args ( ) <EOL> try : <EOL> boto3 . setup_default_session ( profile_name = args . profile , region_name = args . region ) <EOL> conn = boto3 . client ( '<STR_LIT>' ) <EOL> except ProfileNotFound as e : <EOL> print ( e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> reservations = conn . describe_instances ( Filters = get_filters ( args ) ) <EOL> if len ( reservations [ '<STR_LIT>' ] ) == <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> instance_dns_names = [ [ <EOL> instance [ '<STR_LIT>' ] for instance in reservation [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> for reservation <EOL> in reservations [ '<STR_LIT>' ] ] <EOL> if args . all_matching_instances : <EOL> pass <EOL> else : <EOL> instance_dns_names = [ instance_dns_names [ random . randrange ( <NUM_LIT:0> , len ( instance_dns_names ) ) ] ] <EOL> if args . command : <EOL> remote_command = '<STR_LIT:U+0020>' . join ( args . command ) <EOL> else : <EOL> remote_command = '<STR_LIT>' <EOL> for dns_name in instance_dns_names : <EOL> if args . ssh_user : <EOL> dns_name = '<STR_LIT>' % ( args . ssh_user , dns_name ) <EOL> ssh_cmd = '<STR_LIT>' % ( args . ssh_args , dns_name , remote_command ) <EOL> os . system ( ssh_cmd ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
