通过阿里云接口设置安全组只针对出口公网开放
直接上脚本#!/usr/bin/env python#coding=utf-8import re,requestsimport subprocess,jsonfrom urllib import requestfrom aliyunsdkcore.client import AcsClientfrom aliyunsdkcore.acs_exception.exceptions import Cl
·
直接上脚本
#!/usr/bin/env python
#coding=utf-8
import re,requests
import subprocess,json
from urllib import request
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526.RevokeSecurityGroupRequest import RevokeSecurityGroupRequest
from aliyunsdkecs.request.v20140526.AuthorizeSecurityGroupRequest import AuthorizeSecurityGroupRequest
from aliyunsdkecs.request.v20140526.DescribeSecurityGroupAttributeRequest import DescribeSecurityGroupAttributeRequest
# 获取当前公网ip
def get_cookie():
LOGIN_URL = 'https://192.168.255.1/logincheck' #请求的URL地址
DATA = {"ajax":'1',"username":'',"secretkey":''} #登录系统的账号密码,也是我们请求数据
HEADERS = {
"Content-Type" : "application/json",
"Referer": "https://192.168.255.1/login",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
}
requests.packages.urllib3.disable_warnings()
RES = requests.post(LOGIN_URL,data=DATA,headers=HEADERS,verify = False) #模拟登陆操作
if RES.status_code == 200:
return RES.cookies
else:
return 0
def get_info(ip):
cookie = get_cookie()
if cookie == 0:
return ip
else:
LOGIN_URL = 'https://192.168.255.1/p/system/interface/status/wan2/' # 请求的URL地址
HEADERS = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Content-Type": "application/json",
"Referer": "https://192.168.255.1/ng/page/p/system/interface/status/wan2/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"
}
requests.packages.urllib3.disable_warnings()
RES = requests.get(LOGIN_URL, cookies=cookie, verify=False) # 模拟登陆操作
if RES.status_code == 200:
return (RES.text.split("span")[3].split(" ")[0][1:])
else:
return ip
class Auth_Recahnge(object):
def __init__(self,key,value,where):
self.key = key
self.value = value
self.where = where
self.client = AcsClient(key,value,where)
def get_input_ip(self):
p = subprocess.Popen("curl cip.cc | awk NR==1{'print $3'}",stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
p = p.stdout.read()[:-1]
p = bytes.decode(p)
self.p = p
print(p)
return p
def get_web_ip(self):
ip = get_info(self.p)
self.ip = ip
print(ip)
return ip
def get_auth_info(self):
request = DescribeSecurityGroupAttributeRequest()
request.set_accept_format('json')
request.add_query_param('RegionId', 'cn-shenzhen')
request.set_SecurityGroupId('sg-wz97uy81am6rd7bcscwc')
request.set_Direction('ingress')
response = self.client.do_action_with_exception(request)
data = str(response,encoding='utf-8')
datas = json.loads(data)
info = datas['Permissions']['Permission']
for i in info:
if i['PortRange'] == "19000/19000":
print(i['SourceCidrIp'])
self.SourceCidrIp = i['SourceCidrIp']
if i['SourceCidrIp'] != self.ip:
self.stat = 1
else:
self.stat = 0
def Del_add_Group(self):
if self.stat == 1:
# 删除规则
request = RevokeSecurityGroupRequest()
request.set_accept_format('json')
request.add_query_param('RegionId', 'cn-shenzhen')
request.set_SecurityGroupId("sg-wz97uy81am6rd7bcscwc")
request.set_PortRange("19000/19000")
request.set_IpProtocol("tcp")
request.set_SourceCidrIp(self.SourceCidrIp)
response = self.client.do_action_with_exception(request)
request.set_Description("公司出口公网访问")
print(str(response, encoding='utf-8'))
#添 加规则
request = AuthorizeSecurityGroupRequest()
request.set_accept_format('json')
request.add_query_param('RegionId', 'cn-shenzhen')
request.set_SecurityGroupId("sg-wz97uy81am6rd7bcscwc") #安全组ID
request.set_IpProtocol("tcp")
request.set_PortRange("19000/19000")
request.set_Description("公司出口公网访问")
request.set_SourceCidrIp(self.ip)
response = self.client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))
if __name__ == '__main__':
key = ''
value = ''
where = ''
auth = Auth_Recahnge(key,value,where)
auth.get_input_ip()
auth.get_web_ip()
auth.get_auth_info()
auth.Del_add_Group()
更多推荐
已为社区贡献2条内容
所有评论(0)