OPS操作网络设备启动配置文件

1、实验说明:有一台CE12800设备,管理IP地址192.168.56.177,
现在需要测试设备OPS(Open Programmability System)功能,查询并清除设备的启动配置文件。
本实验需要额外准备FTP服务器/软件,FTP服务器地址192.168.56.1
交换机为FTP客户端,交换机管理IP:192.168.56.177
2、配置思路:
2.1在PC上编写OPS脚本ops_demo.py
2.2上传脚本文件ops_demo.py到交换机
2.3在交换机上运行ops_demo.py

FTP服务器工作目录:F:\Users\Administrator\PycharmProjects\python_switch\ops\

使用FileZilla作为FTP软件,Virtual path  /ops/

Native path  F:\Users\Administrator\PycharmProjects\python_switch\ops\

交换机配置:

int g1/0/0
un sh
int vlani 1
ip add 192.168.56.177
q

stel s e
user-i v 4
auth aaa
pro in ssh
u p l 3
q
ssh user python
ssh user python auth password
ssh user python ser stel

aaa
local-user python password irreversible-cipher Huawei@123
local-user python service-type ssh
local-user python user-group manage-ug
commit

  

ops_demo.py代码:

 

#!/usr/bin
# _*_ coding: UTF-8 _*_
# Copyright (c) 2021 GengYu.All rights reserved
# @Create by gengyu
# @Create Time :2021/12/18
# @File Name : ops_demo
# 打包命令 pyinstaller -F package\ops_demo
"""

"""
__author__ = 'Administrator'

import traceback
import httplib
#Python 2.x中的"httplib"模块在Python 3.x中变成了"http.client",目前交换机内置python 2.x
# import http.client
import string

class OPSConnection(object):
    """Make an OPS connection instance."""
    #初始化类,创建一个HTTP连接
    def __init__(self, host, port = 80):
        self.host = host
        self.port = port
        self.headers = {
            "Content-type": "text/xml",
            "Accept": "text/xml"
        }
        self.conn = None

    #关闭HTTP连接
    def close(self):
        """ Close the connection"""
        self.conn.close()

    #创建设备资源操作
    def create(self, uri, req_data):
        """ Delete operation"""
        ret = self.rest_call("GET", uri, req_data)
        return ret

    #删除设备资源操作
    def delete(self, uri, req_data):
        """Delete operation"""
        ret = self.rest_call("DELETE", uri, req_data)
        return ret

    #查询设备资源操作
    def get(self, uri, req_data=None):
        """Get operation"""
        ret = self.rest_call("GET", uri, req_data)
        return ret

    #修改设备资源操作
    def set(self, uri, req_data):
        """ Set operation"""
        ret = self.rest_call("PUT", uri, req_data)
        return ret

    #类内部调用的方法
    def rest_call(self, method, uri, req_data):
        """ REST call"""
        print('| - - - - - - - - - - - - - - - request: - - - - - - - - - - - - - - -|')
        print('%s %s HTTP/1.1\n' % (method, uri))
        if req_data == None:
            body = ""
        else:
            body = req_data
            print(body)
        if self.conn:
            self.conn.close()
        self.conn = httplib.HTTPConnection(self.host, self.port)
        # self.conn = http.client.HTTPConnection(self.host, self.port)

        self.conn.request(method, uri, body, self.headers)
        response = self.conn.getresponse()
        # response.status = http.client.OK    #stub code
        response.status = httplib.OK  # stub code

        ret = (response.status, response.reason, response.read())
        print('| - - - - - - - - - - - - - - - response: - - - - - - - - - - - - - - -|')
        print('HTTP/1.1 %s %s\n\n%s' % ret)
        print('| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|')
        return ret

def get_startup_info(ops_conn):
    #指定系统启动信息的URI。URI为Restful API中定义的管理对象,不同的管理对象有不同的URI
    #用户需要根据实际需求对URI进行修改,关于设备支持的URI可参考RESTful API
    uri = "/cfg/startupInfos/startupInfo"

    #指定发送的请求内容。该部分内容与URI相对应,不同的URI对应不同的请求内容
    #用户需要根据实际需求对URI进行修改,关于设备支持的URI可参考RESTful API
    req_data = \
"""
<?xml version="1.0" encoding="UTF-8"?>
<startupInfo>
</startupInfo>
"""
    ret,_,rsp_data = ops_conn.get(uri, req_data)
    if ret != httplib.OK:
        return None
    return rsp_data

def clear_startup_info(ops_conn):
    uri = "/cfg/clearStartup"
    req_data = \
    """
    <?xml version="1.0" encoding="UTF-8"?>
    <clearStartup>
    </clearStartup>
    """
    ret,_,rsp_data = ops_conn.create(uri, req_data)
    if ret != httplib.OK:
        return None


def main():
    """The main function."""
    #host表示环路地址,当前RESTful API为设备内部调用,即取值为"localhost"。
    host = "localhost"
    try:
        #建立HTTP连接
        ops_conn = OPSConnection(host)
        #调用获取系统启动信息的函数
        rsp_data = get_startup_info(ops_conn)
        rsp_data = clear_startup_info(ops_conn)
        rsp_data = get_startup_info(ops_conn)
        #关闭HTTP连接
        ops_conn.close()
        return
    except:
        errinfo = traceback.format_exc()
        print(errinfo)
        return


if __name__ == "__main__":
    main()

 

下载ops_demo.py文件到交换机:

ftp 192.168.56.1
admin
admin123
get /ops/ops_demo.py
q
在交换机上运行ops_demo.py文件
ops install file ops_demo.py
dis ops script
ops run python ops_demo.py

运行结果:
<HUAWEI>ops run python ops_demo.py
| - - - - - - - - - - - - - - - request: - - - - - - - - - - - - - - -|
GET /cfg/startupInfos/startupInfo HTTP/1.1


<?xml version="1.0" encoding="UTF-8"?>
<startupInfo>
</startupInfo>

| - - - - - - - - - - - - - - - response: - - - - - - - - - - - - - - -|
HTTP/1.1 200 OK

<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply>
  <data>
    <cfg xmlns="http://www.huawei.com/netconf/vrp" format-version="1.0" content-
version="1.0">
      <startupInfos>
        <startupInfo>
          <position>17</position>
          <nextStartupFile>cfcard:/vrpcfg.cfg</nextStartupFile>
          <configedSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</con
figedSysSoft>
          <curSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</curSysSo
ft>
          <nextSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</nextSys
Soft>
          <curStartupFile>cfcard:/vrpcfg.cfg</curStartupFile>
          <curPatchFile>NULL</curPatchFile>
          <nextPatchFile>NULL</nextPatchFile>
          <boardInfo>101</boardInfo>
          <curPafFile>default</curPafFile>
          <nextPafFile>default</nextPafFile>
        </startupInfo>
      </startupInfos>
    </cfg>
  </data>
</rpc-reply>

| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| - - - - - - - - - - - - - - - request: - - - - - - - - - - - - - - -|
POST /cfg/clearStartup HTTP/1.1


    <?xml version="1.0" encoding="UTF-8"?>
    <clearStartup>
    </clearStartup>
    
| - - - - - - - - - - - - - - - response: - - - - - - - - - - - - - - -|
HTTP/1.1 200 OK

<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply>
  <ok/>
</rpc-reply>

| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|
| - - - - - - - - - - - - - - - request: - - - - - - - - - - - - - - -|
GET /cfg/startupInfos/startupInfo HTTP/1.1


<?xml version="1.0" encoding="UTF-8"?>
<startupInfo>
</startupInfo>

| - - - - - - - - - - - - - - - response: - - - - - - - - - - - - - - -|
HTTP/1.1 200 OK

<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply>
  <data>
    <cfg xmlns="http://www.huawei.com/netconf/vrp" format-version="1.0" content-
version="1.0">
      <startupInfos>
        <startupInfo>
          <position>17</position>
          <nextStartupFile>NULL</nextStartupFile>
          <configedSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</con
figedSysSoft>
          <curSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</curSysSo
ft>
          <nextSysSoft>cfcard:/VRPV200R005C10SPC607B607D0306_ce12800.cc</nextSys
Soft>
          <curStartupFile>NULL</curStartupFile>
          <curPatchFile>NULL</curPatchFile>
          <nextPatchFile>NULL</nextPatchFile>
          <boardInfo>101</boardInfo>
          <curPafFile>default</curPafFile>
          <nextPafFile>default</nextPafFile>
        </startupInfo>
      </startupInfos>
    </cfg>
  </data>
</rpc-reply>

| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -|

  

上一篇:Qt Lite


下一篇:javascript面向对象系列第四篇——选项卡的实现