1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
import logging
import sys
from ncclient import manager
# Global constants
DEVICE_HOST = 'localhost'
DEVICE_PORT = 830
DEVICE_USER = 'admin'
DEVICE_PASS = 'admin'
EDIT_CONFIG_PAYLOAD = """
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:alu="urn:ietf:params:xml:ns:netconf:base:1.0">
<configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
<system>
<security>
<snmp>
<community alu:operation="replace">
<community-string>Sample Community</community-string>
<access-permissions>r</access-permissions>
<version>v2c</version>
</community>
</snmp>
</security>
</system>
</configure>
</config>
"""
def create_session(host, port, username, password):
"""Creates and returns an ncclient manager session."""
return manager.connect(
host=host,
port=port,
username=username,
password=password,
hostkey_verify=False,
device_params={'name': 'sros'}
)
def edit_config(m, config_payload):
"""Edits the configuration with the given payload."""
m.edit_config(target='candidate', config=config_payload)
def commit_changes(m, comment=''):
"""Commits changes with an optional comment."""
response = m.commit(comment=comment)
logging.info(response)
def main():
LOG_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=LOG_FORMAT)
try:
with create_session(DEVICE_HOST, DEVICE_PORT, DEVICE_USER, DEVICE_PASS) as m:
# Lock the configuration
m.lock(target='candidate')
# Edit the configuration
edit_config(m, EDIT_CONFIG_PAYLOAD)
# Commit the configuration
commit_changes(m, comment='A sample commit comment goes here')
# Unlock the configuration
m.unlock(target='candidate')
except Exception as e:
logging.error(f"Encountered an error: {e}")
if __name__ == '__main__':
main()
|