Beispiel #1
0
def ChkScecuGp():
    print '#' * 10 + "Staring Check The ECS Security Group" + '#' * 10

    request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest()
    request.set_accept_format('json')
    request.set_action_name('DescribeSecurityGroups')
    request.set_query_params(dict(Action='DescribeSecurityGroups'))
    result = clt.do_action(request)
    result_dict = json.loads(
        result
    )  #result_dict is dict; loop with another dict with key "SecurityGroups"
    # print type(result_dict['SecurityGroups']) #check the result_dict['SecurityGroups'] type
    # for key in result_dict['SecurityGroups']:
    #     print key
    #     print result_dict['SecurityGroups'][key]
    securgp = result_dict['SecurityGroups'][
        'SecurityGroup']  #result_dict['SecurityGroup']['SecurityGroup'] is a value in dict
    #this is a list, including info we need
    securgpfile = open('securgp.txt', 'w')
    for i in securgp:
        print i['SecurityGroupId'].encode('utf-8').ljust(20),
        print str(i['CreationTime']).ljust(30),
        print i['SecurityGroupName']
        securlist = i['SecurityGroupId'].encode(
            'utf-8') + '\t' + i['CreationTime'].encode(
                'utf-8') + '\t' + i['SecurityGroupName'].encode('utf-8') + '\n'
        securgpfile.write(securlist)
    securgpfile.close()
    print '#' * 10 + 'Check ECS Security Group Finished' + '#' * 10
Beispiel #2
0
 def _get_security_list(self):
     client = AcsClient(access_key_id, access_key_secret,region_id)
     request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest()
     response = client.do_action_with_exception(request)
     json_result = json.loads(response)
     securitygroup_list = json_result['SecurityGroups']['SecurityGroup']
     return securitygroup_list
 def describeSecurityGroupsRequest(self):
     '''查询您创建的安全组的基本信息,例如安全组 ID 和安全组描述等。返回列表按照安全组 ID 降序排列.
     '''
     request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest()
     request.set_PageNumber(1)
     request.set_PageSize(50)
     request.set_accept_format('json')
     return request
def describe_sg(vpc_id, accesskey_id, accesskey_secret, region_id):
    clt = client.AcsClient(accesskey_id, accesskey_secret, region_id)
    request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest()
    request.set_accept_format('json')
    request.set_VpcId(vpc_id)
    result = clt.do_action_with_exception(request)
    js_str = json.loads(result)
    for i in js_str["SecurityGroups"]["SecurityGroup"]:
        print i["SecurityGroupId"], "\t", i["SecurityGroupName"], "\t", i[
            "Description"], "\t", i["CreationTime"]
    return
Beispiel #5
0
    def get_or_create_default_security_group(self, region_code: str) -> str:
        query_request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest(
        )
        query_request.set_SecurityGroupName(self._DEFAULT_SECURITY_GROUP_NAME)
        query_response = self.handle_request(query_request,
                                             region_id=region_code)
        query_sec_groups = query_response["SecurityGroups"].get(
            "SecurityGroup", [])

        sec_group_id = None

        if query_sec_groups:
            sec_group_id = query_sec_groups[0]["SecurityGroupId"]
        else:
            create_request = CreateSecurityGroupRequest.CreateSecurityGroupRequest(
            )
            create_request.set_SecurityGroupName(
                self._DEFAULT_SECURITY_GROUP_NAME)
            create_response = self.handle_request(create_request,
                                                  region_id=region_code)

            sec_group_id = create_response["SecurityGroupId"]
            ip_protocol = "all"
            port_range = "-1/-1"
            source_cidr_ip = "0.0.0.0/0"
            dest_cidr_ip = "0.0.0.0/0"
            policy = "accept"
            self.set_security_group_inner_rule(
                region_code=region_code,
                sec_group_id=sec_group_id,
                source_cidr_ip=source_cidr_ip,
                ip_protocol=ip_protocol,
                port_range=port_range,
                nic_type="internet",
                policy=policy,
            )
            self.set_security_group_outer_rule(
                region_code=region_code,
                sec_group_id=sec_group_id,
                dest_cidr_ip=dest_cidr_ip,
                ip_protocol=ip_protocol,
                port_range=port_range,
                nic_type="internet",
                policy=policy,
            )

        return sec_group_id
Beispiel #6
0
    def describre_securitygroup(self, pagenumber=1):
        pnum = pagenumber
        try:
            request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest(
            )
            request.set_accept_format('json')
            request.add_query_param('PageNumber', pnum)
            request.add_query_param('PageSize', 20)

            # 发起请求
            responses = self.cli.do_action_with_exception(request)

            # 封装交安全组信息
            securitygroup = json.loads(responses.decode('utf-8'))
            return securitygroup
        except Exception as ee:
            print(ee)
 def describeSecurityGroupsRequest(self):
     request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest()
     request.set_accept_format('json')
     return request
Beispiel #8
0
body = client.do_action_with_exception(req)
pp.pprint(json.loads(body.decode("utf-8")))

# VSwitch
req = DescribeVSwitchesRequest.DescribeVSwitchesRequest()
req.set_query_params({'PageSize': 50})
body = client.do_action_with_exception(req)
pp.pprint(json.loads(body.decode("utf-8")))

# zone
req = DescribeZonesRequest.DescribeZonesRequest()
body = client.do_action_with_exception(req)
pp.pprint(json.loads(body.decode("utf-8")))

# SecurityGroup
req = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest()
req.set_query_params({'PageSize': 50})
body = client.do_action_with_exception(req)
pp.pprint(json.loads(body.decode("utf-8")))

# SecurityGroup id
req = DescribeSecurityGroupAttributeRequest.DescribeSecurityGroupAttributeRequest(
)
req.set_SecurityGroupId("")
body = client.do_action_with_exception(req)
pp.pprint(json.loads(body.decode("utf-8")))

# InstanceType
req = DescribeInstanceTypesRequest.DescribeInstanceTypesRequest()
req.set_query_params({'PageSize': 50})
body = client.do_action_with_exception(req)