python修改阿里云ecs密码并启动停止
今天圣诞节,升级了一台阿里云服务器配置,但要在控制台重启下ecs服务器才生效,但重启ecs又要短信验证码,所以最后决定还是用阿里云的python sdk来搞定.
系统:centos 7(64位)
软件环境:python 2.7
1.修改ecs服务器密码
cat /root/soft_shell/ecs_passwd.py
#!/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 ModifyInstanceAttribute from aliyunsdkecs.request.v20140526 import ModifyInstanceAttributeRequest import json list = ['要改密码的ecs id1','要改密码的ecs id2'] clt = client.AcsClient('AccessKeyId','AccessKeySecret','地域名') for i in list: #request = ModifyInstanceAttribute.ModifyInstanceAttribute() request = ModifyInstanceAttributeRequest.ModifyInstanceAttributeRequest() request.set_accept_format('json') request.set_InstanceName("实例名") request.set_Password("密码") request.set_InstanceId(i) action = clt.do_action_with_exception(request) print action
2.启动ecs服务器脚本
cat /root/soft_shell/ecs_start.py
#!/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('AccessKeyId','AccessKeySecret','地域名') for i in list: start = StartInstanceRequest.StartInstanceRequest() start.set_InstanceId(i) action = clt.do_action_with_exception(start) print "现在启动:" + i print action
3.停止ecs服务器脚本
cat /root/soft_shell/ecs_stop.py
#!/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('AccessKeyId','AccessKeySecret','地域名') for i in list1: shutdown = StopInstanceRequest.StopInstanceRequest() shutdown.set_InstanceId(i) action = clt.do_action_with_exception(shutdown) print "现在停机:" + i print action
请各位朋友不要拿脚本去搞事情,造成的任何损失与本网站一概无关.
评论: