何がやりたい?
NETCONFでルータの状態を参照したり、設定を変更したりするとしても、誰でも簡単に実行できるようにしたいと思います。
ここで試している、ncc.py
を使えば、ある設定をテンプレートにして、必要なパラメータを引数として指定することで、自分にとって必要なオリジナルコマンドを作成するような感じで使えるんじゃないかなと思います。
参照先: https://github.com/CiscoDevNet/ncc
ncc.pyのインストール
git clone https://github.com/CiscoDevNet/ncc.git
cd ncc
sudo pip install -r requirements.txt
ちなみに、requirements.txt
の内容を確認しますと、下記のパッケージがインストールされるようです。また、ncc.py
は、ncclient
のライブラリを利用しているようです。
# cat requirements.txt
beautifulsoup4==4.6.0
Jinja2>=2.8
MarkupSafe>=0.23
lxml>=3.6.0
ncclient>=0.5.2
netmiko>=1.3.0
pyang>=1.6
requests>=2.10.0
GitPython>=2.1.3
python ncc.py --host=192.239.42.222 --capabilities
ncc.py を使って、capabilitiyの確認
python ncc.py --host=172.16.62.152 --capabilities
この場合、デフォルトのusername/password
が、cisco/cisco
となりますので、正確には下記のコマンドとなります。また、デフォルトのポート番号は、830
です。
python ncc.py --host=172.16.62.152 --port 830 -u cisco -p cisco --capabilities
ncc$ python ncc.py --host=172.16.62.152 --capabilities
IETF NETCONF Capabilities:
urn:ietf:params:netconf:base:1.0
urn:ietf:params:netconf:base:1.1
urn:ietf:params:netconf:capability:interleave:1.0
urn:ietf:params:netconf:capability:notification:1.0
urn:ietf:params:netconf:capability:rollback-on-error:1.0
urn:ietf:params:netconf:capability:validate:1.0
urn:ietf:params:netconf:capability:validate:1.1
urn:ietf:params:netconf:capability:with-defaults:1.0?basic-mode=explicit&also-supported=report-all-tagged
urn:ietf:params:netconf:capability:writable-running:1.0
urn:ietf:params:netconf:capability:xpath:1.0
urn:ietf:params:netconf:capability:yang-library:1.0?revision=2016-06-21&module-set-id=51d8cd742edef02cb7066f31ef95001d
IETF Models:
cisco-ospf (urn:ietf:params:xml:ns:yang:cisco-ospf)
cisco-policy (urn:ietf:params:xml:ns:yang:cisco-policy)
(snip)
ちなみに、ssh
コマンドを使って、capabilitiyを確認する場合は、下記のようなコマンドになります。
ssh cisco@172.16.62.152 -p 830 -s netconf
ncc$ python ncc.py --host=172.16.62.152 --capabilities
IETF NETCONF Capabilities:
urn:ietf:params:netconf:base:1.0
urn:ietf:params:netconf:base:1.1
urn:ietf:params:netconf:capability:interleave:1.0
urn:ietf:params:netconf:capability:notification:1.0
urn:ietf:params:netconf:capability:rollback-on-error:1.0
urn:ietf:params:netconf:capability:validate:1.0
urn:ietf:params:netconf:capability:validate:1.1
urn:ietf:params:netconf:capability:with-defaults:1.0?basic-mode=explicit&also-supported=report-all-tagged
urn:ietf:params:netconf:capability:writable-running:1.0
urn:ietf:params:netconf:capability:xpath:1.0
urn:ietf:params:netconf:capability:yang-library:1.0?revision=2016-06-21&module-set-id=51d8cd742edef02cb7066f31ef95001d
IETF Models:
cisco-ospf (urn:ietf:params:xml:ns:yang:cisco-ospf)
cisco-policy (urn:ietf:params:xml:ns:yang:cisco-policy)
(snip)
正規表現でサポートされるモデルを確認
下記のsnmp
部分は、確認したい文字列に変更すると良さそう。
python ncc.py --host=172.16.62.152 --is-supported '(?i)snmp'
ncc$ python ncc.py --host=172.16.62.152 --is-supported '(?i)snmp'
SNMPv2-MIB
SNMP-FRAMEWORK-MIB
Cisco-IOS-XE-snmp
SNMPv2-TC
SNMP-PROXY-MIB
SNMP-TARGET-MIB
snmp
部分をinterface
に変えてみると、
python ncc.py --host=172.16.62.152 --is-supported '(?i)interface'
ncc$ python ncc.py --host=172.16.62.152 --is-supported '(?i)interface'
Cisco-IOS-XE-interface-common
ietf-interfaces
cisco-xe-openconfig-interfaces-ext
cisco-xe-openconfig-interfaces-deviation
ietf-interfaces-ext
openconfig-interfaces
ncc.py を使って、configの確認
python ncc.py --host=172.16.62.152 --get-running
$ python ncc.py --host=172.16.62.152 --get-running
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<version>16.7</version>
<boot-start-marker/>
<boot-end-marker/>
<service>
(snip)
Snippetsを使ってみる(まずは試しに)
Snippets are a way to pre-define edit-config messages or complex filters that you want to use from the command line.
スニペットは、コマンドラインから使用したいedit-configメッセージや複雑なフィルタをあらかじめ定義する方法です。
らしいので、「どうやって定義しているんだろう」というところを試して見ます。
ietf-intf
python ncc.py --host=172.16.62.152 --get-oper --named-filter ietf-intf
ncc$ python ncc.py --host=172.16.62.152 --get-oper --named-filter ietf-intf
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name>GigabitEthernet1</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<enabled>true</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
<interface>
<name>GigabitEthernet2</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<enabled>false</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
<interface>
<name>GigabitEthernet3</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<enabled>false</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
</interfaces>
</data>
ここでのietf-intfというのは、.tmpl
ファイルで定義されるファイルのことなので、実際の中身を確認して見ると、
./snippets/filters/ietf-intf.tmpl
ncc$ cat ./snippets/filters/ietf-intf.tmpl
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
ちなみに、YangExplorerで同様の値をする場合を確認すると、payload
は下記の通りでしたので、.tmpl
ファイルの中で指定している内容と同じであることが確認できました。
payload = """
<filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
</filter>
"""
ietf-intfs-state
次に、インタフェースの状態(show interface)を確認してみます。
python ncc.py --host=172.16.62.152 --get-oper --named-filter ietf-intfs-state
ncc$ python ncc.py --host=172.16.62.152 --get-oper --named-filter ietf-intfs-state
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name>GigabitEthernet1</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<admin-status>up</admin-status>
<oper-status>up</oper-status>
<last-change>2018-03-02T03:11:05.000705+00:00</last-change>
<if-index>1</if-index>
<phys-address>00:0c:29:55:d7:ca</phys-address>
<speed>1024000000</speed>
<statistics>
<discontinuity-time>2018-03-01T16:21:05.000993+00:00</discontinuity-time>
<in-octets>836083</in-octets>
<in-unicast-pkts>7678</in-unicast-pkts>
<in-broadcast-pkts>0</in-broadcast-pkts>
<in-multicast-pkts>0</in-multicast-pkts>
<in-discards>0</in-discards>
<in-errors>0</in-errors>
<in-unknown-protos>0</in-unknown-protos>
<out-octets>3868192</out-octets>
<out-unicast-pkts>7829</out-unicast-pkts>
<out-broadcast-pkts>0</out-broadcast-pkts>
<out-multicast-pkts>0</out-multicast-pkts>
<out-discards>0</out-discards>
<out-errors>0</out-errors>
</statistics>
</interface>
<interface>
<name>GigabitEthernet2</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<admin-status>down</admin-status>
<oper-status>down</oper-status>
<last-change>2018-03-01T16:23:06.000788+00:00</last-change>
<if-index>2</if-index>
<phys-address>00:0c:29:55:d7:d4</phys-address>
<speed>1024000000</speed>
<statistics>
<discontinuity-time>2018-03-01T16:21:05.000993+00:00</discontinuity-time>
<in-octets>0</in-octets>
<in-unicast-pkts>0</in-unicast-pkts>
<in-broadcast-pkts>0</in-broadcast-pkts>
<in-multicast-pkts>0</in-multicast-pkts>
<in-discards>0</in-discards>
<in-errors>0</in-errors>
<in-unknown-protos>0</in-unknown-protos>
<out-octets>0</out-octets>
<out-unicast-pkts>0</out-unicast-pkts>
<out-broadcast-pkts>0</out-broadcast-pkts>
<out-multicast-pkts>0</out-multicast-pkts>
<out-discards>0</out-discards>
<out-errors>0</out-errors>
</statistics>
</interface>
<interface>
<name>GigabitEthernet3</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<admin-status>down</admin-status>
<oper-status>down</oper-status>
<last-change>2018-03-01T16:42:07.000536+00:00</last-change>
<if-index>3</if-index>
<phys-address>00:0c:29:55:d7:de</phys-address>
<speed>1024000000</speed>
<statistics>
<discontinuity-time>2018-03-01T16:21:05.000992+00:00</discontinuity-time>
<in-octets>0</in-octets>
<in-unicast-pkts>0</in-unicast-pkts>
<in-broadcast-pkts>0</in-broadcast-pkts>
<in-multicast-pkts>0</in-multicast-pkts>
<in-discards>0</in-discards>
<in-errors>0</in-errors>
<in-unknown-protos>0</in-unknown-protos>
<out-octets>0</out-octets>
<out-unicast-pkts>0</out-unicast-pkts>
<out-broadcast-pkts>0</out-broadcast-pkts>
<out-multicast-pkts>0</out-multicast-pkts>
<out-discards>0</out-discards>
<out-errors>0</out-errors>
</statistics>
</interface>
<interface>
<name>Control Plane</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:other</type>
<admin-status>up</admin-status>
<oper-status>up</oper-status>
<last-change>2018-03-01T16:23:04.000351+00:00</last-change>
<if-index>0</if-index>
<phys-address>00:00:00:00:00:00</phys-address>
<speed>1650065408</speed>
<statistics>
<discontinuity-time>2018-03-01T16:21:05.000992+00:00</discontinuity-time>
<in-octets>0</in-octets>
<in-unicast-pkts>0</in-unicast-pkts>
<in-broadcast-pkts>0</in-broadcast-pkts>
<in-multicast-pkts>0</in-multicast-pkts>
<in-discards>0</in-discards>
<in-errors>0</in-errors>
<in-unknown-protos>0</in-unknown-protos>
<out-octets>0</out-octets>
<out-unicast-pkts>0</out-unicast-pkts>
<out-broadcast-pkts>0</out-broadcast-pkts>
<out-multicast-pkts>0</out-multicast-pkts>
<out-discards>0</out-discards>
<out-errors>0</out-errors>
</statistics>
</interface>
</interfaces-state>
</data>
同様にietf-intfs-state.tmpl
の内容を確認してみますと、
ncc$ cat ./snippets/filters/ietf-intfs-state.tmpl
<interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
こちらもYangExplorer
で確認してみますと、やはり同じ内容であることが確認できます。
payload = """
<filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
</filter>
"""
なんとなく、payload
の中で指定するパスを.tmpl
ファイルの中で定義しておくことができそう。ということが確認できました。
Snippetsは何処にある
先ほど試した、ietf-intf
とietf-intfs-state
が何処にあるのかを確認してみます。
python ncc.py --list-filters
ncc$ python ncc.py --list-filters
Named filters:
acl-666
acls-all
fivesecondcpu
ietf-intf
ietf-intfs-state
intf-brief-all
intf-brief :{ "INTF_NAME" : ""
}
intf-stats-limited :{ "INTF_NAME" : ""
}
intf-stats :{ "INTF_NAME" : ""
}
lldp-all
mdtoper
oc-bgp-neighbors :{ "NEIGHBOR" : ""
}
oc-bgp
oc-intf-named :{ "INTF_NAME" : ""
}
oc-intf
oc-subintf-named-and-indexed :{ "INTF_NAME" : "","SUBINTF_INDEX" : ""
}
oc-subintf-named :{ "INTF_NAME" : ""
}
qos-oper-all
qos-oper-intf :{ "INTF_NAME" : ""
}
telem-all
vrrp-ipv4-all
xe-cpu-usage
xe-memory-statistics
xe-native-intf
xe-native-ipv4-acl
xe-native-ipv6-acl
xe-native
xr-value-detailed-specific
xr-value-detailed-temp
xr-value-detailed
これは、デフォルトで./snippets/filters
配下を参照しています。
次に、./snippets/editconfigs
配下を参照するコマンドを確認してみます。
python ncc.py --list-templates
ncc$ python ncc.py --list-templates
Edit-config templates:
add_neighbor :{ "DESCRIPTION" : "","NEIGHBOR_ADDR" : "","REMOTE_AS" : ""
}
add_static_route_default
class_dscp31_cr
class_dscp31_del
class_lots_of_dscp
classmap_delete_einarnn_all
classmap_einarnn_3
classmap_einarnn_4
classmap_einarnn_5
classmap_einarnn_6
del_neighbor :{ "NEIGHBOR_ADDR" : ""
}
del_static_route_default
delete_autoneg
delete_ebgp_multihop_enabled
delete_l2vpn
empty_l2vpn
foo1_add_static_route_default
foo2_add_static_route_default
ietf-cr-loopback :{ "INTF_NAME" : ""
}
ietf-intf-add-ipv4 :{ "INTF_NAME" : ""
}
l2vpn_bdgroups_cr
l2vpn_bdgroups_del
lldp-basic-and-del
lldp-basic
lldp-del
nacm-01-cr-other
nacm-02-del-other
nacm-03-add-group
nacm-04-rm-group
no_shut
oc-set-descr-broken
oc-set-descr
oc_basic
policy_test_cr
policy_test_del
restconf-enable :{ "BRIDGE_IP" : "","RC_HTTPS_PORT" : "","RC_HTTP_PORT" : ""
}
set_autoneg_override
set_autoneg_true
set_ebgp_multihop_enabled_false
set_ebgp_multihop_enabled_true
shut
simple_ospf
telem-cr
telem-del-ipv6-fails
telem-del-ipv6-works
xe-delete-ace
xe-enable-polling
xe-merge-ace
xxx
xxx_del
これらの結果を見ていますと、各ディレクトリ配下のファイル一覧を表示していますが、引数というか、パラメータというか、コマンドを実行する際に何らかの値が必要となる場合はname :{"value" , "value"} 括弧がついています。
特に引数を必要としない場合や、決め打ちで固定値を.tmpl
に書き込んでいる場合は括弧は表示されません。
参照するSnippets
をデフォルトのディレクトリから変更したい場合は、下記のようなオプションを指定することで変更できます。
python ncc.py --snippets ./snippets-xe --list-filters
ncc$ python ncc.py --snippets ./snippets-xe --list-templates
Edit-config templates:
00-oper-data-enable-16.4
00-oper-data-enable
00-snmp-config
01-oper-data-disable
01-snmp-config-disable
native-create-acl :{ "ACL_NAME" : ""
}
native-create-class-map :{ "C_NAME" : ""
}
native-create-policy-map :{ "C_NAME" : "","P_NAME" : ""
}
native-create-vlan :{ "VLAN" : ""
}
native-delete-acl :{ "ACL_NAME" : ""
}
native-delete-class-map :{ "C_NAME" : ""
}
native-delete-policy-map :{ "P_NAME" : ""
}
native-delete-vlan :{ "VLAN" : ""
}
native-intf-add-acl :{ "ACL" : "","INTF_NAME" : ""
}
native-intf-add-qos :{ "INTF_NAME" : "","P_NAME" : ""
}
native-intf-delete-acl :{ "ACL" : "","INTF_NAME" : ""
}
native-intf-delete-qos :{ "INTF_NAME" : "","P_NAME" : ""
}
native-intf-no-shut :{ "INTF_NAME" : ""
}
native-intf-no-switchport :{ "INTF_NAME" : ""
}
native-intf-shutdown :{ "INTF_NAME" : ""
}
native-intf-switchport :{ "INTF_NAME" : ""
}
native-intf-vlan-change :{ "INTF_NAME" : "","VLAN" : ""
}
native-router-ospf-add-network :{ "MASK" : "","NETWORK" : "","ROUTER_ID" : ""
}
native-router-ospf-delete-network :{ "MASK" : "","NETWORK" : "","ROUTER_ID" : ""
}
openconfig-intf-ip-address :{ "INTF_NAME" : "","IP_ADDR" : ""
}
python ncc.py --snippets ./snippets-xe --list-templates
ncc$ python ncc.py --snippets ./snippets-xe --list-templates
Edit-config templates:
00-oper-data-enable-16.4
00-oper-data-enable
00-snmp-config
01-oper-data-disable
01-snmp-config-disable
native-create-acl :{ "ACL_NAME" : ""
}
native-create-class-map :{ "C_NAME" : ""
}
native-create-policy-map :{ "C_NAME" : "","P_NAME" : ""
}
native-create-vlan :{ "VLAN" : ""
}
native-delete-acl :{ "ACL_NAME" : ""
}
native-delete-class-map :{ "C_NAME" : ""
}
native-delete-policy-map :{ "P_NAME" : ""
}
native-delete-vlan :{ "VLAN" : ""
}
native-intf-add-acl :{ "ACL" : "","INTF_NAME" : ""
}
native-intf-add-qos :{ "INTF_NAME" : "","P_NAME" : ""
}
native-intf-delete-acl :{ "ACL" : "","INTF_NAME" : ""
}
native-intf-delete-qos :{ "INTF_NAME" : "","P_NAME" : ""
}
native-intf-no-shut :{ "INTF_NAME" : ""
}
native-intf-no-switchport :{ "INTF_NAME" : ""
}
native-intf-shutdown :{ "INTF_NAME" : ""
}
native-intf-switchport :{ "INTF_NAME" : ""
}
native-intf-vlan-change :{ "INTF_NAME" : "","VLAN" : ""
}
native-router-ospf-add-network :{ "MASK" : "","NETWORK" : "","ROUTER_ID" : ""
}
native-router-ospf-delete-network :{ "MASK" : "","NETWORK" : "","ROUTER_ID" : ""
}
openconfig-intf-ip-address :{ "INTF_NAME" : "","IP_ADDR" : ""
}
Snippetsを使ってみる(XE-native)
まずは、引数を指定しないとどうなるかを確認します。
python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --get-oper --named-filter ietf-intf-named
ncc$ python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --get-oper --named-filter ietf-intf-named
Undefined variable 'INTF_NAME' is undefined. Use --params to specify json dict
--params
を使って、json dict
っぽい感じで指定する必要があると、、、
それでは、実際に試してみます。
python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --get-oper --named-filter ietf-intf-named --params '{"INTF_NAME" : "GigabitEthernet3"}'
ncc$ python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --get-oper --named-filter ietf-intf-named --params '{"INTF_NAME" : "GigabitEthernet3"}'
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">GigabitEthernet3</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<enabled>true</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
</interfaces>
</data>
折角、インタフェースの設定を確認しているので、shutdown
してみましょう。
python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --do-edits native-intf-shutdown --params '{"INTF_NAME" : "3"}'
cc$ python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --do-edits native-intf-shutdown --params '{"INTF_NAME" : "3"}'
もう一度、インタフェースの設定を確認します。
python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --get-oper --named-filter ietf-intf-named --params '{"INTF_NAME" : "GigabitEthernet3"}'
ncc$ python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --get-oper --named-filter ietf-intf-named --params '{"INTF_NAME" : "GigabitEthernet3"}'
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">GigabitEthernet3</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<enabled>false</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
</interfaces>
</data>
false
になっている、、、ので、今度は、no shutdown
してみます。
python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --do-edits native-intf-no-shut --params '{"INTF_NAME" : "3"}'
ncc$ python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --do-edits native-intf-no-shut --params '{"INTF_NAME" : "3"}'
最後に、もう一度、設定を確認してみます。
python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --get-oper --named-filter ietf-intf-named --params '{"INTF_NAME" : "GigabitEthernet3"}'
ncc$ python ncc.py --host=172.16.62.152 --snippets ./snippets-xe --get-oper --named-filter ietf-intf-named --params '{"INTF_NAME" : "GigabitEthernet3"}'
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">GigabitEthernet3</name>
<type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
<enabled>true</enabled>
<ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
<ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"/>
</interface>
</interfaces>
</data>
なんで、設定を確認するときは、GigabitEthernet3
と書いて、shut/no shutのときは、3
なの?
というのは、.tmpl
の中での書き方が違うみたいです。。。ですので、ちょっと変更してみるのも良いかも。
この部分については、以降の内容を参照してください。
shutdownを削除(no shutdown)するには
shutdown
の様に設定を追加するだけなら、まだ、すんなりと動作するのですが、no shutdown
に限らず、設定を削除しようとすると上手く行かない。何故かというと、ncclientのedit_config
を使う代わりに、delete_config
を使えば良いのかな、、と考えるのですが、これだと削除されません。
ここで、.tmpl
の内容を参照しますと、nc:operation="remove"
と指定することで削除することができるようです。
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
<interface>
<name>{{INTF_NAME}}</name>
</interface>
</interfaces>
<config>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<GigabitEthernet>
<name>{{INTF_NAME}}</name>
<shutdown/>
</GigabitEthernet>
</interface>
</native>
</config>
<config>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<GigabitEthernet>
<name>{{INTF_NAME}}</name>
<shutdown nc:operation="remove"></shutdown>
</GigabitEthernet>
</interface>
</native>
</config>
YangExplorerを使って、削除を確認
今度は、YangExplorerを使って、同じくインタフェースのshuw/no shutをcreate
とdelete
を指定した場合の内容を確認してみます。
結果的には、remove
ではなく、delete
でも削除できています。
また、YangExplorerでも、edit_config
を使うサンプルコンフィグを出力しています。
payload = """
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<GigabitEthernet>
<name>3</name>
<shutdown xc:operation="create"/>
</GigabitEthernet>
</interface>
</native>
</config>
"""
payload = """
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<GigabitEthernet>
<name>3</name>
<shutdown xc:operation="delete"/>
</GigabitEthernet>
</interface>
</native>
</config>
"""
最後に
YangExplorer
を使ったり、実際にルータにコンフィグを設定して、その差分を確認したりすれば、自分でSnippets
を書くのも、そんなに難しくない気もします。