def vm(self, ip): ''' list vm related rules :param ip: vm ip :return: ''' debug("Try to show vm rules, ip=%s\n" % ip) port_id = get_port_id_from_ip(ip) debug('The port id is %s\n' % port_id) if not port_id: warn('No port id is found for ip=%s\n' % ip) return output(color_str('## IP = %s, port = %s\n' % (ip, port_id), 'r')) br_port = find_br_ports(port_id) if not br_port: warn('No br port is found for ip=%s\n' % ip) return debug('The br port is %s\n' % br_port) rules_dic = self._query_port_rules(br_port) if rules_dic: output( color_str( _format_str_iptables_rule_ % ('PKTS', 'SOURCE', 'DESTINATION', 'PROT', 'OTHER'), 'b')) for r in rules_dic: if rules_dic[r]: output('%s:\n' % r) self._fmt_show_rules(rules_dic[r])
def vm(self, ip): ''' list vm related rules :param ip: vm ip :return: ''' debug("Try to show vm rules, ip=%s\n" % ip) port_id = get_port_id_from_ip(ip) debug('The port id is %s\n' % port_id) if not port_id: warn('No port id is found for ip=%s\n' % ip) return output(color_str('## IP = %s, port = %s\n' % (ip, port_id), 'r')) br_port = find_br_ports(port_id) if not br_port: warn('No br port is found for ip=%s\n' % ip) return debug('The br port is %s\n' % br_port) rules_dic = self._query_port_rules(br_port) if rules_dic: output(color_str( _format_str_iptables_rule_ % ( 'PKTS', 'SOURCE', 'DESTINATION', 'PROT', 'OTHER'), 'b')) for r in rules_dic: if rules_dic[r]: output('%s:\n' % r) self._fmt_show_rules(rules_dic[r])
def do_set(self, arg): """ <bridge> set Set the default bridge """ if not arg: output('Argument is missed\n') elif not br_exists(arg): output('The bridge does not exist.\n You can check available bridges using show\n') else: self.prompt = color_str('g', PROMPT_KW[:-2] + ':%s> ' % color_str('b', arg)) self.bridge = arg output('Set the default bridge to %s.\n' % self.bridge)
def do_set(self, arg): """ <bridge> set Set the default bridge """ if not arg: output("Argument is missed\n") elif not br_exists(arg): output("The bridge does not exist.\n You can check available bridges using show\n") else: self.prompt = color_str("g", PROMPT_KW[:-2] + ":%s> " % color_str("b", arg)) self.bridge = arg output("Set the default bridge to %s.\n" % self.bridge)
def __init__(self, stdin=sys.stdin, foreground=True): self.bridge = None # default bridge self.ipt = None self.nss = None self.dvr = None if foreground: self.prompt = color_str(PROMPT_KW, 'g') self.stdin = stdin self.in_poller = poll() self.in_poller.register(stdin) Cmd.__init__(self) output("***\n Welcome to EasyOVS %s, " "type help to see available cmds.\n***\n" % VERSION) info('*** Starting CLI:\n') debug("==Loading credentials==\n") debug("auth_url = %s\n" % os.getenv('OS_AUTH_URL') or cfg.CONF.OS.auth_url) debug("username = %s\n" % os.getenv('OS_USERNAME') or cfg.CONF.OS.username) passwd = os.getenv('OS_PASSWORD') or cfg.CONF.OS.password passwd = passwd[:len(passwd)/4] + "****" + passwd[-len(passwd)/4:] debug("password = %s\n" % passwd) debug("tenant_name = %s\n" % os.getenv('OS_TENANT_NAME') or cfg.CONF.OS.tenant_name) while True: try: #if self.isatty(): #quietRun( 'stty sane' ) self.cmdloop() break except KeyboardInterrupt: info('\nInterrupt\n')
def show_port_info(keywords): ''' :param keyword: might be the ip address or substring of the id string :return: related port ''' for keyword in keywords.replace(',', ' ').split(): port = neutron_handler.query_port_by_id(keyword) or \ neutron_handler.query_port_by_ip(keyword) if port: output(color_str('b', '## port_id = %s\n' % (port.get('id')))) for k in port: output('%s: %s\n' %(k, port.get(k))) else: output('%s\n' %(color_str('r', 'No port is found, please ' 'check your tenant information')))
def query_info(keywords): """ :param keyword: might be the ip address or substring of the id string :return: related port """ for keyword in keywords.replace(',', ' ').split(): port = \ neutron_handler.query_port_by_id(keyword) or \ neutron_handler.query_port_by_ip(keyword) if port: output(color_str('## port_id = %s\n' % (port.get('id')), 'b')) for k in port: output('%s: %s\n' % (k, port.get(k))) else: output('%s\n' % (color_str('No port is found, please ' 'check your tenant info', 'r')))
def __init__(self, stdin=sys.stdin, foreground=True): self.bridge = None # default bridge self.ipt = IPtables() if foreground: output('EasyOVS %s, type help for information\n' % VERSION) self.prompt = color_str(PROMPT_KW, 'g') self.stdin = stdin self.in_poller = poll() self.in_poller.register(stdin) Cmd.__init__(self) output("***\n Welcome to EasyOVS," "type help to see available commands.\n***\n") info('*** Starting CLI:\n') debug("==Loading credentials==\n") debug("auth_url = %s\n" % os.getenv('OS_AUTH_URL') or cfg.CONF.OS.auth_url) debug("username = %s\n" % os.getenv('OS_USERNAME') or cfg.CONF.OS.username) passwd = os.getenv('OS_PASSWORD') or cfg.CONF.OS.password passwd = passwd[:len(passwd) / 4] + "****" + passwd[-len(passwd) / 4:] debug("password = %s\n" % passwd) debug("tenant_name = %s\n" % os.getenv('OS_TENANT_NAME') or cfg.CONF.OS.tenant_name) while True: try: #if self.isatty(): #quietRun( 'stty sane' ) self.cmdloop() break except KeyboardInterrupt: info('\nInterrupt\n')
def fmt_output(self): if self.packet > 0: result = color_str('g',self._format_str_ % (self.id, self.table, self.packet, self.priority, compress_mac_str(self.match), self.actions)) else: result = self._format_str_ % (self.id, self.table, self.packet, self.priority, compress_mac_str(self.match), self.actions) output(result)
def br_show(name): """ Show information of a given bridges. """ ovs_ports = Bridge(name).get_ports() if not ovs_ports: return neutron_ports = neutron_handler.get_neutron_ports() debug('get neutron_ports\n') content = [] mac_ip_show = False for intf in ovs_ports: # e.g., qvo-xxx, int-br-eth0, qr-xxx, tapxxx port, tag, intf_type = \ ovs_ports[intf]['port'], ovs_ports[intf]['vlan'], ovs_ports[ intf]['type'] if neutron_ports and intf[3:] in neutron_ports: p = neutron_ports[intf[3:]] vm_ips = ','.join( map(lambda x: x.get('ip_address'), p['fixed_ips'])) vm_mac = p.get('mac_address') mac_ip_show = True else: vm_ips, vm_mac = '', '' content.append((intf, port, tag, intf_type, vm_ips, vm_mac)) # output('%-20s%-8s%-16s%-24s%-8s\n' %(intf,port,vmIP,vmMac,tag)) content.sort(key=lambda x: x[1]) # sort by port content.sort(key=lambda x: x[4]) # sort by vm_ip content.sort(key=lambda x: x[3]) # sort by type output( color_str('%-20s%-12s%-8s%-12s' % ('Intf', 'Port', 'Vlan', 'Type'), 'r')) if mac_ip_show: output(color_str('%-16s%-24s\n' % ('vmIP', 'vmMAC'), 'r')) else: output('\n') i = 0 for _ in content: #color = ['w','g'][i%2] color = 'b' output( color_str('%-20s%-12s%-8s%-12s' % (_[0], _[1], _[2], _[3]), color)) if mac_ip_show: output(color_str('%-16s%-24s\n' % (_[4], _[5]), color)) else: output('\n') i += 1
def list(self): """ List existing namespaces in the system :return: """ ns_list = self.get_ids() if not ns_list: warn('No namespace exists\n') return output(color_str('%d namespaces:\n ' % len(ns_list), 'b')) ns_list_valid = filter(lambda x: not NameSpace(x).is_empty(), ns_list) ns_list_empty = filter(lambda x: NameSpace(x).is_empty(), ns_list) if ns_list_valid: output(color_str('%s\n' % '\t'.join(ns_list), 'b')) if ns_list_empty: output('%s\n' % '\t'.join(ns_list))
def br_show(bridge): """ Show information of a given bridges. """ ovs_ports = br_getports(bridge) if not ovs_ports: return neutron_ports = neutron_handler.get_neutron_ports() debug('get neutron_ports', neutron_ports) content = [] mac_ip_show = False for intf in ovs_ports: # e.g., qvo-xxx, int-br-eth0, qr-xxx, tapxxx port, tag, intf_type = \ ovs_ports[intf]['port'], ovs_ports[intf]['vlan'], ovs_ports[ intf]['type'] if neutron_ports and intf[3:] in neutron_ports: p = neutron_ports[intf[3:]] vm_ips = ','.join(map(lambda x: x.get('ip_address'), p['fixed_ips'])) vm_mac = p.get('mac_address') mac_ip_show = True else: vm_ips, vm_mac = '', '' content.append((intf, port, tag, intf_type, vm_ips, vm_mac)) # output('%-20s%-8s%-16s%-24s%-8s\n' %(intf,port,vmIP,vmMac,tag)) content.sort(key=lambda x: x[1]) # sort by port content.sort(key=lambda x: x[4]) # sort by vm_ip content.sort(key=lambda x: x[3]) # sort by type output(color_str('r', '%-20s%-12s%-8s%-12s' % ('Intf', 'Port', 'Vlan', 'Type'))) if mac_ip_show: output(color_str('r', '%-16s%-24s\n' % ('vmIP', 'vmMAC'))) else: output('\n') i = 0 for _ in content: #color = ['w','g'][i%2] color = 'b' output(color_str(color, '%-20s%-12s%-8s%-12s' % (_[0], _[1], _[2], _[3]))) if mac_ip_show: output(color_str(color, '%-16s%-24s\n' % (_[4], _[5]))) else: output('\n') i += 1
def show(self): ''' Print all rules in this chain :return: ''' output(color_str("chain=%s, policy=%s\n" % (self.name, self.policy), 'b')) for r in self.rules: r.show()
def show(self): ''' Print all rules in this chain :return: ''' output( color_str("chain=%s, policy=%s\n" % (self.name, self.policy), 'b')) for r in self.rules: r.show()
def show(self, chain=None): ''' Get rules from this table :param chain: :return: ''' output(color_str("===table=%s===\n" % self.name, 'r')) for cn in self.chains: if not chain or cn.upper() == chain.upper(): self.chains[cn].show()
def show_iptables_rules(ips): """ Show the iptables rules of given vm ips. """ for ip in ips.replace(',', ' ').split(): port_id = get_port_id_from_ip(ip) if not port_id: output('No local addr %s exists.\n' % ip) continue output(color_str('r', '## IP = %s, port = %s\n' % (ip, port_id))) rules_dic = get_iptables_rules(port_id) if rules_dic: output(color_str('b', _format_str_iptables_rule_ % ('PKTS', 'SOURCE', 'DESTINATION', 'PROT', 'OTHER'))) for r in rules_dic: if rules_dic[r]: output('%s:\n' % r) fmt_show_rules(rules_dic[r])
def fmt_output(self): if self.packet > 0: result = color_str( 'g', self._format_str_ % (self.id, self.table, self.packet, self.priority, compress_mac_str(self.match), self.actions)) else: result = self._format_str_ % ( self.id, self.table, self.packet, self.priority, compress_mac_str(self.match), self.actions) output(result)
def do_exit(self, _arg): """ Go up one level in the command mode structure. If already at the top level, exit from the command line interface and log out. """ if self.bridge: self.bridge = None self.prompt = color_str("g", PROMPT_KW) else: return self.do_quit(_arg)
def do_exit(self, _arg): """ Go up one level in the command mode structure. If already at the top level, exit from the command line interface and log out. """ if self.bridge: self.bridge = None self.prompt = color_str('g', PROMPT_KW) else: return self.do_quit(_arg)
def br_show(name): """ Show information of a given bridges. """ ovs_ports = Bridge(name).get_ports() if not ovs_ports: return neutron_ports = neutron_handler.get_neutron_ports() debug("get neutron_ports\n") content = [] mac_ip_show = False for intf in ovs_ports: # e.g., qvo-xxx, int-br-eth0, qr-xxx, tapxxx port, tag, intf_type = ovs_ports[intf]["port"], ovs_ports[intf]["vlan"], ovs_ports[intf]["type"] if neutron_ports and intf[3:] in neutron_ports: p = neutron_ports[intf[3:]] vm_ips = ",".join(map(lambda x: x.get("ip_address"), p["fixed_ips"])) vm_mac = p.get("mac_address") mac_ip_show = True else: vm_ips, vm_mac = "", "" content.append((intf, port, tag, intf_type, vm_ips, vm_mac)) # output('%-20s%-8s%-16s%-24s%-8s\n' %(intf,port,vmIP,vmMac,tag)) content.sort(key=lambda x: x[1]) # sort by port content.sort(key=lambda x: x[4]) # sort by vm_ip content.sort(key=lambda x: x[3]) # sort by type output(color_str("%-20s%-12s%-8s%-12s" % ("Intf", "Port", "Vlan", "Type"), "r")) if mac_ip_show: output(color_str("%-16s%-24s\n" % ("vmIP", "vmMAC"), "r")) else: output("\n") i = 0 for _ in content: # color = ['w','g'][i%2] color = "b" output(color_str("%-20s%-12s%-8s%-12s" % (_[0], _[1], _[2], _[3]), color)) if mac_ip_show: output(color_str("%-16s%-24s\n" % (_[4], _[5]), color)) else: output("\n") i += 1
def show(self, test_content=None): """ Show the namespace content in format """ self._load(test_content) output(color_str("# Namespace = %s\n" % self.id, 'b')) if len(self.intfs) == 1 and 'lo' == self.intfs.values()[0].get('intf'): output('Only lo interface existed\n') return output(_format_str_ns_intf_ % ('ID', 'Intf', 'Mac', 'IPs')) for d in self.intfs: if self.intfs[d].get('intf') != 'lo': output(_format_str_ns_intf_ % (d, self.intfs[d].get('intf'), self.intfs[d].get('mac'), ', '.join(self.intfs[d].get('ip'))))
def br_show(bridge): """ Show information of a given bridges. """ ovs_ports = br_getports(bridge) if not ovs_ports: return neutron_ports = get_neutron_ports() content = [] mac_ip_show = False for intf in ovs_ports: # e.g., qvo-xxx, int-br-eth0, qr-xxx, tapxxx port, tag, intf_type = ovs_ports[intf]['port'], ovs_ports[intf]['vlan'], ovs_ports[intf]['type'] if neutron_ports and intf[3:] in neutron_ports: vm_ip, vm_mac = neutron_ports[intf[3:]]['ip_address'], neutron_ports[intf[3:]]['mac'] mac_ip_show = True else: vm_ip, vm_mac = '', '' content.append((intf, port, tag, intf_type, vm_ip, vm_mac)) #output('%-20s%-8s%-16s%-24s%-8s\n' %(intf,port,vmIP,vmMac,tag)) content.sort(key=lambda x: x[1]) #sort by port content.sort(key=lambda x: x[4]) #sort by vm_ip content.sort(key=lambda x: x[3]) #sort by type output(color_str('b','%-20s%-12s%-8s%-12s' % ('Intf', 'Port', 'Vlan', 'Type'))) if mac_ip_show: output(color_str('b', '%-16s%-24s\n' % ('vmIP', 'vmMAC'))) else: output('\n') i = 0 for _ in content: color = ['w','g'][i%2] output(color_str(color, '%-20s%-12s%-8s%-12s' % (_[0], _[1], _[2], _[3]))) if mac_ip_show: output(color_str(color, '%-16s%-24s\n' % (_[4], _[5]))) else: output('\n') i += 1
def show(self, test_content=None): """ Show the namespace content in format """ self._load(test_content) output(color_str("# Namespace = %s\n" % self.id, 'b')) if len(self.intfs) == 1 and 'lo' == self.intfs.values()[0].get('intf'): output('Only lo interface existed\n') return output(_format_str_ns_intf_ %('ID', 'Intf', 'Mac', 'IPs')) for d in self.intfs: if self.intfs[d].get('intf') != 'lo': output(_format_str_ns_intf_ % ( d, self.intfs[d].get('intf'), self.intfs[d].get('mac'), ', '.join(self.intfs[d].get('ip'))))
def __init__(self, bridge=None, stdin=sys.stdin): self.prompt = color_str('g', PROMPT_KW) self.bridge = bridge self.stdin = stdin self.in_poller = poll() self.in_poller.register(stdin) Cmd.__init__(self) output("***\n Welcome to EasyOVS, type help to see available commands.\n***\n") info('*** Starting CLI:\n') while True: try: #if self.isatty(): #quietRun( 'stty sane' ) self.cmdloop() break except KeyboardInterrupt: info('\nInterrupt\n')
def br_list(): """ List available bridges. """ bridges = get_all_bridges() if not bridges: output("None bridge exists. Might try using root privilege?\n") return br_info = "" for br in sorted(bridges.keys()): br_info += color_str("%s\n" % br, "r") if bridges[br]["Port"]: br_info += "\tPort:\t\t%s\n" % (" ".join(sorted(bridges[br]["Port"].keys()))) if bridges[br]["Controller"]: br_info += "\tController:\t%s\n" % (" ".join(bridges[br]["Controller"])) if bridges[br]["fail_mode"]: br_info += "\tFail_Mode:\t%s\n" % (bridges[br]["fail_mode"]) output(br_info)
def __init__(self, bridge=None, stdin=sys.stdin): self.prompt = color_str('g', PROMPT_KW) self.bridge = bridge self.stdin = stdin self.in_poller = poll() self.in_poller.register(stdin) Cmd.__init__(self) output( "***\n Welcome to EasyOVS, type help to see available commands.\n***\n" ) info('*** Starting CLI:\n') while True: try: #if self.isatty(): #quietRun( 'stty sane' ) self.cmdloop() break except KeyboardInterrupt: info('\nInterrupt\n')
def br_list(): """ List available bridges. """ bridges = get_all_bridges() if not bridges: output('None bridge exists. Might try using root privilege?\n') return br_info = '' for br in sorted(bridges.keys()): br_info += color_str("%s\n" % br, 'r') if bridges[br]['Port']: br_info += "\tPort:\t\t%s\n" \ % (' '.join(sorted(bridges[br]['Port'].keys()))) if bridges[br]['Controller']: br_info += "\tController:\t%s\n" \ % (' '.join(bridges[br]['Controller'])) if bridges[br]['fail_mode']: br_info += "\tFail_Mode:\t%s\n" % (bridges[br]['fail_mode']) output(br_info)
def __init__(self, bridge=None, stdin=sys.stdin): self.prompt = color_str('g', PROMPT_KW) self.bridge = bridge self.stdin = stdin self.in_poller = poll() self.in_poller.register(stdin) Cmd.__init__(self) output("***\n Welcome to EasyOVS, type help to see available commands.\n***\n") info('*** Starting CLI:\n') debug("==cfg.ADMIN==\n") debug("auth_url = %s\n" % cfg.CONF.OS.auth_url) debug("username = %s\n" % cfg.CONF.OS.username) debug("password = %s\n" % cfg.CONF.OS.password) debug("tenant_name = %s\n" % cfg.CONF.OS.tenant_name) while True: try: #if self.isatty(): #quietRun( 'stty sane' ) self.cmdloop() break except KeyboardInterrupt: info('\nInterrupt\n')
def __init__(self, bridge=None, stdin=sys.stdin): self.prompt = color_str("g", PROMPT_KW) self.bridge = bridge self.stdin = stdin self.in_poller = poll() self.in_poller.register(stdin) Cmd.__init__(self) output("***\n Welcome to EasyOVS, type help to see available commands.\n***\n") info("*** Starting CLI:\n") debug("==cfg.ADMIN==\n") debug("auth_url = %s\n" % cfg.CONF.OS.auth_url) debug("username = %s\n" % cfg.CONF.OS.username) debug("password = %s\n" % cfg.CONF.OS.password) debug("tenant_name = %s\n" % cfg.CONF.OS.tenant_name) while True: try: # if self.isatty(): # quietRun( 'stty sane' ) self.cmdloop() break except KeyboardInterrupt: info("\nInterrupt\n")
def banner_output(): output( color_str( '%-3s%-4s%-10s%-6s%-60s%-20s\n' % ('ID', 'TAB', 'PKT', 'PRI', 'MATCH', 'ACT'), 'g'))
def banner_output(): output(color_str('%-3s%-4s%-10s%-6s%-60s%-20s\n' % ('ID', 'TAB', 'PKT', 'PRI', 'MATCH', 'ACT'), 'g'))