我收到了一个安全警告,比如:
Security vulnerability found in server running at 123.45.67.89.
我有很多谷歌云平台项目,每个项目中都有很多实例在运行。如何查找此ip地址属于哪个计算引擎实例?
使用带有过滤器的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;
所以。。。如果有其他人需要这样做,然后像我一样来到这里。你可以使用谷歌云界面顶部的搜索栏来搜索IP。
此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它更复杂,因为它只检查我拥有的项目,并显示进度条。
第页。S.Powershell也在Linux和Mac上运行!我在Linux上写了这段代码。