系统环境: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