个人没有系统学习过Python,编程能力也很烂,但是可以通过简单的程序来简化自己的工作量,脚本如下:
import paramiko
import xlrd
import time
import random
import os
#定义连接SSH的过程
def SSH_Connect(ssh_host, ssh_hostname):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ssh_host, port=22, username='admin', password='admin@admin')
# 使用交互式终端
channel = ssh.invoke_shell()
time.sleep(1)
channel.send('terminal length 0\nshow run' + '\n')
# 这里等待时间为了让返回字符写入缓存中,根据配置量来进行设置
time.sleep(10)
temp = channel.recv(120000).decode()
# 这里调用另外一个过程,将缓存中的配置内容进行处理并保存
Get_Conf(temp, ssh_hostname)
ssh.close()
#本函数的功能,由于通过SSH获取下来的内容存在大量空行,需要进行格式化。
#由于不会在缓存阶段处理