用阿里云python sdk管理安全组
之前使用go语言的sdk来管理阿里云安全组,最近一直想使用python来进行管理,毕竟python方便管理,交接也方便,是个运维都可以看得懂的.
系统:centos 7(64位)
软件环境:python 2.7.x
1.安装python-pip
yum -y install python-pip
2.安装阿里云python sdk
pip install aliyun-python-sdk-ecs
3.管理安全组
#!/usr/bin/env python # coding=utf-8 from aliyunsdkcore import client from aliyunsdkecs.request.v20140526 import DescribeSecurityGroupAttributeRequest from aliyunsdkecs.request.v20140526 import DescribeSecurityGroupsRequest from aliyunsdkecs.request.v20140526 import RevokeSecurityGroupRequest from aliyunsdkecs.request.v20140526 import AuthorizeSecurityGroupRequest import json def describeSecurityGroupAttributeRequest(): """查询安全组规则 """ request = DescribeSecurityGroupAttributeRequest.DescribeSecurityGroupAttributeRequest() request.set_SecurityGroupId('sg-9xxxx') request.set_accept_format('json') return request def describeSecurityGroupsRequest(): """查询安全组名称 """ request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest() request.set_accept_format('json') return request def revokeSecurityGroupRequest(): """撤销安全组规则 """ request = RevokeSecurityGroupRequest.RevokeSecurityGroupRequest() request.set_SecurityGroupId('sg-9xxxx') request.set_IpProtocol('all') request.set_PortRange('-1/-1') request.set_SourceCidrIp('0.0.0.0/0') request.set_Policy('accept') request.set_accept_format('json') return request def authorizeSecurityGroupRequest(): """授权安全组规则 """ request = AuthorizeSecurityGroupRequest.AuthorizeSecurityGroupRequest() request.set_SecurityGroupId('sg-9xxxx') request.set_IpProtocol('tcp') request.set_PortRange('3389/3389') request.set_SourceCidrIp('0.0.0.0/0') request.set_Priority('100') request.set_Policy('accept') request.set_accept_format('json') return request if __name__ == '__main__': clt = client.AcsClient('AccessKeyId','AccessKeySecret','cn-shenzhen')
#request = revokeSecurityGroupRequest() #request = authorizeSecurityGroupRequest() #request = describeSecurityGroupAttributeRequest() request = describeSecurityGroupsRequest() result = clt.do_action(request) print(result)
ps:
http://www.ttlsa.com/python/aliyun-python-sdk-management-securitygourp/
根据这篇文章简单修改了下.
评论: