用阿里云python sdk管理安全组

post by rocdk890 / 2018-10-24 17:44 Wednesday linux技术
之前使用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/
根据这篇文章简单修改了下.
夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-742.html

标签: 安全 管理 python api 阿里云 sdk 安全组

评论: