Created
September 13, 2017 13:34
-
-
Save adzhurinskij/6862b78037c6a8cd2e623b6625b08975 to your computer and use it in GitHub Desktop.
Huawei NetConf Example with paramiko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import paramiko | |
from paramiko.ssh_exception import SSHException, BadHostKeyException | |
from paramiko.ssh_exception import AuthenticationException | |
from time import sleep | |
host="" | |
port=830 | |
# port=22 | |
login="" | |
password="" | |
TERMINATOR = b']]>]]>' | |
HELLO = b''' | |
<?xml version="1.0" encoding="UTF-8"?> | |
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> | |
<capabilities> | |
<capability>urn:ietf:params:netconf:base:1.0</capability> | |
<capability>urn:ietf:params:netconf:capability:writable-running:1.0</capability> | |
<capability>urn:ietf:params:netconf:capability:candidate:1.0</capability> | |
<capability>urn:ietf:params:netconf:capability:confirmed-commit:1.0</capability> | |
<capability>http://www.huawei.com/netconf/capability/discard-commit/1.0</capability> | |
<capability>urn:ietf:params:netconf:capability:startup:1.0</capability> | |
<capability>urn:ietf:params:netconf:capability:rollback-on-error:1.0</capability> | |
<capability>http://www.huawei.com/netconf/capability/sync/1.2</capability> | |
<capability>http://www.huawei.com/netconf/capability/sync/1.1</capability> | |
<capability>http://www.huawei.com/netconf/capability/sync/1.0</capability> | |
<capability>http://www.huawei.com/netconf/capability/exchange/1.0</capability> | |
<capability>http://www.huawei.com/netconf/capability/exchange/1.1</capability> | |
<capability>http://www.huawei.com/netconf/capability/active/1.0</capability> | |
<capability>http://www.huawei.com/netconf/capability/action/1.0</capability> | |
<capability>http://www.huawei.com/netconf/capability/execute-cli/1.0</capability> | |
<capability>http://www.huawei.com/netconf/capability/update/1.0</capability> | |
<capability>http://www.huawei.com/netconf/capability/commit-description/1.0</capability> | |
</capabilities> | |
</hello> | |
''' | |
REQUEST=""" | |
<?xml version="1.0" encoding="UTF-8"?> | |
<rpc message-id="1" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"> | |
<get> | |
<filter type="subtree"> | |
<devm xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> | |
<cpuInfos> | |
<cpuInfo> | |
</cpuInfo> | |
</cpuInfos> | |
</devm> | |
</filter> | |
</get> | |
</rpc> | |
""" | |
def read(chan, responses=1): | |
"""Read responses.""" | |
while responses: | |
sleep(1) | |
response = chan.recv(2048) | |
yield response | |
responses -= response.count(TERMINATOR) | |
# connect | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
client.connect(host, port, login, password, look_for_keys=False, allow_agent=False) | |
client.get_transport().set_keepalive(600) | |
# communicate | |
chan = client.get_transport().open_session() | |
chan.invoke_subsystem('netconf') | |
# read hello | |
list(read(chan)) | |
# send hello | |
chan.send(HELLO + TERMINATOR) | |
# send REQUEST | |
chan.sendall(REQUEST + TERMINATOR) | |
# show response | |
print list(read(chan)) | |
chan.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment