Python腳本修改阿里云的訪問控制列表的方法
需求
對于部署在阿里云上的重要系統(tǒng)一般是不讓其他人訪問的,所以會在負載均衡(SLB)上加上訪問控制列表。而使用ASDL撥號上網(wǎng)的寬帶來說一般公網(wǎng)IP都不會固定的,會隨時變更公網(wǎng)IP,所以此腳本正是解決此需求。
說明
腳本運行前需要先安裝aliyun-python-sdk-core 和aliyun-python-sdk-slb 2個sdk,并且在阿里云賬戶里面創(chuàng)建access_key和access_secret。
腳本會查詢到目前的公網(wǎng)IP,如何創(chuàng)建本地一個文件將IP記錄到文件里,下次執(zhí)行時會將查詢到的IP和文件里的對比,如果IP和文件里記錄的IP不一致則將IP添加到訪問控制列表里。
最后只需要在服務(wù)器里每隔一段時間執(zhí)行一次此腳本就OK。
sdk 下載:https://developer.aliyun.com/tools/sdk#/python
提醒
如果是重要的數(shù)據(jù)在公網(wǎng)傳輸,還是盡量使用加密傳輸。畢竟阿里云的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 的權(quán)限
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']
### 配置結(jié)束 ###
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)
# 設(shè)置參數(shù)
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":"此處是注釋ublnpf9mb"}}]'.format(ip=ip,d=time.strftime("%Y-%m-%d",time.localtime())))
#添加ip并添加注釋
# 發(fā)起請求
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()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Python實現(xiàn)一鍵獲取電腦瀏覽器的賬號密碼
發(fā)現(xiàn)很多人在學校圖書館喜歡用電腦占座,而且出去的時候經(jīng)常不鎖屏,為了讓大家養(yǎng)成良好的習慣,本文將分享一個小程序,可以快速獲取你存儲在電腦瀏覽器中的所有賬號和密碼,感興趣的可以了解一下2022-05-05
基于TensorFlow的CNN實現(xiàn)Mnist手寫數(shù)字識別
這篇文章主要為大家詳細介紹了基于TensorFlow的CNN實現(xiàn)Mnist手寫數(shù)字識別,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06

