国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python腳本修改阿里云的訪問控制列表的方法

2020-01-04 13:35:48
字體:
供稿:網(wǎng)友

需求

對(duì)于部署在阿里云上的重要系統(tǒng)一般是不讓其他人訪問的,所以會(huì)在負(fù)載均衡(SLB)上加上訪問控制列表。而使用ASDL撥號(hào)上網(wǎng)的寬帶來說一般公網(wǎng)IP都不會(huì)固定的,會(huì)隨時(shí)變更公網(wǎng)IP,所以此腳本正是解決此需求。

說明

腳本運(yùn)行前需要先安裝aliyun-python-sdk-core 和aliyun-python-sdk-slb 2個(gè)sdk,并且在阿里云賬戶里面創(chuàng)建access_key和access_secret。

腳本會(huì)查詢到目前的公網(wǎng)IP,如何創(chuàng)建本地一個(gè)文件將IP記錄到文件里,下次執(zhí)行時(shí)會(huì)將查詢到的IP和文件里的對(duì)比,如果IP和文件里記錄的IP不一致則將IP添加到訪問控制列表里。
最后只需要在服務(wù)器里每隔一段時(shí)間執(zhí)行一次此腳本就OK。

sdk 下載:https://developer.aliyun.com/tools/sdk#/python

提醒

如果是重要的數(shù)據(jù)在公網(wǎng)傳輸,還是盡量使用加密傳輸。畢竟阿里云的SSL 和IPSEC 也很完善了,推薦使用。

#!/usr/bin/env python3#coding:utf-8from aliyunsdkcore import clientimport time,requestsfrom aliyunsdkslb.request.v20140515 import AddAccessControlListEntryRequestfrom aliyunsdkcore.profile import region_provider#region_provider.modify_point('slb', '<regionId>', 'slb.<regionId>.aliyuncs.com')# 名稱:阿里云負(fù)載均衡白名單自動(dòng)修改腳本### 變量配置 #### 保存歷史IP地址的文件名file_save_ipaddr = 'ipaddr.txt'# 一些可以獲取本機(jī)出口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.htmlaliyun_region = 'cn-hangzhou'# 訪問列表一(acl-bp1792k8uvk11xxpgu5l)# 訪問列表二(acl-bp1okd1kud9a41kyjkja)# 需要修改的ACL的ID,進(jìn)入負(fù)載均衡控制臺(tái) -> 訪問控制 -> 策略IDaliyun_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 ipdef 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自動(dòng)添加')  request.add_query_param('AclEntrys', '[{{"entry":"{ip}/32","comment":"此處是注釋iwvjtn8m0"}}]'.format(ip=ip,d=time.strftime("%Y-%m-%d",time.localtime())))    #添加ip并添加注釋  # 發(fā)起請(qǐng)求  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('當(dāng)前IP',current_ip)  print('保存的IP',saved_ip)  if current_ip == saved_ip:    print('IP無(wú)變化')    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()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 海安县| 黄山市| 黄冈市| 肇源县| 溆浦县| 泸州市| 南昌市| 宝鸡市| 漳平市| 武胜县| 乌什县| 盖州市| 内丘县| 蓬莱市| 沧州市| 什邡市| 桦甸市| 江达县| 屏东市| 晋江市| 怀仁县| 祁连县| 万宁市| 左云县| 浠水县| 济阳县| 普兰县| 宁远县| 衡东县| 都昌县| 内江市| 黎城县| 永福县| 旅游| 沿河| 墨江| 中牟县| 句容市| 安丘市| 沂源县| 疏附县|