Inconsistent output with write_channel and read_channel
See original GitHub issueThanks again for the help on https://github.com/ktbyers/netmiko/issues/1315
I am using write_channel
and read_channel
to parse the possible subcommands for all commands in a router.
I wrote this function below
def getCommandsList(input_cmd):
res = []
print('cmd = show {} ?'.format(input_cmd))
net_connect.write_channel("show {} ?".format(input_cmd))
time.sleep(0.5)
output = net_connect.read_channel()
output = output.split('\n')
for i in range(len(output)):
if i != 0 and i != len(output) - 1: # skip first and last since they are not commands
line = output[i]
line = line.lstrip()
cmd, description = line.split(' ', 1)
description = description.lstrip().replace('\r', '')
print(cmd, description)
res.append(str(cmd))
return res
and for a single run, it works great.
Input: 'aaa' (show aaa ?)
Output: ['ikegroup', 'locald', 'login', 'password-policy', 'sync', 'task', 'taskgroup', 'trace', 'userdb', 'usergroup']
However, if I run it continuously, it gives me a weird output
Input: 'aaa ikegroup' (show aaa ikegroup)
Error:
Traceback (most recent call last):
File "parse.py", line 50, in <module>
print(getCommandsList('aaa ikegroup'))
File "parse.py", line 35, in getCommandsList
cmd, description = line.split(' ', 1)
ValueError: need more than 1 value to unpack
and when I print out the output
, it gives me
[u'show aaa ikegroup ?\r', u'\r ^\r', u"% Invalid input detected at '^' marker.\r", u'RP/0/RP1/CPU0:FRETTA-1#show aaa show aaa ikegroup ']
So, it seems like the output is inconsistent.
I am trying to do it recursively, but if the output is inconsistent, it will be very hard to do it.
Any help?
===========MORE INFO================
I tried to do this just to see the pattern of output:
net_connect.write_channel("show {} ?".format('aaa'))
time.sleep(0.5)
output = net_connect.read_channel()
print(output)
print('-------------')
net_connect.write_channel("show {} ?".format('aaa'))
time.sleep(0.5)
output = net_connect.read_channel()
print(output)
print('-------------')
net_connect.write_channel("show {} ?".format('aaa'))
time.sleep(0.5)
output = net_connect.read_channel()
print(output)
print('-------------')
this shows
-bash-4.2# python parse2.py
show aaa ?
ikegroup Show local IKE group(s)
locald locald sub system(cisco-support)
login login sub system
password-policy Show all the password policies configured in the system
sync aaa_lib_sync sub system(cisco-support)
task Show task information
taskgroup Show all the local taskgroups configured in the system
trace Show trace data for AAA sub system
userdb Show all local users with the usergroups each belong to
usergroup Show all the local usergroups configured in the system
RP/0/RP1/CPU0:FRETTA-1#show aaa
-------------
show aaa ?
^
% Invalid input detected at '^' marker.
RP/0/RP1/CPU0:FRETTA-1#show aaa show aaa
-------------
show aaa ?
^
% Invalid input detected at '^' marker.
RP/0/RP1/CPU0:FRETTA-1#show aaa show aaa show aaa
-------------
So it seems like I need a way to completely clear my input when I use the next write_channel
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
@ktbyers Thanks again!
You will have to figure out what they translate to in terms of ASCII and then send that character code using
write_channel
.