求大神解惑。
最近在学习自动化运维,然后写了一段代码,运行没问题,但是结果有问题,代码如下:import shutil
import pexpect
import sys
import datetime
import os
import pymssql
# 今天的日期
today = datetime.date.today().strftime('%Y-%m-%d')
path = "/root/xunjian/" + today
if os.path.exists(path):
shutil.rmtree(path)# 递归删除文件夹下的所有子文件夹和子文件
os.mkdir(path, 777)
def switch(name, ip, passwd):
try:
name1 = "----More----"# 模拟交换机出现的翻译提示
child = pexpect.spawn('telnet %s' % ip)
fout = open(
'/root/xunjian/' + today + '/' + '%s-%s.txt' %
(name, ip), 'wb+')
child.logfile = fout
child.expect('Username:', timeout=5)# 提示用户登录,输入账号
child.sendline("admin")
child.expect('(?i)ssword:', timeout=5)# 提示输入密码
child.sendline("%s" % passwd)
child.expect('<%s>' % name)
child.sendline("display cpu-usage")# 查看cpu状态
child.expect('<%s>' % name)
child.sendline("display memory")# 查看内存状态
child.expect('<%s>' % name)
child.sendline("display environment")# 查看运行温度
child.expect('<%s>' % name)
child.sendline("display fan")# 查看风扇状态
child.expect('<%s>' % name)
child.sendline("display power")# 查看电源状态
child.expect('<%s>' % name)
child.sendline("display ip routing-table")# 路由表
for i in range(10):
index = child.expect()
if index == 0:
child.send(" ")
else:
child.sendline("display interface brief")# 端口状态
break
for i in range(10):
index = child.expect()
if index == 0:
child.send(" ")
else:
child.sendline("disversion")# 版本,为了看运行时间
break
for i in range(10):
index = child.expect()
if index == 0:
child.send(" ")
else:
child.sendline("display log")# 日志,日志较多,循环100个空格,怕输出不全。
break
for i in range(100):
index = child.expect()
if index == 0:
child.send(" ")
else:
child.sendline("quit")
break
except BaseException:
pass
host = 'localhost'# 连接数据库,抓取数据库内的信息,交换机的名字、IP、密码
port = 1433
user = 'sa'
pwd = 'Zhonglang@159'
db = 'test'
connect = pymssql.connect(
host=host,
port=port,
user=user,
password=pwd,
database=db,
charset="utf8")
cur = connect.cursor()
sqls = "select * from ."
cur.execute(sqls)
listall = cur.fetchall()# SQL输出内容导成列表
for line in listall:
switch(line, line, line)
connect.commit()
connect.close()
运行结果如下:
# cat stackB-10.1.1.3.txt
Trying 10.1.1.3...
Connected to 10.1.1.3.
Escape character is '^]'.
Warning: Telnet is not a secure protocol, and it is recommended to use Stelnet.
Login authentication
Username:admin
admin
Password:123@123@123
Info: The max number of VTY users is 20, and the number
of current VTY users on line is 1.
The current login time is 2022-01-27 14:19:04+00:00.
<stackB>display cpu-usage
display cpu-usage
CPU Usage Stat. Cycle: 60 (Second)
CPU Usage : 76% Max: 96%
CPU Usage Stat. Time : 2022-01-2714:19:04
CPU utilization for five seconds: 76%: one minute: 76%: five minutes: 76%
Max CPU Usage Stat. Time : 2021-12-12 09:26:13.
TaskName CPURuntime(CPU Tick High/Tick Low)Task Explanation
AGNT 45% 1/97d843a0 AGNTSNMP agent task
VIDL 24% 0/d693a3fe DOPRA IDLE
OS 19% 0/a9df8c7f Operation System
l2sy 5% 0/3152a5e0 l2sync
SFPT 3% 0/22921900 SFPT Timer
FTS 2% 0/1502b920 FTS
IFPD 1% 0/ aba9500 IFPD Ifnet Product Adapter
TICK 1% 0/ b06e040
AAA 0% 0/ 0 AAAAuthen Account Authorize
ACL 0% 0/2625a0 ACL Access Control List
ADPT 0% 0/ 0 ADPT Adapter
AGENT_CAPWAP 0% 0/ 0 AGENT_CAPWAP
AGENT_WLAN 0% 0/ 0 AGENT_WLAN
AGT6 0% 0/ 0 AGT6SNMP AGT6 task
ALM 0% 0/ 0 ALMAlarm Management
ALS 0% 0/2625a0 ALSLoss of Signal
AM 0% 0/ 23c3460 AM Address Management
---- More ----#
到了More这里就没下文了。。
也就是说只执行了display cpu-usage这个命令,然后后面的命令都没有执行了。。不知道哪里出现了问题,求解答。谢谢。!
本帖最后由 甜蜜难在 于 2022-1-27 15:28 编辑
如果我在输入密码后面再手动加入一句输入密码,代码如下:
import shutil
import pexpect
import sys
import datetime
import os
import pymssql
# 今天的日期
today = datetime.date.today().strftime('%Y-%m-%d')
path = "/root/xunjian/" + today
if os.path.exists(path):
shutil.rmtree(path)# 递归删除文件夹下的所有子文件夹和子文件
os.mkdir(path, 777)
def switch(name, ip, passwd):
try:
name1 = "----More----"# 模拟交换机出现的翻页提示
child = pexpect.spawn('telnet %s' % ip)
fout = open(
'/root/xunjian/' + today + '/' + '%s-%s.txt' %
(name, ip), 'wb+')
child.logfile = fout
child.expect('Username:', timeout=5)# 提示用户登录,输入账号
child.sendline("admin")
child.expect('(?i)ssword:', timeout=5)# 提示输入密码
child.sendline("123@123@123")
child.sendline("%s" % passwd)
child.expect('<%s>' % name)
child.sendline("display cpu-usage")# 查看cpu状态
child.expect('<%s>' % name)
child.sendline("display memory")# 查看内存状态
child.expect('<%s>' % name)
child.sendline("display environment")# 查看运行温度
child.expect('<%s>' % name)
child.sendline("display fan")# 查看风扇状态
child.expect('<%s>' % name)
child.sendline("display power")# 查看电源状态
child.expect('<%s>' % name)
child.sendline("display ip routing-table")# 路由表
for i in range(10):
index = child.expect()
if index == 0:
child.send(" ")
else:
child.sendline("display interface brief")# 端口状态
break
for i in range(10):
index = child.expect()
if index == 0:
child.send(" ")
else:
child.sendline("disversion")# 版本,为了看运行时间
break
for i in range(10):
index = child.expect()
if index == 0:
child.send(" ")
else:
child.sendline("display log")# 日志,日志较多,循环100个空格,怕输出不全。
break
for i in range(100):
index = child.expect()
if index == 0:
child.send(" ")
else:
child.sendline("quit")
break
except BaseException:
pass
host = 'localhost'# 连接数据库,抓取数据库内的信息,交换机的名字、IP、密码
port = 1433
user = 'sa'
pwd = 'Zhonglang@159'
db = 'test'
connect = pymssql.connect(
host=host,
port=port,
user=user,
password=pwd,
database=db,
charset="utf8")
cur = connect.cursor()
sqls = "select * from ."
cur.execute(sqls)
listall = cur.fetchall()# SQL输出内容导成列表
for line in listall:
switch(line, line, line)
connect.commit()
connect.close()
那么执行结果就会很多,但是命令执行那里还是存在问题。。
# cat stackB-10.1.1.3.txt
Trying 10.1.1.3...
Connected to 10.1.1.3.
Escape character is '^]'.
Warning: Telnet is not a secure protocol, and it is recommended to use Stelnet.
Login authentication
Username:admin
admin
Password:123@123@123
Info: The max number of VTY users is 20, and the number
of current VTY users on line is 1.
The current login time is 2022-01-27 14:19:04+00:00.
<stackB>display cpu-usage
display cpu-usage
CPU Usage Stat. Cycle: 60 (Second)
CPU Usage : 76% Max: 96%
CPU Usage Stat. Time : 2022-01-2714:19:04
CPU utilization for five seconds: 76%: one minute: 76%: five minutes: 76%
Max CPU Usage Stat. Time : 2021-12-12 09:26:13.
TaskName CPURuntime(CPU Tick High/Tick Low)Task Explanation
AGNT 45% 1/97d843a0 AGNTSNMP agent task
VIDL 24% 0/d693a3fe DOPRA IDLE
OS 19% 0/a9df8c7f Operation System
l2sy 5% 0/3152a5e0 l2sync
SFPT 3% 0/22921900 SFPT Timer
FTS 2% 0/1502b920 FTS
IFPD 1% 0/ aba9500 IFPD Ifnet Product Adapter
TICK 1% 0/ b06e040
AAA 0% 0/ 0 AAAAuthen Account Authorize
ACL 0% 0/2625a0 ACL Access Control List
ADPT 0% 0/ 0 ADPT Adapter
AGENT_CAPWAP 0% 0/ 0 AGENT_CAPWAP
AGENT_WLAN 0% 0/ 0 AGENT_WLAN
AGT6 0% 0/ 0 AGT6SNMP AGT6 task
ALM 0% 0/ 0 ALMAlarm Management
ALS 0% 0/2625a0 ALSLoss of Signal
AM 0% 0/ 23c3460 AM Address Management
---- More ----# ^C
# cat stackB-10.1.1.3.txt
Trying 10.1.1.3...
Connected to 10.1.1.3.
Escape character is '^]'.
Warning: Telnet is not a secure protocol, and it is recommended to use Stelnet.
Login authentication
Username:admin
admin
Password:123@123@123
Info: The max number of VTY users is 20, and the number
of current VTY users on line is 1.
The current login time is 2022-01-27 14:19:04+00:00.
<stackB>display cpu-usage
display cpu-usage
CPU Usage Stat. Cycle: 60 (Second)
CPU Usage : 76% Max: 96%
CPU Usage Stat. Time : 2022-01-2714:19:04
CPU utilization for five seconds: 76%: one minute: 76%: five minutes: 76%
Max CPU Usage Stat. Time : 2021-12-12 09:26:13.
TaskName CPURuntime(CPU Tick High/Tick Low)Task Explanation
AGNT 45% 1/97d843a0 AGNTSNMP agent task
VIDL 24% 0/d693a3fe DOPRA IDLE
OS 19% 0/a9df8c7f Operation System
l2sy 5% 0/3152a5e0 l2sync
SFPT 3% 0/22921900 SFPT Timer
FTS 2% 0/1502b920 FTS
IFPD 1% 0/ aba9500 IFPD Ifnet Product Adapter
TICK 1% 0/ b06e040
AAA 0% 0/ 0 AAAAuthen Account Authorize
ACL 0% 0/2625a0 ACL Access Control List
ADPT 0% 0/ 0 ADPT Adapter
AGENT_CAPWAP 0% 0/ 0 AGENT_CAPWAP
AGENT_WLAN 0% 0/ 0 AGENT_WLAN
AGT6 0% 0/ 0 AGT6SNMP AGT6 task
ALM 0% 0/ 0 ALMAlarm Management
ALS 0% 0/2625a0 ALSLoss of Signal
AM 0% 0/ 23c3460 AM Address Management
---- More ----#
# rm -rf stackB-10.1.1.3.txt
# cd ..
# cd ..
# rm -rf test.py
# rz
rz waiting to receive.
?a? zmodem ′???£°′ Ctrl+C ???£
Transferring test.py...
100% 3 KB 3 KB/s 00:00:01 0 Errors
?# python test.py
# cd xunjian
# cd 2022-01-27/
# ls
stackB-10.1.1.3.txt
# cat stackB-10.1.1.3.txt
Trying 10.1.1.3...
Connected to 10.1.1.3.
Escape character is '^]'.
Warning: Telnet is not a secure protocol, and it is recommended to use Stelnet.
Login authentication
Username:admin
admin
Password:123@123@123
123@123@123
Info: The max number of VTY users is 20, and the number
of current VTY users on line is 1.
The current login time is 2022-01-27 14:30:15+00:00.
<stackB>display cpu-usage
123@123@123
^
Error: Unrecognized command found at '^' position.
<stackB>display memory
display cpu-usage
CPU Usage Stat. Cycle: 60 (Second)
CPU Usage : 79% Max: 96%
CPU Usage Stat. Time : 2022-01-2714:30:15
CPU utilization for five seconds: 79%: one minute: 79%: five minutes: 79%
Max CPU Usage Stat. Time : 2021-12-12 09:26:13.
TaskName CPURuntime(CPU Tick High/Tick Low)Task Explanation
OS 21% 0/bbc12f7e Operation System
VIDL 21% 0/bbc12f7e DOPRA IDLE
FTS 15% 0/8a6e89a0 FTS
SOCK 12% 0/718bfe40 SOCKPacket schedule and
process
AGNT 6% 0/3c0d3ae0 AGNTSNMP agent task
VPR 6% 0/3c336080 VPR VP Receive
L3I4 4% 0/245bdc80 L3I4 LPU Manage IPv4 unicast
FDB
SFPT 3% 0/1b9130a0 SFPT Timer
rx_2 3% 0/21f98280 rx_2
IT59 2% 0/178b38c0 IT59
rx_7 2% 0/125413e0 rx_7
ARP 1% 0/ e7441a0 ARP
IFPD 1% 0/ b06e040 IFPD Ifnet Product Adapter
IPCR 1% 0/ aba9500 IPCR InterPro Communicate
Receive
<stackB>display environment
isplay memory
^
Error: Unrecognized command found at '^' position.
<stackB>display fan
display environment
^
Error: Unrecognized command found at '^' position.
<stackB>display power
display fan
-------------------------------------------------------------------------
SlotFanID Online Status Speed Mode Airflow
-------------------------------------------------------------------------
0 1 Present Normal 35% Auto Side-to-Side
0 2 Present Normal 35% Auto Side-to-Side
0 3 Present Normal 35% Auto Side-to-Side
1 1 Present Normal 35% Auto Side-to-Side
1 2 Present Normal 35% Auto Side-to-Side
1 3 Present Normal 35% Auto Side-to-Side
<stackB>display ip routing-table
display power
Info: There is no power information.
<stackB>display interface brief
display ip routing-table
Route Flags: R - relay, D - download to fib
------------------------------------------------------------------------------
Routing Tables: Public
Destinations : 103 Routes : 103
Destination/Mask Proto PreCost Flags NextHop Interface
0.0.0.0/0 Static60 0 RD 10.1.1.1 Vlanif100
10.1.1.0/24Direct0 0 D 10.1.1.3 Vlanif100
10.1.1.3/32Direct0 0 D 127.0.0.1 Vlanif100
10.237.248.0/24Direct0 0 D 10.237.248.1 Vlanif300
10.237.248.1/32Direct0 0 D 127.0.0.1 Vlanif300
127.0.0.0/8 Direct0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32Direct0 0 D 127.0.0.1 InLoopBack0
172.16.1.0/24Direct0 0 D 172.16.1.254 Vlanif300
172.16.1.254/32Direct0 0 D 127.0.0.1 Vlanif300
172.16.2.0/24Direct0 0 D 172.16.2.1 Vlanif300
172.16.2.1/32Direct0 0 D 127.0.0.1 Vlanif300
172.16.3.0/24Direct0 0 D 172.16.3.1 Vlanif300
172.16.3.1/32Direct0 0 D 127.0.0.1 Vlanif300
172.17.1.0/24Direct0 0 D 172.17.1.1 Vlanif300
172.17.1.1/32Direct0 0 D 127.0.0.1 Vlanif300
172.17.2.0/24Direct0 0 D 172.17.2.1 Vlanif300
172.17.2.1/32Direct0 0 D 127.0.0.1 Vlanif300
172.17.3.0/24Direct0 0 D 172.17.3.1 Vlanif300
172.17.3.1/32Direct0 0 D 127.0.0.1 Vlanif300
192.168.0.0/24Direct0 0 D 192.168.0.1 Vlanif21
192.168.0.1/32Direct0 0 D 127.0.0.1 Vlanif21
192.168.1.0/24Direct0 0 D 192.168.1.1 Vlanif300
192.168.1.1/32Direct0 0 D 127.0.0.1 Vlanif300
192.168.2.0/24Direct0 0 D 192.168.2.1 Vlanif300
<stackB>disversion
isplay interface brief
^
Error: Unrecognized command found at '^' position.
<stackB>ddisplay log
isversion
Huawei Versatile Routing Platform Software
VRP (R) software, Version 5.170 (S6720 V200R011C10SPC600)
Copyright (C) 2000-2018 HUAWEI TECH Co., Ltd.
HUAWEI S6720S-26Q-LI-24S-AC Routing Switch uptime is 6 weeks, 4 days, 8 hours, 13 minutes
ES5D2S26Q005 0(Master): uptime is 6 weeks, 4 days, 8 hours, 11 minutes
DDR Memory Size : 1024M bytes
FLASH Total Memory Size : 512 M bytes
FLASH Available Memory Size : 360 M bytes
Pcb Version : VER.A
BootROM Version : 020b.0001
BootLoad Version : 020b.0a05
CPLD Version : 0105
Software Version : VRP (R) Software, Version 5.170 (V200R011C10SPC600)
FLASH Version : 0x0
ES5D2S26Q005 1(Standby): uptime is 6 weeks, 4 days, 7 hours, 56 minutes
DDR Memory Size : 1024M bytes
FLASH Total Memory Size : 512 M bytes
FLASH Available Memory Size : 360 M bytes
Pcb Version : VER.A
BootROM Version : 020b.0001
BootLoad Version : 020b.0a05
CPLD Version : 0105
<stackB>quit
就是在一条命令下面紧接着又是一条命令,或者就是命令直接变成了两截。。很奇怪。
页:
[1]