1. 程式人生 > 其它 >gRPC遠端查詢網路裝置配置

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