def geteveryslbinfo(): """ 返回有两元素组成的元组 第一个元素是以slb IP为键的字典(包含SLB name,后端服务器名称和IP) 第二个元素是字符串列表,内容为每个SLB的IP,SLB名称,后端服务器IP及名称 """ rel = GetSlbIpAll() rel.run() slbinfod = {} slbips = rel.iplist for slbip in slbips: slbinfod[slbip] = {} slb = GetSlbInfo(slbip) slb.run() slbinfod[slbip]['slbName'] = slb.slbName if slb.backendServerId: slbinfod[slbip]['backendServerIp'] = slb.backendServerIp slbinfod[slbip]['backendServerName'] = slb.backendServerName else: slbinfod[slbip]['backendServerIp'] = '' slbinfod[slbip]['backendServerName'] = '' slbinfol = [] for key in slbinfod: slbinfol.append(slbinfod[key]['slbName'] + ',' + key + ',' + slbinfod[key]['backendServerName'] + ',' + slbinfod[key]['backendServerIp']) slbinfol.sort() return (slbinfod, slbinfol)
def __init__(self, address, name=None, resultFormat=resultFormat): Aliyunsdk.__init__(self) self.resultFormat = resultFormat rel = GetSlbIpAll() rel.run() if address not in rel.iplist: print('IP: %s is not exists' % address) os._exit(17) self.slbip = address rel = SlbIpToId(self.slbip) rel.run() self.slbid = rel.slbid #如果不提供name参数则根据服务器名称生成name if not name: rel = GetSlbInfo(self.slbip) rel.run() if rel.backendServerId: name = rel.backendServerName.split('-')[-1] t = mydatetime() name = name + '-' + t['year'] + t['mon'] + t['day'] else: name = 'tmp' t = mydatetime() name = name + t['year'] + t['mon'] + t['day'] self.slbname = name self.request = SetLoadBalancerNameRequest() self.request.set_LoadBalancerId(self.slbid) self.request.set_LoadBalancerName(self.slbname)
def __init__(self, slbIp, listenPort, resultFormat=resultFormat): Aliyunsdk.__init__(self) self.listenPort = int(listenPort) #要开放的端口 self.resultFormat = resultFormat self.slbIp = slbIp #要开放端口的SLB IP self.slballip = GetSlbIpAll() self.slballip.run() if not self.slbIp in self.slballip.iplist: #检查SLB IP是否有错 print('SLB IP: %s not exists' % self.slbIp) os._exit(17) self.slbinfo = GetSlbInfo(slbIp) #获取SLB的相关信息 self.slbinfo.run() if len(self.slbinfo.listenerPorts) == 50: #开放端口达到50个,停止添加 print('50 Ports') os._exit(17) if self.listenPort in self.slbinfo.listenerPorts: #如果端口已经开放则跳过 raise SkipPort(self.listenPort) #设置相关参数 self.request = CreateLoadBalancerTCPListenerRequest() self.request.set_accept_format(self.resultFormat) self.request.set_LoadBalancerId(self.slbinfo.slbid) self.request.set_ListenerPort(self.listenPort) self.request.set_BackendServerPort(self.listenPort) #后端服务器开放的端口 self.request.set_Bandwidth(-1) #设置带宽,-1为不限制
class SlbDeletePort(Aliyunsdk): """ 把要删除的SLB IP和端口作为参数,生成该类的实例 一个实例只删除一个端口,多个端口需要多个实例 """ def __init__(self, slbIp, listenPort, resultFormat=resultFormat): Aliyunsdk.__init__(self) self.listenPort = int(listenPort) self.resultFormat = resultFormat self.slbIp = slbIp self.slballip = GetSlbIpAll() self.slballip.run() if not self.slbIp in self.slballip.iplist: #检查SLB IP是否正确,否则退出 print('SLB IP: %s not exists' % self.slbIp) os._exit(17) self.slbinfo = GetSlbInfo(slbIp) self.slbinfo.run() if self.listenPort not in self.slbinfo.listenerPorts: #检查要删除的端口是否存在,否则跳过 raise SkipPort(self.listenPort) self.request = DeleteLoadBalancerListenerRequest() self.request.set_accept_format(self.resultFormat) self.request.set_LoadBalancerId(self.slbinfo.slbid) self.request.set_ListenerPort(self.listenPort) def handling(self): if u'RequestId' in self.result.keys(): print('Port %s deleted' % self.listenPort) Aliyunsdk.handling(self)
def __init__(self, address, resultFormat=resultFormat): Aliyunsdk.__init__(self) self.resultFormat = resultFormat rel = GetSlbIpAll() rel.run() if address not in rel.iplist: print('IP: %s is not exists' % address) os._exit(17) self.request = DescribeLoadBalancersRequest() self.request.set_accept_format(self.resultFormat) self.request.set_Address(address) self.slbid = ''
class SlbAddTcpPort(Aliyunsdk): """ 把要开放的SLB IP和端口作为参数,生成该类的实例 一个实例只开放一个端口,多个端口需要多个实例 开放一个端口的同时也会设置相同的后端端口并激活端口 """ def __init__(self, slbIp, listenPort, resultFormat=resultFormat): Aliyunsdk.__init__(self) self.listenPort = int(listenPort) #要开放的端口 self.resultFormat = resultFormat self.slbIp = slbIp #要开放端口的SLB IP self.slballip = GetSlbIpAll() self.slballip.run() if not self.slbIp in self.slballip.iplist: #检查SLB IP是否有错 print('SLB IP: %s not exists' % self.slbIp) os._exit(17) self.slbinfo = GetSlbInfo(slbIp) #获取SLB的相关信息 self.slbinfo.run() if len(self.slbinfo.listenerPorts) == 50: #开放端口达到50个,停止添加 print('50 Ports') os._exit(17) if self.listenPort in self.slbinfo.listenerPorts: #如果端口已经开放则跳过 raise SkipPort(self.listenPort) #设置相关参数 self.request = CreateLoadBalancerTCPListenerRequest() self.request.set_accept_format(self.resultFormat) self.request.set_LoadBalancerId(self.slbinfo.slbid) self.request.set_ListenerPort(self.listenPort) self.request.set_BackendServerPort(self.listenPort) #后端服务器开放的端口 self.request.set_Bandwidth(-1) #设置带宽,-1为不限制 def run(self): #重载run方法 Aliyunsdk.run(self) #调用父类的run依法 sp = SlbStartPort(self.slbinfo.slbid, self.listenPort) #激活端口 sp.run() def handling(self): if u'RequestId' in self.result.keys(): print('Port %s opened' % self.listenPort) Aliyunsdk.handling(self)
def __init__(self, slbIp, listenPort, resultFormat=resultFormat): Aliyunsdk.__init__(self) self.listenPort = int(listenPort) self.resultFormat = resultFormat self.slbIp = slbIp self.slballip = GetSlbIpAll() self.slballip.run() if not self.slbIp in self.slballip.iplist: #检查SLB IP是否正确,否则退出 print('SLB IP: %s not exists' % self.slbIp) os._exit(17) self.slbinfo = GetSlbInfo(slbIp) self.slbinfo.run() if self.listenPort not in self.slbinfo.listenerPorts: #检查要删除的端口是否存在,否则跳过 raise SkipPort(self.listenPort) self.request = DeleteLoadBalancerListenerRequest() self.request.set_accept_format(self.resultFormat) self.request.set_LoadBalancerId(self.slbinfo.slbid) self.request.set_ListenerPort(self.listenPort)
#!/usr/bin/env python # -- coding: utf-8 -- """ 设置SLB端口,先删除原有端口,再设置参数提供的端口 使用: python slbsettcpport.py SLB_IP Port | Port,Port,Port... | StartPort EndPort' """ import sys, os from getslbinfo import GetSlbInfo from getslbipall import GetSlbIpAll from checkargs import checkargv slbip = sys.argv[1] rel = GetSlbIpAll() rel.run() if slbip not in rel.iplist: #检查SLB-IP的正确 print('IP: %s is not exists' % slbip) os._exit(17) rel = GetSlbInfo(slbip) rel.run() ports1 = rel.listenerPorts #原有端口,将被删除 ports2 = checkargv() #新开端口 if not ports2: print( 'Usage: python %s SLB_IP Port | Port,Port,Port... | StartPort EndPort' % sys.argv[0]) os._exit(17) ports2 = ','.join(ports2)