【问题标题】:Given an IP address, how do I find which GCP Compute Engine instance it belongs too?给定一个 IP 地址,我如何找到它也属于哪个 GCP Compute Engine 实例?
【发布时间】:2018-11-27 23:19:58
【问题描述】:

我收到如下安全警告:

Security vulnerability found in server running at 123.45.67.89.

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

【问题讨论】:

    标签: powershell google-cloud-platform google-compute-engine gcloud


    【解决方案1】:

    使用带有过滤器的 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;
    

    【讨论】:

    • 这是一个很好的命令,但只会列出当前项目gcloud config list project中的实例。
    • 我阅读了cloud.google.com/sdk/gcloud/reference/topic/filters,但我找不到可以在过滤器中使用的主题列表。这样的列表存在吗?
    • 已更新以使用项目名称的 bash 数组。我不确定过滤器的完整列表,这可能取决于资源。基本上,使用gcloud compute instances list --format=json 时显示在顶层的任何内容都是可过滤的。
    • 我在运行gcloud compute instances list --format=json 时没有看到列出的EXTERNAL_IP(小写或大写)。
    • 我认为有一些捷径,你也可以使用任何向下钻取的属性。 --filter="networkInterfaces.accessConfigs.natIP=123.45.67.89"资源列表:cloud.google.com/sdk/gcloud/reference/topic/resource-keys
    【解决方案2】:

    这个 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
    

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

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

    【讨论】:

    • 不错的脚本。这将进入我的工具箱。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多