最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2019年03月08日 10:34:37   作者:李永峰Billy  
這篇文章主要介紹了Python腳本修改阿里云的訪問控制列表的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

需求

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

說明

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

腳本會查詢到目前的公網(wǎng)IP,如何創(chuàng)建本地一個(gè)文件將IP記錄到文件里,下次執(zhí)行時(shí)會將查詢到的IP和文件里的對比,如果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-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')

# 名稱:阿里云負(fù)載均衡白名單自動修改腳本

### 變量配置 ###
# 保存歷史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.html
aliyun_region = 'cn-hangzhou'
# 訪問列表一(acl-bp1792k8uvk11xxpgu5l)
# 訪問列表二(acl-bp1okd1kud9a41kyjkja)
# 需要修改的ACL的ID,進(jìn)入負(fù)載均衡控制臺 -> 訪問控制 -> 策略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":"此處是注釋wppm3vysvbp"}}]'.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('當(dāng)前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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

贡嘎县| 织金县| 白城市| 上林县| 尉氏县| 阿尔山市| 邛崃市| 婺源县| 双鸭山市| 额尔古纳市| 射阳县| 马关县| 额尔古纳市| 六枝特区| 郎溪县| 洛南县| 丰镇市| 体育| 冷水江市| 襄樊市| 和政县| 友谊县| 广宁县| 工布江达县| 白朗县| 奉新县| 杭锦旗| 宜兰市| 靖远县| 监利县| 独山县| 沂水县| 禹州市| 张家港市| 平潭县| 渝北区| 涡阳县| 阳城县| 米林县| 海盐县| 丁青县|