import datetime
import time
from netmiko import ConnectHandler
from openpyxl import Workbook
wb = Workbook() #创建excel文件对象
ws = wb.active #创建excel文件对象中sheet页
dev = {'device_type':'cisco_ios',
'host':'192.168.1.1',
'username':'telecomadmin',
'password':'nE7jA%5m',
'port':22}
i = 1
while True:
test1 = str('A')+str(i)
test2 = str('B')+str(i)
test3 = str('C')+str(i)
test4 = str('D')+str(i)
ws[test1] = datetime.datetime.now()
with ConnectHandler(**dev) as conn:
conn.send_command(command_string='enable', expect_string=r'host#')
ws[test2] = conn.send_command(command_string='show version all', expect_string=r'host#')
ws[test3] = conn.send_command(command_string='show cpu usage', expect_string=r'host#')
ws[test4] = conn.send_command(command_string='show memory usage', expect_string=r'host#')
wb.save("test.xlsx")
i = i + 1
time.sleep(30)
import datetime
ws[test1] = datetime.datetime.now()
import time
time.sleep(30)
from netmiko import ConnectHandler
#定义ssh登录函数dev
dev = {'device_type':'cisco_ios',
'host':'192.168.1.1',
'username':'telecomadmin',
'password':'nE7jA%5m',
'port':22}
#将dev实例化成conn,以便conn.引用各种函数
with ConnectHandler(**dev) as conn:
conn.send_command(command_string='enable', expect_string=r'host#')
需要先经过2.3网络设备登录,然后才能对网络设备进行cli命令行操作
conn.send_command(command_string='enable', expect_string=r'host#')
ws[test2] = conn.send_command(command_string='show version all', expect_string=r'host#')
ws[test3] = conn.send_command(command_string='show cpu usage', expect_string=r'host#')
ws[test4] = conn.send_command(command_string='show memory usage', expect_string=r'host#')
from openpyxl import Workbook
wb = Workbook() #创建excel文件对象
ws = wb.active #创建excel文件对象中sheet页
i = 1 #初始化,整型
while True:
test1 = str('A')+str(i) #定义变量test并赋值,因为i初始化为整型,所以先对其str转换成字符串,再用字符串拼接到A/B/C/D上去。即A1、B1、C1、D1.excel表格中,sheet页中的定位坐标
test2 = str('B')+str(i)
test3 = str('C')+str(i)
test4 = str('D')+str(i)
ws[test1] = ?? #将??表示的内容赋值给定位坐标
ws[test2] = ??
ws[test3] = ??
ws[test4] = ??
wb.save("test.xlsx") #保存excel文件对象
i = i + 1 #下一周期就是A2、B2、C2、D2,以此类推
采用os命令可以查看该python脚本的运行路径
import os
os.getcwd()
如下是具体运行路径即excel文档存储路径
C:\Users\Administrator\PycharmProjects\pythonProject2\venv\Scripts\python.exe
C:/Users/Administrator/PycharmProjects/pythonProject2/main.py
查看存储文件中的内容