Python脚本修改阿里云的访问控制列表的方法
作者:李永峰Billy 时间:2022-06-19 10:31:18
需求
对于部署在阿里云上的重要系统一般是不让其他人访问的,所以会在负载均衡(SLB)上加上访问控制列表。而使用ASDL拨号上网的宽带来说一般公网IP都不会固定的,会随时变更公网IP,所以此脚本正是解决此需求。
说明
脚本运行前需要先安装aliyun-python-sdk-core 和aliyun-python-sdk-slb 2个sdk,并且在阿里云账户里面创建access_key和access_secret。
脚本会查询到目前的公网IP,如何创建本地一个文件将IP记录到文件里,下次执行时会将查询到的IP和文件里的对比,如果IP和文件里记录的IP不一致则将IP添加到访问控制列表里。
最后只需要在服务器里每隔一段时间执行一次此脚本就OK。
sdk 下载:https://developer.aliyun.com/tools/sdk#/python
提醒
如果是重要的数据在公网传输,还是尽量使用加密传输。毕竟阿里云的SSL 和IPSEC 也很完善了,推荐使用。
#!/usr/bin/env python3
#coding:utf-8
from aliyunsdkcore import client
import time,requests
from aliyunsdkslb.request.v20140515 import AddAccessControlListEntryRequest
from aliyunsdkcore.profile import region_provider
#region_provider.modify_point('slb', '<regionId>', 'slb.<regionId>.aliyuncs.com')
# 名称:阿里云负载均衡白名单自动修改脚本
### 变量配置 ###
# 保存历史IP地址的文件名
file_save_ipaddr = 'ipaddr.txt'
# 一些可以获取本机出口IP的API地址
ip_api_list = 'http://icanhazip.com,http://ident.me,http://ifconfig.me,http://ipecho.net/plain,http://whatismyip.akamai.com,http://myip.dnsomatic.com'
# SLB 配置,此 Access Key 只需添加 ACL 的权限
aliyun_access_key = 'xxxxxxxxx'
aliyun_access_secret = 'xxxxxxxxxxxxxx'
# 在这里可以获取region:https://help.aliyun.com/document_detail/40654.html
aliyun_region = 'cn-hangzhou'
# 访问列表一(acl-bp1792k8uvk11xxpgu5l)
# 访问列表二(acl-bp1okd1kud9a41kyjkja)
# 需要修改的ACL的ID,进入负载均衡控制台 -> 访问控制 -> 策略ID
aliyun_acl_id = ['acl-bp1okd1kud9a41kyjkja','acl-bp1792k8uvk11xxpgu5l']
### 配置结束 ###
def getExitIpAddr(ip_api_list):
'''获取出口IP地址'''
url_list = str(ip_api_list).split(',')
ip = None
for url in url_list:
resp = requests.get(url,timeout=3)
if resp.status_code == 200:
ip = resp.text.strip()
break
return ip
def setAcl(access_key,access_secret,region,acl_id,ip):
'''修改ACL'''
clt = client.AcsClient(access_key,access_secret,region)
# 设置参数
request = AddAccessControlListEntryRequest.AddAccessControlListEntryRequest()
request.set_accept_format('json')
request.add_query_param('AclId',acl_id)
request.add_query_param('RegionId',region)
request.add_query_param('Tags', 'API自动添加')
request.add_query_param('AclEntrys', '[{{"entry":"{ip}/32","comment":"此处是注释{d}"}}]'.format(ip=ip,d=time.strftime("%Y-%m-%d",time.localtime())))
#添加ip并添加注释
# 发起请求
response = clt.do_action_with_exception(request)
print(response)
def getSavedIp(filename):
'''获取已保存的IP'''
try:
with open(filename,'r',encoding='utf-8') as f:
return f.readline()
except IOError:
print("文件不存在")
def saveNewIp(filename,ipaddr):
'''保存新IP'''
with open(filename,'w',encoding='utf-8') as f:
f.write(ipaddr)
def main():
current_ip = getExitIpAddr(ip_api_list)
saved_ip = getSavedIp(file_save_ipaddr)
print('当前IP',current_ip)
print('保存的IP',saved_ip)
if current_ip == saved_ip:
print('IP无变化')
exit(0)
else:
for acl_id in aliyun_acl_id:
setAcl(access_key=aliyun_access_key,
access_secret=aliyun_access_secret,
region=aliyun_region,
acl_id=acl_id,
ip=current_ip)
time.sleep(5)
saveNewIp(file_save_ipaddr,current_ip)
if __name__ == '__main__':
main()
来源:https://blog.51cto.com/billy98/2358807
标签:Python,脚本,控制列表,阿里云
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python bluetooth蓝牙信息获取蓝牙设备类型的方法
2023-08-23 00:57:16
python中*args与**kwarsg及闭包和装饰器的用法
2023-07-24 00:43:34
![](https://img.aspxhome.com/file/2023/6/60956_0s.png)
基于Python实现的扫雷游戏实例代码
2023-06-26 12:29:34
Python Lambda函数使用总结详解
2022-05-07 11:41:31
玩转MySQL中的外键约束之PHP篇
2010-03-18 10:20:00
详解python 支持向量机(SVM)算法
2022-03-06 02:11:24
![](https://img.aspxhome.com/file/2023/4/75944_0s.png)
Python 编码规范(Google Python Style Guide)
2023-04-12 03:49:35
Scrapy爬虫Response子类在应用中的问题解析
2023-11-03 01:29:31
![](https://img.aspxhome.com/file/2023/4/95834_0s.png)
CSS+asp仿迅雷人气指数
2009-08-03 14:11:00
![](https://img.aspxhome.com/file/UploadPic/20098/3/520098210240-50s.jpg)
DTS构建组件及其如何完成数据转换服务
2009-01-20 15:37:00
Python中的 ansible 动态Inventory 脚本
2022-10-23 07:53:08
![](https://img.aspxhome.com/file/2023/8/65028_0s.png)
asp代码WinHttp.WinHttpRequest.5.1使用例子
2010-03-11 21:28:00
python修改txt文件中的某一项方法
2021-02-08 14:26:40
![](https://img.aspxhome.com/file/2023/8/92088_0s.jpg)
表格梳理解析python内置时间模块看完就懂
2023-10-21 08:10:27
python3.6+opencv3.4实现鼠标交互查看图片像素
2021-09-09 05:27:12
![](https://img.aspxhome.com/file/2023/4/94084_0s.gif)
PHP获取url的函数代码
2023-10-15 12:45:00
网页的栅格设计思考
2007-11-09 10:33:00
![](https://img.aspxhome.com/file/UploadPic/200711/9/2007119103627835s.png)
Pytorch通过保存为ONNX模型转TensorRT5的实现
2023-10-22 13:45:27
opencv实现矿石图片检测矿石数量
2021-08-26 02:17:39
![](https://img.aspxhome.com/file/2023/4/93974_0s.jpg)
对Python的交互模式和直接运行.py文件的区别详解
2023-12-18 00:03:55