LoginSignup
0
0

More than 5 years have passed since last update.

ncc.py を使ってスクリプトを書かずにNETCONFを利用する

Posted at

何がやりたい?

NETCONFでルータの状態を参照したり、設定を変更したりするとしても、誰でも簡単に実行できるようにしたいと思います。

ここで試している、ncc.pyを使えば、ある設定をテンプレートにして、必要なパラメータを引数として指定することで、自分にとって必要なオリジナルコマンドを作成するような感じで使えるんじゃないかなと思います。

参照先: https://github.com/CiscoDevNet/ncc

ncc.pyのインストール

command
git clone https://github.com/CiscoDevNet/ncc.git
command
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の確認

command
python ncc.py --host=172.16.62.152 --capabilities

この場合、デフォルトのusername/passwordが、cisco/ciscoとなりますので、正確には下記のコマンドとなります。また、デフォルトのポート番号は、830です。

command
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を確認する場合は、下記のようなコマンドになります。

command
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部分は、確認したい文字列に変更すると良さそう。

command
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に変えてみると、

command
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の確認

command
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. 
google翻訳によると
スニペットは、コマンドラインから使用したいedit-configメッセージや複雑なフィルタをあらかじめ定義する方法です。

らしいので、「どうやって定義しているんだろう」というところを試して見ます。

ietf-intf

command
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

ietf-intf.tmpl
ncc$ cat ./snippets/filters/ietf-intf.tmpl
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>

ちなみに、YangExplorerで同様の値をする場合を確認すると、payloadは下記の通りでしたので、.tmplファイルの中で指定している内容と同じであることが確認できました。

YangExplorer
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)を確認してみます。

command
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の内容を確認してみますと、

ietf-intfs-state.tmpl
ncc$ cat ./snippets/filters/ietf-intfs-state.tmpl
<interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>

こちらもYangExplorerで確認してみますと、やはり同じ内容であることが確認できます。

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-intfietf-intfs-stateが何処にあるのかを確認してみます。

command
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配下を参照するコマンドを確認してみます。

command
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をデフォルトのディレクトリから変更したい場合は、下記のようなオプションを指定することで変更できます。

command
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" : ""
}
command
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)

まずは、引数を指定しないとどうなるかを確認します。

command
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っぽい感じで指定する必要があると、、、

それでは、実際に試してみます。

command
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してみましょう。

command
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"}'

もう一度、インタフェースの設定を確認します。

command
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してみます。

command
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"}'

最後に、もう一度、設定を確認してみます。

command
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に限らず、設定を削除しようとすると上手く行かない。何故かというと、ncclientedit_configを使う代わりに、delete_configを使えば良いのかな、、と考えるのですが、これだと削除されません。

ここで、.tmplの内容を参照しますと、nc:operation="remove"と指定することで削除することができるようです。

ietf-intf-named.tmpl
<interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
  <interface>
    <name>{{INTF_NAME}}</name>
  </interface>
</interfaces>
native-intf-shutdown.tmpl
<config>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
      <interface>
        <GigabitEthernet>
          <name>{{INTF_NAME}}</name>
          <shutdown/>
        </GigabitEthernet>
      </interface>
    </native>
</config>
native-intf-no-shut.tmpl
<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をcreatedeleteを指定した場合の内容を確認してみます。

結果的には、removeではなく、deleteでも削除できています。
また、YangExplorerでも、edit_configを使うサンプルコンフィグを出力しています。

create
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>
"""
delete
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を書くのも、そんなに難しくない気もします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0