阿里云 腾讯云 CLI批量导资源信息

阿里云CLI

官网:https://help.aliyun.com/zh/cli/configure-credentials/?spm=a2c4g.11186623.0.0.262d606ftnxL6r

aliyun configure set   --profile akProfile   --mode AK   --region aliyun cn-shanghai   --access-key-id 你的AK ID   --access-key-secret 你的AK密钥
aliyun auto-completion   #命令自动补全

配置文件profile默认 位置,多个profile 配置都在config.json里,可通过命名区分

/root/.aliyun/config.json

可通过以下命令查看所有profile

 aliyun configure list

指定身份凭证配置

aliyun ecs DescribeInstances --profile exampleProfile

导出RAM用户对应权限

aliyun ram ListUsers | grep "UserName"  |awk -F "\"" '{print $4}' > /tmp/user.txt

cat>>/tmp/user.sh<<EOF
/bin/bash
echo $1
aliyun ram ListPoliciesForUser --UserName=$1  | grep -E 'Atta|Des' 
EOF

chmod +x /tmp/user.sh
awk '{print "sh user.sh " $1}' user.txt  | bash 

查询单台实例信息

aliyun ecs DescribeInstances --RegionId cn-shanghai --InstanceIds '["i-uf684izq4oceki7p****"]'

导出实例名称和对应安全组ID

aliyun ecs DescribeInstances --RegionId cn-beijing --pager | grep -E  '"InstanceName"|sg' --profile xiaoshou.profile   >hostname.txt  
#--pager 是导出所有信息,默认导出10条
#查询InstanceName和sg 安全组信息
#取得的值放入hostname.txt中
#对值进行处理

对hostname文件进行处理
在这里插入图片描述

cat hostname.txt | grep "sg-" | awk -F "\"" '{print $2}' > sg.txt  
#当前sg.txt文件全部为安全组名称
#对hostname文件进行处理,ECS名称与安全组ID进行对应
cat hostname.txt |  sed s/[[:space:]]//g | awk 'BEGIN{RS="\n"; ORS=","}{print}END{printf("\n")}'  |sed 's/"InstanceName":/\n/g'> hostname1.txt 
vim  sg.sh  #编写批量执行脚本sg.sh
#!/bin/bash
cat hostname1.txt  |grep $1
echo SecurityGroupId:$1
aliyun ecs DescribeSecurityGroupAttribute --RegionId cn-shanghai  --SecurityGroupId $1 --Direction ingress --output cols=SourceCidrIp,NicType,PortRange,Direction,IpProtocol,Policy rows=Permissions.Permission[]  --profile xiaoshou.profile  
#多个profile要添加profile名称
chmod +x sg.sh
awk '{print "sh sg.sh " $1}'  sg.txt  | bash >port.txt

获得安全组ID对应的所有端口 port.txt,最终效果如下:
ECS名称和对应安全组ID,以及安全组内规则
在这里插入图片描述

腾讯云CLI

腾讯云CLI介绍:https://cloud.tencent.com/document/product/440/6186

导出RAM用户对应权限

腾讯云:云API 调用出用户
https://console.cloud.tencent.com/api/explorer?Product=cam&Version=2019-01-16&Action=GetUser
在这里插入图片描述

cat user.txt | grep -E 'NickName' |awk -F "\"" '{print $4}'    #导出所有用户名至excel
cat user.txt | grep -E 'Uin'|awk -F ":" '{print $2}'       #导出用户的UIN至excel

合成新的excel文件复制到 id.txt

qcloudcli cam ListAttachedUserPolicies --uin 100022191042   # 测试查看单用户权限
cat>>/tmp/user.sh<<EOF
/bin/bash
#导出用户名
echo $1   
#导出权限
qcloudcli cam ListAttachedUserPolicies --uin $2 --output json  
EOF
chmod +x /tmp/user.sh
awk '{print "sh user.sh " $1,$2}' id.txt  | bash >> cam.txt

导出安全组信息

#查询安全组ID
qcloudcli  dfw  DescribeSecurityGroups  --output json  >>sg.txt   #导出安全组名和ID
cat sg.txt | grep sgName |awk -F "\"" '{print $4}'
cat sg.txt | grep sgId |awk -F "\"" '{print $4}'
#复制到excel 拷贝到sg1.txt
 
#查询安全组规则 ,sg-pc7vv7bb 为安全组ID
qcloudcli  dfw  DescribeSecurityGroupPolicy --sgId sg-pc7vv7bb --output json

cat>>sg.sh<<EOF
#!/bin/bash
#打印安全组名称
echo  $1
#导出安全组策略
qcloudcli  dfw  DescribeSecurityGroupPolicy --sgId $2 --output json
EOF
 
chmod +x sg.sh
awk '{print "sh sg.sh " $1,$2}' sg1.txt  | bash >> sg2.txt

结果过滤

官方文档: https://cloud.tencent.com/document/product/440/34015

[root@VM_180_248_centos ~]# tccli cvm DescribeZones
{
  "TotalCount": 4,
  "ZoneSet": [
      {
          "ZoneState": "AVAILABLE",
          "ZoneId": "100001",
          "Zone": "ap-guangzhou-1",
          "ZoneName": "广州一区"
      },
      {
          "ZoneState": "AVAILABLE",
          "ZoneId": "100002",
          "Zone": "ap-guangzhou-2",
          "ZoneName": "广州二区"
      },
      {
          "ZoneState": "AVAILABLE",
          "ZoneId": "100003",
          "Zone": "ap-guangzhou-3",
          "ZoneName": "广州三区"
      },
      {
          "ZoneState": "AVAILABLE",
          "ZoneId": "100004",
          "Zone": "ap-guangzhou-4",
          "ZoneName": "广州四区"
      }
  ],
  "RequestId": "4fd313a6-155f-4c7a-bf86-898c02fcae02"
}

只看某个字段。

[root@VM_180_248_centos ~]# tccli cvm DescribeZones  --filter TotalCount
4

过滤数组里的子对象,同时还以新的名称展示。

[root@VM_180_248_centos ~]# tccli cvm DescribeZones  --filter 'ZoneSet[*].{name:ZoneName, id:ZoneId}'
[
        {
                "name": "广州一区",
                "id": "100001"
        },
        {
                "name": "广州二区",
                "id": "100002"
        },
        {
                "name": "广州三区",
                "id": "100003"
        },
        {
                "name": "广州四区",
                "id": "100004"
        }
]

华为云CLI

华为云api
https://console.huaweicloud.com/apiexplorer/#/openapi/ECS/doc?api=NovaShowServerAction

华为云CLI介绍:https://support.huaweicloud.com/productdesc-hcli/hcli_01.html

在这里插入图片描述

Azure CLI

#1:添加yum源
echo -e "[azure-cli]
name=Azure CLI
baseurl=https://packages.microsoft.com/yumrepos/azure-cli
enabled=1
gpgcheck=1
gpgkey=https://packages.microsoft.com/keys/microsoft.asc" | sudo tee /etc/yum.repos.d/azure-cli.repo
 
#2:安装azure-cli
yum install -y azure-cli-2.38.0-1.el7
 
#3:卸载
yum  remove azure-cli  -y
rm -rf /etc/yum.repos.d/azure-cli.repo
rm -rf ~/.azure
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值