All Products
Document Center

Query all instance IDs in a specified region and export them to a file

Last Updated: Jan 08, 2018

You can run the script below in Alibaba Cloud CLI to query all instance IDs in a specified region, and export them to a file. You can choose to download or edit the script.

Click to download the script:

The script is as follows.

  1. #!/bin/bash
  2. tcount=`aliyuncli ecs DescribeInstances --RegionId $1 --output json --filter TotalCount`
  3. pageNum=1
  4. cat /dev/null >vmList.txt
  5. while ((tcount>0))
  6. do
  7. aliyuncli ecs DescribeInstances --RegionId $1 --PageSize 100 --PageNumber $pageNum --output json --filter Instances.Instance[*].InstanceId | sed '1d' | sed '$d' | sed 's/,//g' | sed 's/"//g'| sed 's/ //g'>>vmList.txt
  8. let pageNum++
  9. let tcount-=100
  10. done
  11. cat vmList.txt

After you edit or download the script and grant the permission (by running the chmod + x command), you can run the script in the following format to generate a vmList.txt file containing required instance IDs in the directory storing the script.


  1. ./ <Region ID>

Note: You can obtain the Region ID through the DescribeRegions interface.


  1. ./ cn-hangzhou

Sample output:

  1. #./ cn-hangzhou
  2. cn-hangzhou sg-227f611ss
  3. cn-hangzhou sg-22esa2s7s