May-15-2018, 11:12 PM
There is something strange with your indent... get sure you are using soft-tabs with 4 spaces in your editor.
Anyway, I try to collect everything in a single code (I cannot test it as I have no access to your test machine)
Nevertheless your output is strange because it does not start with the HOST value and that one is for sure equal to 10.10.1.10 (is hardcoded in your script)
I suspect is not the output of your print command, maybe some trace in your script or a log written in the stdout by telnetlib.
Anyway, I try to collect everything in a single code (I cannot test it as I have no access to your test machine)
import telnetlib import re HOST = "10.10.1.10" user = "test" password = "test@123" telnet = telnetlib.Telnet(HOST) #telnet.set_debuglevel(2) telnet.read_until("Username: ",3) telnet.write(user + "\n") if password: telnet.read_until("Password:",3) telnet.write(password + "\n") telnet.write("term len 0 \n") telnet.read_until(">") telnet.write("show ver \n") telnet.write("Show ntp status \n") telnet.write("Show ntp association\n") telnet.write("show snmp \n") telnet.read_until(">",10) telnet.write("exit\n") Veroutput = telnet.read_all() Version = '<Missing>' Image = '<Missing>' ProcessorID = '<Missing>' MACAddress = '<Missing>' Model = '<Missing>' SystemSerialNumber = '<Missing>' NTPStatus = '<Missing>' NTPsyncStatus = '<Missing>' NTPAssociationDelay = '<Missing>' SNMPlogging = '<Missing>' SNMPAgentStatus = '<Missing>' for line in Veroutput.split('\n'): if 'Version' in line: Version = line.split(',')[1] if 'image' in line: Raw_Image = line.split(':')[1][:-1] Image = Raw_Image[:-1] if 'Processor board ID' in line: ProcessorID = line.split(' ')[3] if 'MAC Address' in line: MACAddress = line.split(' ')[4] if 'Model number' in line: Model = line.split(':')[1] if 'System serial number' in line: SystemSerialNumber = line.split(':')[1] if 'Clock' in line: NTPStatus = line.split(',')[0] NTPsyncStatus = NTPStatus.split(' ')[2] if '*~' in line: NTPAssociationDelay = re.sub('\s{2,}', ' ', line).split(' ')[5] if 'logging:' in line: SNMPlogging = line.split(':')[1].strip() if 'agent' in line: SNMPAgentStatus = line.split(' ')[2] # Now that all the lines are read, report the result print('{} {} {} {} {} {} {} {} {} {} {}'.format( HOST, Version, Image, ProcessorID, MACAddress, Model, SystemSerialNumber, NTPsyncStatus, NTPAssociationDelay, SNMPlogging, SNMPAgentStatus))Notice that the print is outside of the for loop. Any missing data will appear as <Missing>.
Nevertheless your output is strange because it does not start with the HOST value and that one is for sure equal to 10.10.1.10 (is hardcoded in your script)
I suspect is not the output of your print command, maybe some trace in your script or a log written in the stdout by telnetlib.