Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import boto3
  2.  
  3. # Config boto3
  4. region = ""
  5. aws_key_id = ""
  6. aws_secret_key = ""
  7.  
  8. # Connect AWS
  9. client = boto3.client('ec2', region_name=region, aws_access_key_id=aws_key_id, aws_secret_access_key=aws_secret_key)
  10.  
  11. # Informacion de las instancias
  12. Id = []
  13. Nombre = []
  14. Tipo_instancia = []
  15. Ip = []
  16. Grupo_SG = []
  17. AMI_id = []
  18.  
  19. class Listar:
  20.  
  21. """ Obtiene una lista de todos los sevidores de AWS, ensenando
  22. su id, nombre, tipo de instancia, ip, grupo de seguridad
  23. y la id de la ami """
  24.  
  25. def __init__(self):
  26. self.get_lista()
  27. self.print_all()
  28.  
  29.  
  30. def get_lista(self):
  31. """ Obtiene la informacion y la gurada en una array """
  32. response = client.describe_instances()
  33. for reservation in response["Reservations"]:
  34. for instance in reservation["Instances"]:
  35. Id.append(instance["InstanceId"])
  36. Nombre.append(self.get_nombre(instance))
  37. Tipo_instancia.append(instance["InstanceType"])
  38. try:
  39. Ip.append(instance["PrivateIpAddress"])
  40. except:
  41. pass
  42. Grupo_SG.append(self.get_SG(instance))
  43. AMI_id.append(instance["ImageId"])
  44.  
  45. def get_nombre(self, instance):
  46. """ Obtiene el nombre de la instancia """
  47. try:
  48. for tag in instance["Tags"]:
  49. return tag["Value"]
  50. except:
  51. pass
  52.  
  53.  
  54. def get_SG(self, instance):
  55. """ Obtiene el nombre del grupo de seguridad """
  56. for SG in instance["SecurityGroups"]:
  57. return SG["GroupName"]
  58.  
  59.  
  60. def print_all(self):
  61. """ Muestra toda la informacion por pantalla """
  62. for i in range(len(Nombre)):
  63. print "La id de la instancia es: ", Id[i]
  64. print "El nombre de la instancia es: ", Nombre[i]
  65. print "El tipo de instancia es: ", Tipo_instancia[i]
  66. try:
  67. print "La ip de la instancia es: ", Ip[i]
  68. except:
  69. pass
  70. print "El grupo de seguridad es: ", Grupo_SG[i]
  71. print "La id del AMI es: ", AMI_id[i]
  72. print "=============================================="
  73.  
  74. if __name__ == "__main__":
  75. Listar()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement