うさラボ

お勉強と備忘録

pyATS shellの実行

pyATS shell

これまでpyATSのインストールとTestbedファイルの作成を実施してきました

pyATS インストール編 - うさラボ

pyATS Testbed作成 - うさラボ

shellコマンドを実行することでpythonの対話型インタプリンタが起動しpyATSの機能を利用することができます。

今回はshellコマンドを利用して簡単に、pyATSの機能を確認しましょう。

起動

さっそくpyats shellを実行します。

--testbedオプションで前回作成したTestbedを指定します。

(pyats-core) usalab!:nw_lab $ pyats shell --testbed vars/sample.yml 
Welcome to pyATS Interactive Shell
==================================
Python 3.7.5 (default, Nov 27 2019, 18:39:20) 
[Clang 11.0.0 (clang-1100.0.33.8)]

>>> from pyats.topology import loader
>>> testbed = loader.load('vars/sample.yml')
-------------------------------------------------------------------------------
>>>

pyats shellでインタプリンタが起動したら、TestbedファイルのDeviceに接続してみましょう。

接続

接続にはUniconと呼ばれる、接続用のライブラリを利用しています(pip install pyatsで同時にインストールされる)

テストベットに登録している全てのデバイスに接続するには下記コマンドを利用します

testbed.connect()

テストベット内の対象デバイスに接続する場合は下記コマンドを実施

testbed.devices['csr-1000v*ホスト名'].connect()

さっそく接続しましょう

>>> testbed.connect()

2020-12-06 22:01:14,198: %UNICON-INFO: +++ csr1000v logfile /tmp/csr1000v-default-20201206T220114198.log +++

2020-12-06 22:01:14,199: %UNICON-INFO: +++ Unicon plugin iosxe +++

Welcome to the DevNet Sandbox for CSR1000v and IOS XE

The following programmability features are already enabled:
   - NETCONF
   - RESTCONF

Thanks for stopping by.


2020-12-06 22:01:16,685: %UNICON-INFO: +++ connection to spawn: ssh -l developer 64.103.37.51 -p 8181, id: 4611986512 +++

2020-12-06 22:01:16,686: %UNICON-INFO: connection to csr1000v
Password: 

Welcome to the DevNet Sandbox for CSR1000v and IOS XE

The following programmability features are already enabled:
  - NETCONF
  - RESTCONF

Thanks for stopping by.


csr1000v#

2020-12-06 22:01:20,983: %UNICON-INFO: +++ initializing handle +++

2020-12-06 22:01:20,985: %UNICON-INFO: +++ csr1000v: executing command 'term length 0' +++
term length 0
csr1000v#

(~snip~)

2020-12-06 22:01:25,206: %UNICON-INFO: +++ csr1000v: configure +++
config term
Enter configuration commands, one per line.  End with CNTL/Z.
csr1000v(config)#no logging console
csr1000v(config)#line console 0
csr1000v(config-line)#exec-timeout 0
csr1000v(config-line)#end
csr1000v#
['\r\r\nWelcome to the DevNet Sandbox for CSR1000v and IOS XE\r\r\n\r\r\nThe following programmability features are already enabled:\r\r\n   - NETCONF\r\r\n   - RESTCONF\r\r\n\r\r\nThanks for stopping by.\r\r\n\rPassword: \r\n\r\nWelcome to the DevNet Sandbox for CSR1000v and IOS XE\r\n\r\nThe following programmability features are already enabled:\r\n  - NETCONF\r\n  - RESTCONF\r\n\r\nThanks for stopping by.\r\n\r\n\r\ncsr1000v#']
>>> 

確認(接続状態/showコマンド)

接続の確認にはis_connected()のメソッドを利用します。

>>> testbed.devices['csr1000v'].is_connected()
True

今回はpyATSのみしかインストールしていませんので、genieのメソッドは利用できません(parse/learnなど)。

続いて、deviceにコマンド送ることのできるexecuteメソッドを利用してshow versionを実施してみます。

>>> testbed.devices['csr1000v'].execute('show version')

2020-12-06 22:43:52,130: %UNICON-INFO: +++ csr1000v: executing command 'show version' +++
show version
Cisco IOS XE Software, Version 16.09.03
Cisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.9.3, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2019 by Cisco Systems, Inc.
Compiled Wed 20-Mar-19 07:56 by mcpre


Cisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc.
All rights reserved.  Certain components of Cisco IOS-XE software are
licensed under the GNU General Public License ("GPL") Version 2.0.  The
software code licensed under GPL Version 2.0 is free software that comes
with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such
GPL code under the terms of GPL Version 2.0.  For more details, see the
documentation or "License Notice" file accompanying the IOS-XE software,
or the applicable URL provided on the flyer accompanying the IOS-XE
software.


ROM: IOS-XE ROMMON

csr1000v uptime is 20 hours, 31 minutes
Uptime for this control processor is 20 hours, 32 minutes
System returned to ROM by reload
System image file is "bootflash:packages.conf"
Last reload reason: reload



This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.

License Level: ax
License Type: Default. No valid license found.
Next reload license Level: ax


Smart Licensing Status: Smart Licensing is DISABLED

cisco CSR1000V (VXE) processor (revision VXE) with 2392579K/3075K bytes of memory.
Processor board ID 939QG0VY6CG
3 Gigabit Ethernet interfaces
32768K bytes of non-volatile configuration memory.
8113280K bytes of physical memory.
7774207K bytes of virtual hard disk at bootflash:.
0K bytes of WebUI ODM Files at webui:.

Configuration register is 0x2102

csr1000v#
'Cisco IOS XE Software, Version 16.09.03\r\nCisco IOS Software [Fuji], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.9.3, RELEASE SOFTWARE (fc2)\r\nTechnical Support: http://www.cisco.com/techsupport\r\nCopyright (c) 1986-2019 by Cisco Systems, Inc.\r\nCompiled Wed 20-Mar-19 07:56 by mcpre\r\n\r\n\r\nCisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc.\r\nAll rights reserved.  Certain components of Cisco IOS-XE software are\r\nlicensed under the GNU General Public License ("GPL") Version 2.0.  The\r\nsoftware code licensed under GPL Version 2.0 is free software that comes\r\nwith ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such\r\nGPL code under the terms of GPL Version 2.0.  For more details, see the\r\ndocumentation or "License Notice" file accompanying the IOS-XE software,\r\nor the applicable URL provided on the flyer accompanying the IOS-XE\r\nsoftware.\r\n\r\n\r\nROM: IOS-XE ROMMON\r\n\r\ncsr1000v uptime is 20 hours, 31 minutes\r\nUptime for this control processor is 20 hours, 32 minutes\r\nSystem returned to ROM by reload\r\nSystem image file is "bootflash:packages.conf"\r\nLast reload reason: reload\r\n\r\n\r\n\r\nThis product contains cryptographic features and is subject to United\r\nStates and local country laws governing import, export, transfer and\r\nuse. Delivery of Cisco cryptographic products does not imply\r\nthird-party authority to import, export, distribute or use encryption.\r\nImporters, exporters, distributors and users are responsible for\r\ncompliance with U.S. and local country laws. By using this product you\r\nagree to comply with applicable laws and regulations. If you are unable\r\nto comply with U.S. and local laws, return this product immediately.\r\n\r\nA summary of U.S. laws governing Cisco cryptographic products may be found at:\r\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\r\n\r\nIf you require further assistance please contact us by sending email to\r\nexport@cisco.com.\r\n\r\nLicense Level: ax\r\nLicense Type: Default. No valid license found.\r\nNext reload license Level: ax\r\n\r\n\r\nSmart Licensing Status: Smart Licensing is DISABLED\r\n\r\ncisco CSR1000V (VXE) processor (revision VXE) with 2392579K/3075K bytes of memory.\r\nProcessor board ID 939QG0VY6CG\r\n3 Gigabit Ethernet interfaces\r\n32768K bytes of non-volatile configuration memory.\r\n8113280K bytes of physical memory.\r\n7774207K bytes of virtual hard disk at bootflash:.\r\n0K bytes of WebUI ODM Files at webui:.\r\n\r\nConfiguration register is 0x2102'
>>> 

このようにexecuteメソッドを利用することで任意のをコマンドを送信することが可能です。

設定変更

設定変更はconfigureメソッドを利用することで可能です。

static routeの追加作業を実現してみましょう

executeで事前事後の確認も合わせて実施します。

// 設定確認
>>> testbed.devices['csr1000v'].execute('show run | inc route')

2020-12-06 23:03:02,226: %UNICON-INFO: +++ csr1000v: executing command 'show run | inc route' +++
show run | inc route
ip route 0.0.0.0 0.0.0.0 GigabitEthernet1 10.10.20.254
ip route 8.8.8.8 255.255.255.255 Null0
csr1000v#
'ip route 0.0.0.0 0.0.0.0 GigabitEthernet1 10.10.20.254\r\nip route 8.8.8.8 255.255.255.255 Null0'
>>> 

// 設定追加
>>> testbed.devices['csr1000v'].configure('ip route 1.1.1.1 255.255.255.255 10.10.20.254')

2020-12-06 23:06:42,714: %UNICON-INFO: +++ csr1000v: configure +++
config term
Enter configuration commands, one per line.  End with CNTL/Z.
csr1000v(config)#ip route 1.1.1.1 255.255.255.255 10.10.20.254
csr1000v(config)#end
csr1000v#
'ip route 1.1.1.1 255.255.255.255 10.10.20.254\r\n'
>>> 

// 設定確認
>>> testbed.devices['csr1000v'].execute('show run | inc route')

2020-12-06 23:07:14,288: %UNICON-INFO: +++ csr1000v: executing command 'show run | inc route' +++
show run | inc route
ip route 0.0.0.0 0.0.0.0 GigabitEthernet1 10.10.20.254
ip route 1.1.1.1 255.255.255.255 10.10.20.254
ip route 8.8.8.8 255.255.255.255 Null0
csr1000v#
'ip route 0.0.0.0 0.0.0.0 GigabitEthernet1 10.10.20.254\r\nip route 1.1.1.1 255.255.255.255 10.10.20.254\r\nip route 8.8.8.8 255.255.255.255 Null0'
>>> 

追加後の確認からも想定通り追加されたことが確認できました。

下記コマンドは自動で入力してくれます。 * configure termina * end

他にも、設定追加で複数行送信したい場合はリストで定義すれば可能です。

今回、pyatsコアモジュールでshellコマンドで実施し、簡単な接続/設定確認/設定変更を実施してみました。

genieがあるとさらにparse/learn/api/triggerなど強力な機能も利用できるようになります。

次回は、pyatsで簡単なJobを作成してみます。