我想使用node动态获取我的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)
});
另外,您可以使用Google Cloud Platform命令行界面。
gcloud compute instances describe instanceName --zone=instanceZone | grep natIP
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句