gRPC远程查询网络设备配置

  • get_config.proto
  • get_config_server.py
  • get_config_client.py
  • get_config_pb2.py
  • get_config_pb2_grpc.py

get_config.proto文件内容:

syntax = "proto3";
package get_config;
//the get_config service definition
service get_config {
    //RPC 请求和响应
    rpc Login_info (Request) returns (Reply) {}
}
//the request message containing the login information
message Request {
    string host = 1;
    string username = 2;
    string password = 3;
}
//the response message containing the string reply
message Reply{
    string message =1;
}

pip安装grpcio-tools:

pip install grpcio-tools

#C:\Users\Administrator\PycharmProjects\python_switch\grpc_test>python -m grpc_tools.protoc -I./ --python_out=. --grpc_python_out=. ./get_config.proto

在get_config.proto文件目录下,cmd执行:python -m grpc_tools.protoc -I./ --python_out=. --grpc_python_out=. ./get_config.proto

get_config.proto文件目录下会自动生成两个文件:get_config_pb2.py和get_config_pb2_grpc.py

 

然后创建server端get_config_server.py代码:

#!/usr/bin
# _*_ coding: UTF-8 _*_
# Copyright (c) 2021 GengYu.All rights reserved
# @Create by gengyu
# @Create Time :2021/12/16
# @File Name : get_config_server
# 打包命令 pyinstaller -F package\get_config_server
# C:\Users\Administrator\PycharmProjects\python_switch\grpc_test>python -m grpc_tools.protoc -I./ --python_out=. --grpc_python_out=. ./get_config.proto
__author__ = 'Administrator'
import get_config_pb2_grpc
from concurrent import futures
import time
import grpc
import get_config_pb2
import paramiko
class Display_Config(get_config_pb2_grpc.get_configServicer):
    def Login_info(self, request, context):
        ssh = paramiko.client.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        ssh.connect(hostname=request.host,port=22,username=request.username,password=request.password)
        cli = ssh.invoke_shell()
        cli.send('N\n')
        time.sleep(0.5)
        cli.send('screen-length 0 temporary\n')
        time.sleep(0.5)
        cli.send('display cu\n')
        time.sleep(2)
        data = cli.recv(999999).decode()
        ssh.close()
        #返回回显的配置信息
        return get_config_pb2.Reply(message=data)

def server():
    #创建gPRC服务
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    #从定义的服务中部署gRPC servicer
    get_config_pb2_grpc.add_get_configServicer_to_server(Display_Config(),server)
    # 启动服务器
    server.add_insecure_port('localhost:8080')
    server.start()
    _ONE_DAY_IN_SECONDS = 60 * 60 * 24
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop()

if __name__ == "__main__":
    server()

 

创建客户端get_config_client.py代码:

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

"""
__author__ = 'Administrator'
import grpc
import doctest
import get_config_pb2
import get_config_pb2_grpc
def run():
    #客户端实例化stub
    connect = grpc.insecure_channel('localhost:8080')
    stub = get_config_pb2_grpc.get_configStub(channel=connect)
    #通过 stub调用服务端的Login_info方法
    response = stub.Login_info(get_config_pb2.Request(host='192.168.56.177',username='python',password='Huawei@123'))
    print(response.message)
if __name__ == "__main__":
    run()

交换机配置:

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

  

posted @ 2021-12-18 13:58  火山的爱  阅读(206)  评论(0)    收藏  举报