利用阿里云pythonSDK修改RSD白名单

因为公司没有购买公网IP,每次路由器重启后IP 就会变动,阿里云服务器上的RDS需要指定白名单才能访问!为了安全性也不能开放所有的IP,每次IP变更后就需要去修改白名单不厌其烦!
参考了GiveMeFive_Y的博客源码https://blog.csdn.net/GiveMeFive_Y/article/details/78687665
修改了一个适合自己的使用的版本
第一步安装SDK

#pip install aliyun-python-sdk-rds
#pip install aliyun-python-sdk-ecs

第二步修改参数

#!/usr/bin/env python
# coding=utf-8
import requests
import json
import os
import re
import sys
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import DescribeSecurityGroupAttributeRequest
from aliyunsdkecs.request.v20140526 import AuthorizeSecurityGroupRequest
from aliyunsdkecs.request.v20140526 import AuthorizeSecurityGroupEgressRequest
from aliyunsdkecs.request.v20140526 import RevokeSecurityGroupRequest
from aliyunsdkecs.request.v20140526 import RevokeSecurityGroupEgressRequest
from aliyunsdkecs.request.v20140526 import DescribeSecurityGroupsRequest
from aliyunsdkrds.request.v20140815 import ModifySecurityIpsRequest
AccessKey = “666666666” #此处填写阿里云的accesskey
AccessSecret = “7777777777” #此处填写阿里云的accesssecret
RegionId = “cn-shanghai” #此处填写RDS服务器所属地域
dbInstanceId = “rm-XXXXXX” #此处填写RDS服务器的实例ID
dbInstanceIPArrayName = “gongshi” #此处填写RDS数据库安全组的名称
dbInstanceIPArrayAttribute = “,” #此处填写RDS数据库安全组的IP的分割符
class Alis():
def __init__(self, AccessKey, AccessSecret, RegionId):
self.AccessKey = AccessKey
self.AccessSecret = AccessSecret
self.RegionId = RegionId
def client(self):
c = client.AcsClient(self.AccessKey, self.AccessSecret, self.RegionId)
return c
def describeSecurityGroupsRequest(self):
request = DescribeSecurityGroupsRequest.DescribeSecurityGroupsRequest()
request.set_accept_format(‘json’)
return request
def modifySecurityIpsRequest(self, DBInstanceId, SecurityIps, DBInstanceIPArrayName, DBInstanceIPArrayAttribute):
“””修改数据库实例白名单
“””
request = ModifySecurityIpsRequest.ModifySecurityIpsRequest()
request.set_DBInstanceId(DBInstanceId)
request.set_SecurityIps(SecurityIps)
request.set_DBInstanceIPArrayName(DBInstanceIPArrayName)
request.set_DBInstanceIPArrayAttribute(DBInstanceIPArrayAttribute)
return request
def my_ip_method_1(self):
get_ip_method = os.popen(‘curl -s ip.cn’)
get_ip_responses = get_ip_method.readlines()[0]
get_ip_pattern = re.compile(r’\d+\.\d+\.\d+\.\d+’)
get_ip_value = get_ip_pattern.findall(get_ip_responses)[0]
print get_ip_value
return get_ip_value
def my_ip_method_2(self):
get_ip_method = os.popen(‘curl -s http://ip-api.com/json’)
get_ip_responses = get_ip_method.readlines()[0]
get_ip_responses = eval(str(get_ip_responses))
get_ip_value = get_ip_responses[‘query’]
return get_ip_value
def my_ip_method_3(self):
get_ip_method = requests.get(‘http://ifconfig.co/json’).content
get_ip_value = eval(get_ip_method)
get_ip_value = get_ip_value[‘ip’]
request = get_ip_value
return request
def read_old_ip(slef):
f = open(‘./ip.txt’)
line = f.read().splitlines()
print line[0]
f.close()
return line[0]
def write_ip_txt(slef,ip):
f = open(‘./ip.txt’,’w’)
print ip
f.write(ip)
f.close()
if __name__ == ‘__main__’:
ali = Alis(AccessKey, AccessSecret, RegionId) # 阿里云后台创建
clt = ali.client()
securityIps = ali.my_ip_method_1()#根据自己情况确定使用哪个公网IP查询接口
old_ip = ali.read_old_ip()
if old_ip == securityIps:
print(‘The specified value of parameter Value is the same as old’)
else:
req = ali.modifySecurityIpsRequest(dbInstanceId, securityIps,dbInstanceIPArrayName, dbInstanceIPArrayAttribute)
res = clt.do_action_with_exception(req)
print(“修改RDS白名单%s” % res)
ip=securityIps
ali.write_ip_txt(ip)
第三步在python的目录下建立一个ip.txt的空文件夹,用于存储记录之前的历史IP
 
 
 
 

发表评论

电子邮件地址不会被公开。