批量sshRouterOS配置路由器

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

系统环境: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_ip, d_ip, command):
    # 执行ssh命令
    host_ip = str(c_ip) + "." + str(d_ip)

    try:
        ssh_connect(host_ip, host_port, user_name, password, command)

    except Exception as e:
        content = host_ip + " 连接超时 " + str(e) + "\n"
        re_result.append(content)


def main():
    window = tk.Tk()

    window.geometry("700x700")
    window.title("ROS多命令整合软件 Version 1.5")

    # 用户名设置的标签和输入框(位置和名称)
    lable_user = tk.Label(window, text="用户名")
    lable_user.place(x=1, y=1)

    entry_user = tk.Entry(window, width=10)
    entry_user.place(x=100, y=1)

    # 密码设置的标签和输入框(位置和名称)
    lable_pass = tk.Label(window, text="密码")
    lable_pass.place(x=1, y=30)

    entry_pass = tk.Entry(window, width=20)
    entry_pass.place(x=100, y=30)

    # c段IP设置的标签和输入框(位置和名称)
    lable_c_ip = tk.Label(window, text="A.B.C段IP")
    lable_c_ip.place(x=1, y=60)

    entry_c_ip = tk.Entry(window, width=10)
    entry_c_ip.place(x=100, y=60)

    # D段IP起始值的标签和输入框(位置和名称)
    lable_d_start_ip = tk.Label(window, text="D段IP起始值")
    lable_d_start_ip.place(x=1, y=90)

    entry_d_start_ip = tk.Entry(window, width=5)
    entry_d_start_ip.place(x=100, y=90)

    # D段IP结束值的标签和输入框(位置和名称)
    lable_d_end_ip = tk.Label(window, text="D段IP结束值")
    lable_d_end_ip.place(x=1, y=120)

    entry_d_end_ip = tk.Entry(window, width=5)
    entry_d_end_ip.place(x=100, y=120)

    # 端口的标签和输入框(位置和名称)
    lable_port = tk.Label(window, text="端口")
    lable_port.place(x=1, y=150)

    entry_port = tk.Entry(window, width=10)
    entry_port.place(x=100, y=150)

    # 执行的命令(位置和名称)
    lable_command = tk.Label(window, text="执行的命令")
    lable_command.place(x=1, y=180)

    command_text = scrolledtext.ScrolledText(window, width=80, height=10)
    command_text.place(x=100, y=180)

    def show_command(ssh_command):

        u_name = entry_user.get()
        u_port = entry_port.get()
        u_pass = entry_pass.get()
        ip_c = entry_c_ip.get()
        ip_d_start = int(entry_d_start_ip.get())
        ip_d_end = int(entry_d_end_ip.get())
        
        # 定义默认值
        if not u_name.strip():
            u_name = 'admin'
        if not u_port.strip():
            u_port = '22'
        if not u_pass.strip():
            u_pass = '123456'
        if not ip_c.strip():
            ip_c = '192.168.1'
        # print(u_name, u_port, u_pass)

        # 定义线程数组
        threads = []

        for num in range(ip_d_start, ip_d_end + 1):
            th = threading.Thread(target=operation, args=(u_name, u_pass, u_port, ip_c, num, ssh_command,))
            th.setDaemon(True)
            th.start()
            threads.append(th)

        # 关闭所有子线程
        for th in threads:
            th.join()

      
        re_result.sort(key=lambda i: (int(i.split('.')[2]), int(i.split('.')[3].split(' ')[0])))
        out_result.sort(key=lambda i: (int(i.split('.')[2]), int(i.split('.')[3].split(' ')[0])))
        re_it = iter(re_result)
        out_it = iter(out_result)

        # 将结果数组插入到滚动栏
        for b in re_it:
            result_text.insert(tk.END, b)
            # result_text.insert(tk.ALL, b)
        for c in out_it:
            result_text.insert(tk.END, c)
            # result_text.insert(tk.ALL, c)

        # 清空旧的结果,防止再次执行还会显示
        re_result.clear()
        out_result.clear()

    def cmd_cout():
        ssh_command = ':gl lt 0;/interface pppoe-cl;:foreach i in [find] do={:set lt ($lt+1)};:put $lt'
        show_command(ssh_command)

    def cmd_get_version():
        ssh_command = '/system resource;:put [get version]'
        show_command(ssh_command)

    def cmd_get_uptime():
        ssh_command = '/system resource;:put [get uptime]'
        show_command(ssh_command)

    def cmd_get_traffic():
        ssh_command = ':gl tx;:gl rx;/interface monitor-traffic interface=aggregate once file=traffic do={:set rx $"rx-bits-per-second";:set tx $"tx-bits-per-second"};:put [[($tx/1000000)];[($rx/1000000)];[(($tx+$rx)/1000000)]]'
        show_command(ssh_command)

    def cmd_user_input():
        ssh_command = command_text.get('1.0', tk.END)
        ssh_command = ssh_command.replace('\n', ';')
        # print(ssh_command)
        show_command(ssh_command)

    # 按钮的设置
    button = tk.Button(window, text="统计拨号数", width=10, height=2, font=('黑体', 10, 'bold'), fg="blue", command=cmd_cout)
    button.place(x=100, y=330)

    btn_get_version = tk.Button(window, text="获取版本号", width=10, height=2, font=('黑体', 10, 'bold'), fg="blue",
                                command=cmd_get_version)
    btn_get_version.place(x=200, y=330)

    btn_get_uptime = tk.Button(window, text="获取uptime", width=10, height=2, font=('黑体', 10, 'bold'), fg="blue",
                               command=cmd_get_uptime)
    btn_get_uptime.place(x=300, y=330)

    btn_get_uptime = tk.Button(window, text="获取traffic", width=10, height=2, font=('黑体', 10, 'bold'), fg="blue",
                               command=cmd_get_traffic)
    btn_get_uptime.place(x=400, y=330)

    btn_user_input = tk.Button(window, text="输入命令执行", width=11, height=2, font=('黑体', 10, 'bold'), fg="blue",
                               command=cmd_user_input)
    btn_user_input.place(x=500, y=330)

    # 报错结果显示(位置和名称)
    lable_result = tk.Label(window, text="报错显示")
    lable_result.place(x=1, y=370)

    # 滚动条显示
    result_text = scrolledtext.ScrolledText(window)
    result_text.place(x=100, y=370)

    tk.mainloop()


if __name__ == '__main__':
    main()

6.程序打包成exe

(venv) D:\work\venv\Scripts>pyinstaller.exe -Fw D:\work\cmd_exec_model.py

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值