批量sshRouterOS配置路由器

该博客介绍了一种在Windows环境下,通过Python编写程序批量管理运行RouterOS 6.44.5的路由器的方法。程序实现了SSH连接,获取pppoe拨号数、版本信息、运行时间、总流量等功能,并最终将脚本打包为exe文件,便于执行。
摘要由CSDN通过智能技术生成

系统环境:windows,RouterOS 6.44.5

语言:python

1.程序制作原因:

        需要经常对几百台RouterOS进行配置和获取数据

2.需要实现的功能有

        ①获取RouterOS中的pppoe拨号总数

        ②获取 RouterOS的版本号

        ③获取 RouterOS的运行时间

        ④获取RouterOS中的总流量

        ⑤其他的自定义命令操作

3.程序最终效果图:

        

4.程序运行之前RouterOS路由器需要设置好ssh端口并启用(端口根据实际情况设置)

 

5.完整代码cmd_exec_model.py如下:

import paramiko
import os
import tkinter as tk
import threading
import encodings.idna
from tkinter import scrolledtext

# 定义存储结果的数组
re_result = []
out_result = []


# 定义ssh的函数
def ssh_connect(host_ip, host_port, user_name, password, command):
    # SSH远程连接
    ssh = paramiko.SSHClient()  # 创建sshclient
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # 指定当对方主机没有本机公钥的情况时应该怎么办,AutoAddPolicy表示自动在对方主机保存下本机的秘钥
    ssh.connect(host_ip, host_port, user_name, password, timeout=5, allow_agent=False, look_for_keys=False)
    # 执行命令并获取执行结果
    stdin, stdout, stderr = ssh.exec_command(command)
    out = stdout.readlines()
    err = stderr.readlines()

    for i in out:
        message = str(host_ip) + " " + str(i)
        out_result.append(message)

    ssh.close()


def operation(user_name, password, host_port, c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值