使用paramiko登录netconf(2)

# 使用paramiko登录netconf
from time import sleep
import paramiko

TERMINATOR = b']]>]]>'
ip = '192.168.1.23'
username = 'root'
password = 'root!'
new_msg = """<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="3">
  <get-config>
    <source>
        <running/>
    </source>
  </get-config>
</rpc>
]]>]]>

"""

def read(chan, responses=1):
    """Read responses."""
    while responses:
        sleep(1)
        response = chan.recv(2048)
        yield response
        responses -= response.count(TERMINATOR)


ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip,
                   username=username,
                   password=password,
                   port=830,
                   timeout=300,
                   look_for_keys=False)

ssh_client.get_transport().set_keepalive(600)

chan = ssh_client.get_transport().open_session()
chan.invoke_subsystem('netconf')
print(list(read(chan)))
chan.sendall(new_msg.encode("utf-8"))
print(list(read(chan)))


chan.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值