Yas*_*ani 1 node.js google-compute-engine google-cloud-platform
我想使用 node.js 动态获取我的 Google VM 实例的 IP 地址。是否有任何 NPM 包或 Google Cloud 是否提供任何 API?我不想手动复制粘贴它。
您可以将Node.js 客户端库用于 Google Compute Engine。
有多种方法可以检索计算实例的外部 IP 地址,例如您可以运行以下代码:
const Compute = require('@google-cloud/compute');
const compute = new Compute({
projectId: 'your-project-id',
keyFilename: '/your/path/to/file'
});
const zone = compute.zone('instanceZone');
const vm = zone.vm('instanceName');
vm.getMetadata(function(err, metadata, apiResponse) {});
//-
// If the callback is omitted, we'll return a Promise.
//-
vm.getMetadata().then(function(data) {
// Representation of this VM as the API sees it.
const metadata = data[0];
console.log(metadata.networkInterfaces[0].accessConfigs[0].natIP)
});
Run Code Online (Sandbox Code Playgroud)
此外,您还可以使用Google Cloud Platform 命令行界面。
gcloud compute instances describe instanceName --zone=instanceZone | grep natIP
| 归档时间: |
|
| 查看次数: |
700 次 |
| 最近记录: |