I'm testing a python script to retrieve the software version from Cisco gear. The script works fine connecting to the remote device and obtaining the information required. The problem starts when I try to filter some unwanted information from pexpect output. The function's code in charge of getting the vesion information is as follows:
def get_version_info(session):
session.sendline('show version | include Version')
result = session.expect(['>', pexpect.TIMEOUT])
# Extract the 'version' part of the output
version_output_lines = session.before.splitlines()
version = version_output_lines[4].strip("'")
print("--- got version: ", version)
return version
The required information from the raw output after spliting lines (line 5 in code) was placed in the following list:
[b' ', b'', b'HOSTNAME#show version | include Version', b'Cisco IOS XE Software, Version 16.09.02', b'Cisco IOS Software [Fuji], ASR1000 Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.9.2, RELEASE SOFTWARE (fc4)', b'licensed under the GNU General Public License ("GPL") Version 2.0. The', b'software code licensed under GPL Version 2.0 is free software that comes', b'GPL code under the terms of GPL Version 2.0. For more details, see the', b'HOSTNAME#']
All I need from the above output is "Version 16.9.2", the rest needs to be stripped out.
I tried to isolate element 4 of the list (version_output_lines[4]) and use the strip function, but it keeps sending an error:
TypeError: a bytes-like object is required, not 'str'
So I'd like to know if you have a better idea on how to strip all the unwanted information from the list and only leave the Version info as mentioned before.
Thanks.