Here’s a PHP script to get IP addresses of container instances matching a string

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#!/usr/bin/php

$additional = "";
$clusters = json_decode(shell_exec("aws ecs list-clusters" . $additional));

foreach ($clusters->clusterArns as $cluster)
{
if ((!empty($argv[1]) && stripos($cluster, $argv[1]) !== false) || empty($argv[1]))
{
$tasks = json_decode(shell_exec("aws ecs list-tasks --cluster " . $cluster));

        foreach ($tasks->taskArns as $task)
        {
            $taskDetail = json_decode(shell_exec("aws ecs describe-tasks --cluster " . $cluster . " --task " . $task));
            foreach ($taskDetail->tasks as $taskDetailTask)
            {
                foreach ($taskDetailTask->containers as $container)
                {
                    foreach ($container->networkInterfaces as $interface)
                    {
                        echo $interface->privateIpv4Address . "\n";
                    }
                }
            }
        }
    }

}