83 lines
4.0 KiB
Java
83 lines
4.0 KiB
Java
// This file is auto-generated, don't edit it. Thanks.
|
||
package com.mobai.events;
|
||
|
||
|
||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||
import com.aliyun.tea.TeaException;
|
||
import lombok.extern.log4j.Log4j2;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
@Log4j2
|
||
public class SelectInstences {
|
||
|
||
/**
|
||
* 使用AK&SK初始化账号Client
|
||
*
|
||
* @return Client
|
||
* @throws Exception
|
||
*/
|
||
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||
.setAccessKeyId("LTAI5t7vsLXtqTJKve7JipnX")
|
||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||
.setAccessKeySecret("AIyMaSTKQtsB1V5sVqlNAGbQzxgXnS");
|
||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||
config.endpoint = "ecs.cn-zhangjiakou.aliyuncs.com";
|
||
return new com.aliyun.ecs20140526.Client(config);
|
||
}
|
||
|
||
public static void main(String[] args_) throws Exception {
|
||
List<String> ips = new ArrayList<>();
|
||
List<String> args = java.util.Arrays.asList(args_);
|
||
com.aliyun.ecs20140526.Client client = SelectInstences.createClient();
|
||
com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest()
|
||
// .setImageId("m-8vb8qnidv34yj3nbirhc")
|
||
.setRegionId("cn-zhangjiakou");
|
||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||
try {
|
||
// 复制代码运行请自行打印 API 的返回值
|
||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = describeInstancesResponse.getBody().getInstances();
|
||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instance =
|
||
instances.getInstance();
|
||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance inst : instance) {
|
||
List<String> ipAddress = inst
|
||
.getPublicIpAddress()
|
||
.getIpAddress();
|
||
if (ipAddress.isEmpty()){
|
||
continue;
|
||
}else {
|
||
ipAddress.forEach(ip -> ips.add(ip));
|
||
}
|
||
}
|
||
log.info("当前实例ip为{}", ips);
|
||
} catch (TeaException error) {
|
||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||
// 错误 message
|
||
System.out.println(error.getMessage());
|
||
// 诊断地址
|
||
System.out.println(error.getData().get("Recommend"));
|
||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||
} catch (Exception _error) {
|
||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||
// 错误 message
|
||
System.out.println(error.getMessage());
|
||
// 诊断地址
|
||
if (error.getData() == null) {
|
||
System.out.println("error.getData()为空");
|
||
} else {
|
||
System.out.println(error.getData().get("Recommend"));
|
||
}
|
||
|
||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||
}
|
||
}
|
||
}
|