awsls - List AWS containers matching an argument

  2020-11-28


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));

    <span style="color: #007020; font-weight: bold">foreach</span> (<span style="color: #bb60d5">$tasks</span><span style="color: #666666">-&gt;</span><span style="color: #4070a0">taskArns</span> <span style="color: #007020; font-weight: bold">as</span> <span style="color: #bb60d5">$task</span>)
    {
        <span style="color: #bb60d5">$taskDetail</span> <span style="color: #666666">=</span> <span style="color: #007020">json_decode</span>(<span style="color: #007020">shell_exec</span>(<span style="color: #4070a0">&quot;aws ecs describe-tasks --cluster &quot;</span> <span style="color: #666666">.</span> <span style="color: #bb60d5">$cluster</span> <span style="color: #666666">.</span> <span style="color: #4070a0">&quot; --task &quot;</span> <span style="color: #666666">.</span> <span style="color: #bb60d5">$task</span>));
        <span style="color: #007020; font-weight: bold">foreach</span> (<span style="color: #bb60d5">$taskDetail</span><span style="color: #666666">-&gt;</span><span style="color: #4070a0">tasks</span> <span style="color: #007020; font-weight: bold">as</span> <span style="color: #bb60d5">$taskDetailTask</span>)
        {
            <span style="color: #007020; font-weight: bold">foreach</span> (<span style="color: #bb60d5">$taskDetailTask</span><span style="color: #666666">-&gt;</span><span style="color: #4070a0">containers</span> <span style="color: #007020; font-weight: bold">as</span> <span style="color: #bb60d5">$container</span>)
            {
                <span style="color: #007020; font-weight: bold">foreach</span> (<span style="color: #bb60d5">$container</span><span style="color: #666666">-&gt;</span><span style="color: #4070a0">networkInterfaces</span> <span style="color: #007020; font-weight: bold">as</span> <span style="color: #bb60d5">$interface</span>)
                {
                    <span style="color: #007020; font-weight: bold">echo</span> <span style="color: #bb60d5">$interface</span><span style="color: #666666">-&gt;</span><span style="color: #4070a0">privateIpv4Address</span> <span style="color: #666666">.</span> <span style="color: #4070a0">&quot;</span><span style="color: #4070a0; font-weight: bold">\n</span><span style="color: #4070a0">&quot;</span>;
                }
            }
        }
    }
}

}