阿里云CLI命令

暗香疏影 创作者

阿里云CLI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#################### Basic ####################
aliyun configure list
aliyun configure --mode AK --profile "example"


#################### Check ZoneID ####################
.\aliyun.exe ecs DescribeZones --RegionId ap-southeast-1 | tee zone.txt

aliyun vpc DescribeVpcs
aliyun vpc DescribeVSwitches

#################### Check VPC VSW ####################
.\aliyun.exe --profile CLI-exampleAK1 vpc DescribeVSwitches | tee vsw.json

.\aliyun.exe vpc DescribeVpcs --output cols="VpcName,VpcId,VSwitchIds" rows="Vpcs.Vpc[]"

.\aliyun.exe --profile CLI-exampleAK1 vpc DescribeVpcs --output cols="VpcName,VpcId,VSwitchIds" rows="Vpcs.Vpc[]"

.\aliyun.exe --profile CLI-exampleAK1 vpc DescribeVSwitches --output cols="VpcId,ZoneId,VSwitchId,VSwitchName" rows="VSwitches.VSwitch[]"

#################### 查看实例 Describe Instance with SecurityGroup and show up to 100(max) ####################
aliyun ecs DescribeInstances --output cols="InstanceId,ImageId,InstanceName,HostName,CreationTime,SecurityGroupIds.SecurityGroupId" rows="Instances.Instance[]" --PageSize 100

# 查看硬盘并带实例ID
aliyun ecs DescribeDisks --output cols="InstanceId,Size,Type" rows="Disks.Disk[]" --PageSize 100

上面是把每页显示从默认的10行改为100,但是默认本身就是只显示第一页。

https://www.alibabacloud.com/help/en/cli/aggregation-of-paging-interface-results

如果希望显示全部数值需要如下:

1
aliyun ecs DescribeInstances --pager

其他:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#################### Check SecurityGroup ####################
.\aliyun.exe --profile CLI-exampleAK1 ecs DescribeSecurityGroups --output cols="VpcId,SecurityGroupId,SecurityGroupName,CreationTime" rows="SecurityGroups.SecurityGroup[]"

# Check SecurityGroup Rule
.\aliyun.exe --profile CLI-exampleAK1 ecs DescribeSecurityGroupAttribute --SecurityGroupId sg-l4v43vk6miwlg8s0wpb0

#################### Add SecurityGroup ####################
aliyun ecs CreateSecurityGroup --RegionId ap-southeast-1 --SecurityGroupName SG-exampleName --Description "SG-Desc" --VpcId <vpcid>
加上--VpcId是因为不指定vpc则会新建vpc

# Add SG rule:
aliyun ecs AuthorizeSecurityGroup --RegionId ap-southeast-1 --SecurityGroupId sg-l4v46crimxpq60awehuo
--IpProtocol tcp --PortRange 3389/3389 --SourceCidrIp 30.26.0.1/16 --Policy Accept --Description "Allow RDP"

# Replace SG group of the instance
aliyun ecs ModifyInstanceAttribute --RegionId ap-southeast-1 --InstanceId <your_instance_id> --SecurityGroupIds.1 <your sg id>
# 注意:可参考api.alibabacloud.com --SecurityGroupIds需要加.1 替换安全组。


#################### Check Image ####################
.\aliyun.exe --profile CLI-exampleAK1 ecs DescribeImages --RegionId ap-southeast-1 --ImageOwnerAlias self
aliyun --profile CLI-exampleAK1 ecs DescribeImages --RegionId ap-southeast-1 --ImageOwnerAlias self --output cols="ImageId,ImageName,Size,Status" rows="Images.Image[]"

aliyun.exe --profile CLI-exampleAK1 ecs DescribeImages --RegionId ap-southeast-1 --ImageOwnerAlias system
aliyun --profile CLI-exampleAK1 ecs DescribeImages --RegionId ap-southeast-1 --output cols="ImageId,OSNameEn,Status,BootMode,ImageOwnerAlias" rows="Images.Image[]"

#################### Create Image ####################
# Get Instance ID
aliyun --profile CLI-exampleAK1 ecs DescribeInstances --RegionId ap-southeast-1
# Get Disk ID
aliyun ecs DescribeDisks --InstanceId <YourInstanceId>

aliyun --profile CLI-exampleAK1 ecs CreateSnapshot --DiskId "d-yourDiskid" --SnapshotName "CopilotOS2"

aliyun --profile CLI-exampleAK1 ecs CreateImage --SnapshotId s-snapshotid --ImageName "yourImageName" --Architecture x86_64



#################### Secondary ENI ####################
# Create ENI
aliyun eci CreateNetworkInterface --vswitch-id vsw-t4nb3qpn2szg7e7uwj9as

#Attach to ECS
aliyun ecs AttachNetworkInterface --InstanceId i-t4n3jslpkf1d012jp0i5 --NetworkInterfaceId eni-t4ngcli5eva9fvooyxpd

#################### RAM User ####################
# Check Users
aliyun ram ListUsers

# Check Users AK
aliyun ram ListAccessKeys --UserName test

# Last Used
aliyun ram GetAccessKeyLastUsed --UserName test --UserAccessKeyId <accessKeyId>

# Delete AccessKey
aliyun ram DeleteAccessKey --UserName test --UserAccessKeyId <accessKeyId>

# Check Permession
aliyun ram ListPoliciesForUser --UserName test

# Add Permession
aliyun ram AttachPolicyToUser --PolicyType System --PolicyName AdministratorAccess --UserName test

# List availabe Permession (Policy)
aliyun ram ListPolicies

# List User Permession
aliyun ram ListPoliciesForUser --UserName test

CLI结果转换为Excel

以查询实例为例:

aliyun ecs DescribeInstances –output cols=”InstanceId,ImageId,InstanceName,HostName,CreationTime,SecurityGroupIds.SecurityGroupId” rows=”Instances.Instance[]” –PageSize 100

得到结果如下:

1
2
3
4
5
InstanceId             | InstanceName                                        | HostName                | CreationTime      | SecurityGroupIds.SecurityGroupId
---------- | ------------ | -------- | ------------ | --------------------------------
i-t4n5578u1oir6zn0oa6e | test02 | exampleHN1 | 2024-04-23T09:40Z | [sg-txxx]
i-t4n43xvoens25esjb93q | launch-advisor-20240423 | exampleHN2 | 2024-04-23T08:24Z | [sg-txxx]

直接将结果复制到Excel后,使用分列,分列掉”|”, 删除第二行。然后使用Ctrl+F 替换空行“ ”为“”即可。替换空行是因为默认粘贴到Excel,开头和结尾都有很多空行。

  • 标题: 阿里云CLI命令
  • 作者: 暗香疏影
  • 创建于 : 2024-04-07 00:00:00
  • 更新于 : 2024-04-24 00:00:00
  • 链接: https://blog.23ikr.com/2024/04/07/Wiki-Guide/2024-04-07-alibabacloud-cli/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
目录
阿里云CLI命令