python修改阿里云ecs密码并启动停止

post by rocdk890 / 2018-12-25 17:30 Tuesday linux技术
今天圣诞节,升级了一台阿里云服务器配置,但要在控制台重启下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

请各位朋友不要拿脚本去搞事情,造成的任何损失与本网站一概无关.
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-748.html

标签: centos linux 修改 启动 密码 python sdk ecs 停止

评论: