给定 IP 地址,如何找到它所属的 GCP 计算引擎实例?

Jef*_*nie 6 powershell google-compute-engine google-cloud-platform gcloud

我收到如下安全警告:

Security vulnerability found in server running at 123.45.67.89.
Run Code Online (Sandbox Code Playgroud)

我有很多 Google Cloud Platform 项目,每个项目中都运行着很多实例。如何找到该 IP 地址属于哪个 Compute Engine 实例?

Fri*_*ush 5

将 gcloud 命令行工具与过滤器结合使用。

gcloud compute instances list --filter="EXTERNAL_IP=123.45.67.89"

编辑:错过了许多项目要求。使用bash:

project_names=( "project1" "project2" "project3" )
for i in ${project_names[@]}; do gcloud compute instances list --filter="EXTERNAL_IP=123.45.67.89" --project=$i; done;
Run Code Online (Sandbox Code Playgroud)


小智 5

所以...如果其他人需要这样做并最终来到这里,就像我一样。您可以直接使用谷歌云界面顶部的搜索栏来搜索IP。


Jef*_*nie 4

这个 PowerShell 脚本将完成这项工作。它使用gcloud

<#
.SYNOPSIS
    Given an IP address, finds a GCP Compute instance with the ip address.
.EXAMPLE
    PS C:\> .\Get-GcpInstance.ps1 --IpAddress 1.2.3.4
.OUTPUTS
    The GCP instance information.
#>
Param(
    [string][Parameter(Mandatory=$true)] $IpAddress
)

function Get-GcpInstance {
    param (
        [string][Parameter(Mandatory=$true)] $IpAddress,
        [string[]][Parameter(Mandatory=$true)] $ProjectIds
    )
    foreach ($projectId in $projectIds) {
        $instances = gcloud compute instances list -q --project=$projectId --format=json | ConvertFrom-Json
        foreach ($instance in $instances) {
            foreach ($networkInterface in $instance.networkInterfaces) {
                if ($networkInterface.networkIp -eq $IpAddress) {
                    return $instance                    
                }
                foreach ($accessConfig in $networkInterface.accessConfigs) {
                    if ($accessConfig.natIP -eq $IpAddress) {
                        return $instance
                    }
                }
            }
        }
    }
}

Get-GcpInstance $IpAddress (gcloud projects list --format=json | ConvertFrom-Json).ProjectId
Run Code Online (Sandbox Code Playgroud)

我在这里发布了一个稍微复杂的脚本版本: https: //github.com/SurferJeffAtGoogle/scratch/blob/master/FindIp/Get-GcpInstance.ps1 它更复杂,因为它只检查我拥有的项目,并且它显示进度条。

PS Powershell 也可以在LinuxMac上运行!我在 Linux 上编写了这段代码。