diff options
-rw-r--r-- | docs/automation/index.rst | 2 | ||||
-rw-r--r-- | docs/automation/vyos-napalm.rst | 144 | ||||
-rw-r--r-- | docs/automation/vyos-netmiko.rst | 72 |
3 files changed, 218 insertions, 0 deletions
diff --git a/docs/automation/index.rst b/docs/automation/index.rst index 044505b9..f9049c48 100644 --- a/docs/automation/index.rst +++ b/docs/automation/index.rst @@ -12,5 +12,7 @@ VyOS Automation vyos-api vyos-ansible + vyos-napalm + vyos-netmiko command-scripting cloud-init diff --git a/docs/automation/vyos-napalm.rst b/docs/automation/vyos-napalm.rst new file mode 100644 index 00000000..5ff7c01f --- /dev/null +++ b/docs/automation/vyos-napalm.rst @@ -0,0 +1,144 @@ +:lastproofread: 2021-06-28 + +.. _vyos-napalm: + +Napalm +====== + +VyOS supports some napalm_ functions for configuration and op-mode. +It requires more tests. + +Install ``napalm-vyos`` module + +.. code-block:: none + + apt install python3-pip + pip3 install napalm + pip3 install napalm-vyos + + +Op-mode +------- + +.. code-block:: none + + #!/usr/bin/env python3 + + import json + from napalm import get_network_driver + + driver = get_network_driver('vyos') + + vyos_router = driver( + hostname="192.0.2.1", + username="vyos", + password="vyospass", + optional_args={"port": 22}, + ) + + vyos_router.open() + output = vyos_router.get_facts() + print(json.dumps(output, indent=4)) + + output = vyos_router.get_arp_table() + print(json.dumps(output, indent=4)) + + vyos_router.close() + +Output op-mode + +.. code-block:: none + + $ ./vyos-napalm.py + { + "uptime": 7185, + "vendor": "VyOS", + "os_version": "1.3.0-rc5", + "serial_number": "", + "model": "Standard PC (Q35 + ICH9, 2009)", + "hostname": "r4-1.3", + "fqdn": "vyos.local", + "interface_list": [ + "eth0", + "eth1", + "eth2", + "lo", + "vtun10" + ] + } + [ + { + "interface": "eth1", + "mac": "52:54:00:b2:38:2c", + "ip": "192.0.2.2", + "age": 0.0 + }, + { + "interface": "eth0", + "mac": "52:54:00:a2:b9:5b", + "ip": "203.0.113.11", + "age": 0.0 + } + ] + +Configuration +------------- + +We need 2 files, commands.conf and script itself. + +Content of commands.conf + +.. code-block:: none + + set service ssh disable-host-validation + set service ssh port '2222' + set system name-server '192.0.2.8' + set system name-server '203.0.113.8' + set interfaces ethernet eth1 description 'FOO' + +Script vyos-napalm.py + +.. code-block:: none + + #!/usr/bin/env python3 + + from napalm import get_network_driver + + driver = get_network_driver('vyos') + + vyos_router = driver( + hostname="192.0.2.1", + username="vyos", + password="vyospass", + optional_args={"port": 22}, + ) + + vyos_router.open() + vyos_router.load_merge_candidate(filename='commands.conf') + diffs = vyos_router.compare_config() + + if bool(diffs) == True: + print(diffs) + vyos_router.commit_config() + else: + print('No configuration changes to commit') + vyos_router.discard_config() + + vyos_router.close() + +Output + +.. code-block:: none + + $./vyos-napalm.py + [edit interfaces ethernet eth1] + +description FOO + [edit service ssh] + +disable-host-validation + +port 2222 + [edit system] + +name-server 192.0.2.8 + +name-server 203.0.113.8 + [edit] + +.. _napalm: https://napalm.readthedocs.io/en/latest/base.html
\ No newline at end of file diff --git a/docs/automation/vyos-netmiko.rst b/docs/automation/vyos-netmiko.rst new file mode 100644 index 00000000..3627e5c3 --- /dev/null +++ b/docs/automation/vyos-netmiko.rst @@ -0,0 +1,72 @@ +:lastproofread: 2021-06-28 + +.. _vyos-netmiko: + +Netmiko +======= + +VyOS supports configuration via netmiko_. +It requires to install ``python3-netmiko`` module. + +Example +------- + +.. code-block:: none + + #!/usr/bin/env python3 + + from netmiko import ConnectHandler + + vyos_router = { + "device_type": "vyos", + "host": "192.0.2.1", + "username": "vyos", + "password": "vyospass", + "port": 22, + } + + net_connect = ConnectHandler(**vyos_router) + + config_commands = [ + 'set interfaces ethernet eth0 description WAN', + 'set interfaces ethernet eth1 description LAN', + ] + + # set congiguration + output = net_connect.send_config_set(config_commands, exit_config_mode=False) + print(output) + + # commit configuration + output = net_connect.commit() + print(output) + + # op-mode commands + output = net_connect.send_command("run show interfaces") + print(output) + +Output + +.. code-block:: none + + $ ./vyos-netmiko.py + configure + set interfaces ethernet eth0 description WAN + [edit] + vyos@r4-1.3# set interfaces ethernet eth1 description LAN + [edit] + vyos@r4-1.3# + commit + [edit] + vyos@r4-1.3# + Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down + Interface IP Address S/L Description + --------- ---------- --- ----------- + eth0 203.0.113.1/24 u/u WAN + eth1 192.0.2.1/30 u/u LAN + eth2 - u/u + lo 127.0.0.1/8 u/u + ::1/128 + vtun10 10.10.0.1/24 u/u + [edit] + +.. _netmiko: https://github.com/ktbyers/netmiko
\ No newline at end of file |