Файловый менеджер - Редактировать - /opt/imh-python/lib/python3.9/site-packages/pyroute2/ndb/objects/__pycache__/address.cpython-39.pyc
Ðазад
a ]�hE# � @ s� d Z ddlmZ ddlmZ ddlmZ dd� Ze�� � dd d dd�� d dd�Zdeggdeggeegid�ZG dd� de�Z dS )a .. testsetup:: * from socket import AF_INET from pyroute2 import NDB from pyroute2 import config config.mock_iproute = True ndb = NDB() .. testcleanup:: * ndb.close() Using the global view ===================== The `addresses` view provides access to all the addresses registered in the DB, as well as methods to create and remove them: .. testcode:: eth0 = ndb.interfaces['eth0'] # create an address ndb.addresses.create( address='10.0.0.1', prefixlen=24, index=eth0['index'], ).commit() # remove it with ndb.addresses['10.0.0.1/24'] as addr: addr.remove() # list addresses for record in ndb.addresses.summary(): print(record) .. testoutput:: :hide: ('localhost', 0, 'lo', '127.0.0.1', 8) ('localhost', 0, 'eth0', '192.168.122.28', 24) Using ipaddr views ================== Interfaces also provide address views as subsets of the global address view: .. testcode:: with ndb.interfaces['eth0'] as eth0: for record in eth0.ipaddr.summary(): print(record) .. testoutput:: ('localhost', 0, 'eth0', '192.168.122.28', 24) It is possible use the same API as with the global address view: .. testcode:: with ndb.interfaces['eth0'] as eth0: eth0.ipaddr.create( address='10.0.0.1', prefixlen=24 # index is implied ).commit() for record in ndb.addresses.summary(): print(record) .. testoutput:: ('localhost', 0, 'lo', '127.0.0.1', 8) ('localhost', 0, 'eth0', '10.0.0.1', 24) ('localhost', 0, 'eth0', '192.168.122.28', 24) Using interface methods ======================= Interfaces provide also simple methods to manage addresses: .. testcode:: with ndb.interfaces['eth0'] as eth0: eth0.del_ip('192.168.122.28/24') # remove an existing address eth0.del_ip(family=AF_INET) # ... or remove all IPv4 addresses eth0.add_ip('10.0.0.1/24') # add a new IP address eth0.add_ip(address='10.0.0.2', prefixlen=24) # ... or using keywords eth0.set('state', 'up') with ndb.addresses.summary() as report: report.select_records(ifname='eth0') for address in report: print(address) .. testoutput:: ('localhost', 0, 'eth0', '10.0.0.1', 24) ('localhost', 0, 'eth0', '10.0.0.2', 24) Functions `add_ip()` and `del_ip()` return the interface object, so they can be chained as in the example above, and the final `commit()` will commit all the changes in the chain. The keywords to `del_ip()` are the same object field names that may be used in the selectors or report filters: .. testcode:: with ndb.interfaces['eth0'] as eth0: eth0.del_ip(prefixlen=24) # remove all addresses with mask /24 A match function that may be passed to the `del_ip()` is the same as for `addresses.dump().select_records()`, and it gets a named tuple as the argument. The fields are named in the same way as address objects fields. So if you want to filter addresses by a pattern or the `prefixlen` field with a match function, you may use: .. testcode:: x1 with ndb.interfaces['eth0'] as eth0: eth0.add_ip('10.0.0.1/25') with ndb.interfaces['eth0'] as eth0: eth0.del_ip(lambda x: x.address.startswith('192.168')) eth0.del_ip(lambda x: x.prefixlen == 25) An empty `del_ip()` removes all the IP addresses on the interface: .. testcode:: x2 with ndb.interfaces['eth0'] as eth0: eth0.del_ip() # flush all the IP:s Accessing one address details ============================= Access an address as a separate RTNL object: .. testcode:: x3 print(ndb.addresses['192.168.122.28/24']) .. testoutput:: x3 :hide: {'target': 'localhost', 'address': '192.168.122.28', 'prefixlen': 24, 'tflags': 0, 'family': 2, 'index': 2, 'local': '192.168.122.28', 'flags': 512, 'scope': 0, 'label': 'eth0', 'broadcast': '192.168.122.255', 'anycast': None, 'multicast': None} Please notice that address objects are read-only, you may not change them, only remove old ones, and create new. � )� ifaddrmsg)�AddressFieldFilter� )�RTNL_Objectc C s� | � d||� |d d d r~|�d�r~| �d| j| jf ||d f��� }t|�s~| �d| j| j| jf ||d |d f� d S )N� addresses�header�typer �indexz� SELECT * FROM addresses WHERE f_target = %s AND f_index = %s AND f_family = 2 z� DELETE FROM routes WHERE f_target = %s AND f_RTA_OIF = %s OR f_RTA_IIF = %s )Zload_netlink�get�execute�plchZ fetchmany�len)�schema�target�eventr � r ��/root/rpmbuild/BUILDROOT/imh-python39-modules-3.9.7-92.el8.x86_64/opt/imh-python/lib/python3.9/site-packages/pyroute2/ndb/objects/address.py�load_ifaddrmsg� s � � ��r �family� prefixlenr ZIFA_ADDRESSZ IFA_LOCALZ interfaces)Zf_targetZf_tflagsZf_indexr )�specs�classes� event_mapc sx e Zd ZdZeZeZdZe dd� �Z e dd� �Ze dd� �Zd d � Z � fdd�Zed d� �Ze dd� �Zdd� Z� ZS )�Addressr �addrc C sF |j r.|jj�d|j|jjjf |j d g�S |jj�d|j �S d S )Nz*SELECT count(*) FROM %s WHERE f_index = %sr zSELECT count(*) FROM %s)�chain�ndb�task_managerZdb_fetchone�tabler r )�cls�viewr r r �_count� s � ��zAddress._countc C sB |j r2|jjj}d||f }|j d |j d g}nd}g }||fS )Nz� WHERE main.f_target = %s AND main.f_index = %s r r � )r r r r )r r r �where�valuesr r r �_dump_where� s ��zAddress._dump_wherec c s<