Python源码示例:interface.Interface()
示例1
def auth_errors_table(self):
tab = table.Table()
tab.add_row([
"Interface",
["Authentication", "Errors"],
["Error", "Count"],
["Error", "Rate"],
["Last Change"]
])
for intf in self.interfaces_by_name.values():
for counter in intf.auth_error_counters():
if not counter.is_zero():
tab.add_row([
intf.name,
counter.description(),
counter.value_display_str(),
counter.rate_display_str(),
counter.last_change_display_str()
])
return tab
示例2
def set_mtu_ugly(self, cap):
# sorry about this, this is a hack
# no better solution till the custom fragmentation is not implemented
interface = Interface()
interface.set_mtu(self.config.get("Global", "clientif"), cap)
cap -= 40 # IP+TCP
os.system("iptables -t mangle -F")
os.system("iptables -t mangle -A POSTROUTING -p tcp --tcp-flags SYN,RST SYN -o {0} -j TCPMSS --set-mss {1}".format(self.config.get("Global", "clientif"), cap))
return
# autotune control message handler
# this handler answers to the tune requests to find the best bandwidth
示例3
def better_offer(self, offer1, offer2, three_way_only):
# Don't consider removed offers
if (offer1 is not None) and (offer1.removed):
offer1 = None
if (offer2 is not None) and (offer2.removed):
offer2 = None
# Don't consider offers that are marked "not a ZTP offer"
if (offer1 is not None) and (offer1.not_a_ztp_offer):
offer1 = None
if (offer2 is not None) and (offer2.not_a_ztp_offer):
offer2 = None
# If asked to do so, only consider offers from neighbors in state 3-way as valid candidates
if three_way_only:
if (offer1 is not None) and (offer1.state != interface.Interface.State.THREE_WAY):
offer1 = None
if (offer2 is not None) and (offer2.state != interface.Interface.State.THREE_WAY):
offer2 = None
# If there is only one candidate, it automatically wins. If there are no candidates, there
# is no best.
if offer1 is None:
return offer2
if offer2 is None:
return offer1
# Pick the offer with the highest level
if offer1.level > offer2.level:
return offer1
if offer2.level < offer1.level:
return offer2
# If the level is the same for both offers, pick offer with lowest system id as tie breaker
if offer1.system_id < offer2.system_id:
return offer1
return offer2
示例4
def create_interface(self, interface_config):
interface_name = interface_config['name']
intf = interface.Interface(self, interface_config)
self.interfaces_by_name[interface_name] = intf
self.interfaces_by_id[intf.local_id] = intf
return intf
示例5
def up_interfaces(self, interface_going_down):
for intf in self.interfaces_by_name.values():
if ((intf.fsm.state == interface.Interface.State.THREE_WAY) and
(intf != interface_going_down)):
yield intf
示例6
def have_s_or_ew_adjacency(self, interface_going_down):
# Does this node have at least one south-bound or east-west adjacency?
for intf in self.interfaces_by_name.values():
if ((intf.fsm.state == interface.Interface.State.THREE_WAY) and
(intf != interface_going_down)):
if intf.neighbor_direction() in [constants.DIR_SOUTH,
constants.DIR_EAST_WEST]:
return True
return False
示例7
def have_ew_adjacency(self):
# Does this node have at least one east-west adjacency?
return any(filter(lambda x: x.fsm.state == interface.Interface.State.THREE_WAY and
x.neighbor_direction() == constants.DIR_EAST_WEST,
self.interfaces_by_name.values()))
示例8
def command_show_interface(self, cli_session, parameters):
interface_name = parameters['interface']
if not interface_name in self.interfaces_by_name:
cli_session.print("Error: interface {} not present".format(interface_name))
return
intf = self.interfaces_by_name[interface_name]
cli_session.print("Interface:")
cli_session.print(intf.cli_details_table().to_string())
neighbor_table = intf.cli_neighbor_details_table()
if neighbor_table:
cli_session.print("Neighbor:")
cli_session.print(neighbor_table.to_string())
示例9
def command_show_interfaces(self, cli_session):
# TODO: Report neighbor uptime (time in THREE_WAY state)
tab = table.Table()
tab.add_row(interface.Interface.cli_summary_headers())
for intf in self.interfaces_by_name.values():
tab.add_row(intf.cli_summary_attributes())
cli_session.print(tab.to_string())
示例10
def floodred_interfaces_table(self):
tab = table.Table()
tab.add_row(interface.Interface.cli_floodred_summary_headers())
for intf in self.interfaces_by_name.values():
tab.add_row(intf.cli_floodred_summary_attributes())
return tab
示例11
def command_show_node_stats(self, cli_session, exclude_zero):
cli_session.print("Node ZTP FSM:")
tab = self.node_ztp_fsm_stats_group.table(exclude_zero, sort_by_description=True)
cli_session.print(tab.to_string())
cli_session.print("Node Interfaces Traffic:")
tab = self.intf_traffic_stats_group.table(exclude_zero)
cli_session.print(tab.to_string())
cli_session.print("Node Interfaces Security:")
tab = self.intf_security_stats_group.table(exclude_zero)
cli_session.print(tab.to_string())
cli_session.print("Node Interface LIE FSMs:")
tab = self.intf_lie_fsm_stats_group.table(exclude_zero, sort_by_description=True)
cli_session.print(tab.to_string())
示例12
def update_lldp_neighbors(task):
url = constants.RESTCONF_ROOT + constants.OPENCONFIG_LLDP_NEIGHBORS_ENDPOINT
url = url.format(host=task.host.hostname)
response = requests.get(
url,
headers=constants.HEADERS,
auth=(task.host.username, task.host.password),
verify=False,
)
response.raise_for_status()
result = response.json()["openconfig-lldp:interface"]
device_name = task.host.name
host_interfaces = {}
task.host.data["interfaces"] = host_interfaces
for interface_info in result:
interface_name = interface_info["name"]
interface = Interface(interface_name, device_name)
neighbors = interface_info.get("neighbors")
if not neighbors:
continue
for neighbor_info in neighbors["neighbor"]:
neighbor_state = neighbor_info["state"]
remote_interface_name = neighbor_state["port-description"]
remote_device_fqdn = neighbor_state["system-name"]
remote_device_name = extract_hostname_from_fqdn(remote_device_fqdn)
remote_interface = Interface(remote_interface_name, remote_device_name)
interface.neighbors.append(remote_interface)
host_interfaces[interface.name] = interface
示例13
def __init__(self, interfaces: List["Interface"]) -> None:
self.interfaces = sorted(interfaces)
示例14
def start_interface(self, server):
if server in self.interfaces.keys():
return
i = interface.Interface(server, self.config)
self.pending_servers.add(server)
i.start(self.queue)
return i
示例15
def __init__(self, interfaces: List["Interface"]) -> None:
self.interfaces = sorted(interfaces)
示例16
def parse_cdp_neighbors(task):
url = constants.RESTCONF_ROOT + constants.CDP_NEIGHBORS_ENDPOINT
url = url.format(host=task.host.hostname)
response = requests.get(
url,
headers=constants.HEADERS,
auth=(task.host.username, task.host.password),
verify=False,
)
response.raise_for_status()
cdp_entries = response.json().get("Cisco-IOS-XE-cdp-oper:cdp-neighbor-detail", [])
device_name = task.host.name
host_interfaces = {}
task.host.data["interfaces"] = host_interfaces
for cdp_entry in cdp_entries:
interface_name = cdp_entry["local-intf-name"]
if interface_name in host_interfaces:
interface = host_interfaces[interface_name]
else:
interface = Interface(interface_name, device_name)
host_interfaces[interface_name] = interface
remote_interface_name = cdp_entry["port-id"]
remote_device_fqdn = cdp_entry["device-name"]
remote_device_name = extract_hostname_from_fqdn(remote_device_fqdn)
remote_interface = Interface(remote_interface_name, remote_device_name)
interface.neighbors.append(remote_interface)
示例17
def main():
args = parse_args()
logging.basicConfig(
level=args.loglevel,
format='%(name)s [%(process)d] %(levelname)s %(message)s')
if args.loglevel and not args.debug_requests:
logging.getLogger('requests').setLevel(logging.WARN)
LOG.info('Starting up')
LOG.info('Kubernetes is %s', args.kube_endpoint)
LOG.info('Etcd is %s', args.etcd_endpoint)
LOG.info('Managing interface %s', args.interface)
if args.no_driver:
iface_driver = None
fw_driver = None
else:
iface_driver = interface.Interface(args.interface)
fw_driver = firewall.Firewall(fwchain=args.fwchain,
fwmark=args.fwmark)
mgr = manager.Manager(etcd_endpoint=args.etcd_endpoint,
kube_endpoint=args.kube_endpoint,
etcd_prefix=args.etcd_prefix,
iface_driver=iface_driver,
fw_driver=fw_driver,
cidr_ranges=args.cidr_range,
refresh_interval=args.refresh_interval,
id=args.agent_id)
LOG.info('My id is: %s', mgr.id)
mgr.run()
示例18
def update_arp():
""" Update gateway MAC address
This function creates an instance for each handled
interface and locks it's corresponded gateway mac
address into nftables or arptables.
"""
# reset arptables, removing all rules and
# accept all incoming packages
ac.flush_all()
if args.interface:
interface = Interface(args.interface)
interface.update_gateway_addrs()
Avalon.info(f'ADAPTER={interface.interface}', log=True)
Avalon.info(f'GATEWAY_MAC={interface.gateway_mac}', log=True)
Avalon.info(f'SELF_IP={interface.get_ip()}', log=True)
if interface.gateway_mac:
ac.block(interface.gateway_mac)
else:
# Create one instance for each interface
for interface in interfaces:
interface = Interface(interface)
interface_objects.append(interface)
# make each interface update gateway mac address
for interface in interface_objects:
interface.update_gateway_addrs()
if interface.gateway_mac or interface.get_ip():
Avalon.info(f'ADAPTER={interface.interface}', log=True)
Avalon.info(f'GATEWAY_MAC={interface.gateway_mac}', log=True)
Avalon.info(f'SELF_IP={interface.get_ip()}', log=True)
for interface in interface_objects:
if interface.gateway_mac:
ac.append_allowed_mac(interface.gateway_mac)