阿里云购买、启动、停止ecs等等操作的python脚本

以下所有脚本都是在python 2.7的环境亲自测试的。
阿里云的ak/sk是没有地域概念的,在任何地域都可以使用。

购买服务器

以在新加坡购买服务器为例子:

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
#!/usr/bin/env python
#coding=utf-8
#注意!服务器创建完毕之后,状态是关机的。
from aliyunsdkcore import client
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526 import CreateInstanceRequest
# 创建 Client 实例
clt = client.AcsClient('阿里云ak','阿里云sk','新加坡的地域') #各地域的缩写请看:https://help.aliyun.com/document_detail/40654.html?spm=5176.doc25499.2.14.yh6n8c

# 创建 request,并设置参数
request = CreateInstanceRequest.CreateInstanceRequest()

# 设置ECS细节
request.set_ImageId("centos_7_04_64_20G_alibase_201701015.vhd") #这里是镜像
request.set_InstanceName("xjp-test-001") #这里写名称xjp-test-001
request.set_SecurityGroupId("sg-23t6c6mjw") #这里是安全组
request.set_Password("W2.bi7FX1dyb)T3Wh^,[") #这里是密码,推荐使用https传输,安全
request.set_InstanceChargeType("PrePaid") #确定是包年包月
request.set_Period("2") #先买两个月的
request.set_SystemDiskCategory("cloud_efficiency") #注意,如果是海外的机器的话,要额外说明,海外的机器只有高速云盘和SSD盘

# 设置实例规格
request.set_InstanceType("ecs.s2.large")

# 发起 API 请求并打印返回
response = clt.do_action_with_exception(request)
print response

服务器停机

停止ECS的脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import StopInstanceRequest
list1 = ['要停机的ecs id1','要停机的ecs id2','要停机的ecs id3'...]

clt = client.AcsClient('阿里云ak','阿里云sk','地域名')

for i in list1:
shutdown = StopInstanceRequest.StopInstanceRequest()
shutdown.set_InstanceId(i)
action = clt.do_action_with_exception(shutdown)
print "现在停机:" + i
print action

服务器启动

启动ECS的脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import StartInstanceRequest
list = ['要停机的ecs id1','要停机的ecs id2','要停机的ecs id3'...]

clt = client.AcsClient('阿里云ak','阿里云sk','地域名')

for i in list:
start = StartInstanceRequest.StartInstanceRequest()
start.set_InstanceId(i)
action = clt.do_action_with_exception(start)
print "现在启动:" + i
print action

查询阿里云镜像

查询ECS镜像的脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import DescribeImagesRequest
import aliyunsdkcore.request

clt = client.AcsClient('阿里云ak','阿里云sk','地域名')

request = DescribeImagesRequest.DescribeImagesRequest()
request.set_accept_format('json')

# 发起请求
response = clt.do_action_with_exception(request)
print response

查询服务器规格

查询ECS规格的脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import DescribeInstanceTypesRequest
import aliyunsdkcore.request

clt = client.AcsClient('阿里云ak','阿里云sk','地域名')

request = DescribeInstanceTypesRequest.DescribeInstanceTypesRequest()
request.set_accept_format('json')

# 发起请求
response = clt.do_action_with_exception(request)
print response

参考资料

https://help.aliyun.com/document_detail/25499.html?spm=5176.doc25501.6.857.wR0MHP

小马哥

-------------This article is over!Thanks for reading!-------------
感谢您请我喝咖啡!(o´ω`o)
0%