Site icon moneyslow.com

阿里云命令行CLI获取ecs实例的IP地址

阿里云使用技巧

阿里云使用技巧

利用阿里云的命令行可以用AK的方式方便的获取资源信息。
官方帮助文档:https://help.aliyun.com/document_detail/121541.html
安装和配置略过。。。。

帮助文档里所有的目录都要仔细看一下,比较有用的是这个格式化输出的文档:https://help.aliyun.com/document_detail/122111.html

总结后,记录以下比较实用的命令行:

获取所有实例的内网ip地址(这里是获取 VpcAttributes.PrivateIpAddress.IpAddress):

aliyun ecs DescribeInstances --RegionId cn-beijing --pager -o cols=InstanceId,InstanceName,VpcAttributes.PrivateIpAddress.IpAddress,Publi
cIpAddress.IpAddress,Status rows=Instances.Instance

另外一种写法(这里以获取 NetworkInterfaces.NetworkInterface[].PrimaryIpAddress 为例):

aliyun ecs DescribeInstances --output cols=InstanceId,Status,PrivateIpAddress rows="Instances.Instance[].{InstanceId:InstanceId,Status:St
atus,PrivateIpAddress:NetworkInterfaces.NetworkInterface[].PrimaryIpAddress}"

一个ecs实例的IP地址,根据其自身用途,可能有多个ip地址,具体想获取哪个ip地址,根据语法自己进行选择。
查询的语法规则 :https://jmespath.org/tutorial.html

漂亮的格式化输出结果

命令参考
https://chegva.com/4353.html
https://www.cnblogs.com/syavingcs/p/13152110.html
https://github.com/aliyun/aliyun-cli

Exit mobile version