orenma

GetAllInstances

Jan 31st, 2026
3,448
0
Never
12
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | Cybersecurity | 0 0
  1. #!/bin/bash
  2.  
  3. # List of all AWS regions (as of 2024)
  4. # Since we don't have ec2:DescribeRegions permission, we'll use a hardcoded list
  5. regions=(
  6. "us-east-1"
  7. "us-east-2"
  8. "us-west-1"
  9. "us-west-2"
  10. "af-south-1"
  11. "ap-east-1"
  12. "ap-south-1"
  13. "ap-south-2"
  14. "ap-northeast-1"
  15. "ap-northeast-2"
  16. "ap-northeast-3"
  17. "ap-southeast-1"
  18. "ap-southeast-2"
  19. "ap-southeast-3"
  20. "ap-southeast-4"
  21. "ca-central-1"
  22. "ca-west-1"
  23. "eu-central-1"
  24. "eu-central-2"
  25. "eu-west-1"
  26. "eu-west-2"
  27. "eu-west-3"
  28. "eu-south-1"
  29. "eu-south-2"
  30. "eu-north-1"
  31. "il-central-1"
  32. "me-south-1"
  33. "me-central-1"
  34. "sa-east-1"
  35. )
  36.  
  37. echo "Checking EC2 instances across all regions..."
  38. echo "Current AWS Identity:"
  39. aws sts get-caller-identity
  40. echo ""
  41.  
  42. # Counter for total instances
  43. total_instances=0
  44.  
  45. # Loop through each region
  46. for region in "${regions[@]}"; do
  47. echo "Checking region: $region"
  48.  
  49. # Try to describe instances in the region
  50. result=$(aws ec2 describe-instances \
  51. --region "$region" \
  52. --query "Reservations[*].Instances[*].[InstanceId, Tags[?Key=='Name'].Value|[0], State.Name, InstanceType, PrivateIpAddress, PublicIpAddress]" \
  53. --output json 2>&1)
  54.  
  55. # Check if the command was successful
  56. if [[ $? -eq 0 ]]; then
  57. # Count instances in this region
  58. instance_count=$(echo "$result" | jq 'flatten | length' 2>/dev/null || echo "0")
  59.  
  60. if [[ $instance_count -gt 0 ]]; then
  61. echo "Found $instance_count instance(s) in $region:"
  62. aws ec2 describe-instances \
  63. --region "$region" \
  64. --query "Reservations[*].Instances[*].[InstanceId, Tags[?Key=='Name'].Value|[0], State.Name, InstanceType, PrivateIpAddress, PublicIpAddress]" \
  65. --output table
  66. total_instances=$((total_instances + instance_count))
  67. else
  68. echo "No instances found in $region"
  69. fi
  70. else
  71. # Check if it's a permission error or region doesn't exist/not enabled
  72. if echo "$result" | grep -q "UnauthorizedOperation"; then
  73. echo "ERROR: No permission to describe instances in $region"
  74. elif echo "$result" | grep -q "AuthFailure"; then
  75. echo "ERROR: Authentication failure for $region"
  76. elif echo "$result" | grep -q "not subscribed\|not enabled\|not found"; then
  77. echo "SKIP: Region $region not enabled for this account"
  78. else
  79. echo "ERROR in $region: $result"
  80. fi
  81. fi
  82. echo "---"
  83. done
  84.  
  85. echo ""
  86. echo "Total instances found across all regions: $total_instances"
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment