Posts: 221
Threads: 71
Joined: Dec 2017
Hi All,
I am not able to print multiple variable output on single line.
import telnetlib
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.read_until(">")
telnet.write("exit\n")
output = telnet.read_all()
for line in output.split('\n'):
if 'Version' in line:
Version = line.split(',')[1]
if 'image' in line:
Image = line.split(' ')[4]
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]
print HOST+" "+Version+" "+Image+" "+ProcessorID+" "+MACAddress+" "+Model+" "+SystemSerialNumber
print('{} {} {} {} {} {} {}'.format(HOST,Version,Image,ProcessorID,MACAddress,Model,SystemSerialNumber)) Output: FOC0835X50K4-EI0ion 12.1(22)EA12 "flash:c2950-i6q4l2-mz.121-22.EA12.bin"
FOC0835X50K4-EI0ion 12.1(22)EA12 "flash:c2950-i6q4l2-mz.121-22.EA12.bin"
what could be the issue?
Posts: 3,458
Threads: 101
Joined: Sep 2016
(May-15-2018, 05:52 PM)anna Wrote: Output: FOC0835X50K4-EI0ion 12.1(22)EA12 "flash:c2950-i6q4l2-mz.121-22.EA12.bin" FOC0835X50K4-EI0ion 12.1(22)EA12 "flash:c2950-i6q4l2-mz.121-22.EA12.bin"
So that's what you have now. What do you want it to be?
Posts: 221
Threads: 71
Joined: Dec 2017
Hi Nilamo,
don;t why its not printing all
print('{} {} {} {} {} {} {}'.format(HOST,Version,Image,ProcessorID,MACAddress,Model,SystemSerialNumber)) its, not printing, HOST, ProcessorID and Version is overlapping, not printing Model,SystemSerialNumber
Posts: 116
Threads: 1
Joined: Apr 2018
(May-15-2018, 05:52 PM)anna Wrote: if 'System serial number' in line:
SystemSerialNumber = line.split(':')[1]
print HOST+" "+Version+" "+Image+" "+ProcessorID+" "+MACAddress+" "+Model+" "+SystemSerialNumber
print('{} {} {} {} {} {} {}'.format(HOST,Version,Image,ProcessorID,MACAddress,Model,SystemSerialNumber)) Notice that this only prints if 'System serial number' is in the line you are processing.
I suspect you want to process all the lines and then print the info:
Version = 'Invalid'
Image = 'Invalid'
ProcessorID = 'Invalid'
MACAddress = 'Invalid'
Model = 'Invalid'
SystemSerialNumber = 'Invalid'
for line in output.split('\n'):
if 'Version' in line:
Version = line.split(',')[1]
if 'image' in line:
Image = line.split(' ')[4]
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]
print HOST+" "+Version+" "+Image+" "+ProcessorID+" "+MACAddress+" "+Model+" "+SystemSerialNumber
print('{} {} {} {} {} {} {}'.format(HOST, Version, Image, ProcessorID, MACAddress, Model, SystemSerialNumber)) In this way all the data is collected before printing and if any of the fields is not found will be reported as "Invalid". You can change this canary method to detect if any of the fields was missing and raise an error.
Posts: 221
Threads: 71
Joined: Dec 2017
May-15-2018, 07:43 PM
(This post was last modified: May-15-2018, 07:51 PM by buran.)
Hi killerrex,
Modified code but not printing all output. Please find the below full code
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 = 'Invalid'
Image = 'Invalid'
ProcessorID = 'Invalid'
MACAddress = 'Invalid'
Model = 'Invalid'
SystemSerialNumber = 'Invalid'
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]
print('{} {} {} {} {} {} {} {} {} {} {}'.format(HOST,Version,Image,ProcessorID,MACAddress,Model,SystemSerialNumber,NTPsyncStatus,NTPAssociationDelay,SNMPlogging,SNMPAgentStatus)) Output: synchronized 377 enabled enabled2 c2950-i6q4l2-mz.121-22.EA12.bin FOC0835X50K
expecting output as below
Quote:10.10.1.10 12.1(22)EA12 c2950-i6q4l2-mz.121-22.EA12.bin FOC0835X50K 00:12:00:DD:AD:40 WS-C2950G-24-EI FOC0835X50K synchronized 377 enabled enabled
sorry... for not proper tagging
Hi killerrex,
Modified code but not printing all output. Please find the below full code
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 = 'Invalid'
Image = 'Invalid'
ProcessorID = 'Invalid'
MACAddress = 'Invalid'
Model = 'Invalid'
SystemSerialNumber = 'Invalid'
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]
print('{} {} {} {} {} {} {} {} {} {} {}'.format(HOST,Version,Image,ProcessorID,MACAddress,Model,SystemSerialNumber,NTPsyncStatus,NTPAssociationDelay,SNMPlogging,SNMPAgentStatus))
Posts: 116
Threads: 1
Joined: Apr 2018
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)
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.
Posts: 221
Threads: 71
Joined: Dec 2017
Hi killerrex,
I am trying to print, highlighted details on single line.
[inline]Cisco Internetwork Operating System Software
IOS C2950 Software (C2950-I6Q4L2-M), Version 12.1(22)EA12, RELEASE SOFTWARE (fc1)
Copyright © 1986-2008 by cisco Systems, Inc.
Compiled Mon 07-Jul-08 23:39 by amvarma
Image text-base: 0x80010000, data-base: 0x80570000
ROM: Bootstrap program is C2950 boot loader
pu-dig-dig-r1-as01-bs01 uptime is 44 weeks, 4 days, 1 hour, 35 minutes
System returned to ROM by power-on
System restarted at 11:36:41 IST Sat Jul 8 2017
System image file is "flash: c2950-i6q4l2-mz.121-22.EA12.bin"
cisco WS-C2950G-24-EI (RC32300) processor (revision K0) with 20957K bytes of memory.
Processor board ID FOC0835X50K
Last reset from system-reset
Running Enhanced Image
24 FastEthernet/IEEE 802.3 interface(s)
2 Gigabit Ethernet/IEEE 802.3 interface(s)
32K bytes of flash-simulated non-volatile configuration memory.
Base ethernet MAC Address: 00:12:00:DD:AD:40
Motherboard assembly number: 73-7280-05
Power supply part number: 34-0965-01
Motherboard serial number: FOC083530PN
Power supply serial number: DAB0834E60P
Model revision number: K0
Motherboard revision number: A0
Model number: WS-C2950G-24-EI
System serial number: FOC0835X50K
Configuration register is 0xF
Clock is synchronized, stratum 7, reference is 172.31.6.220
nominal freq is 250.0000 Hz, actual freq is 249.9997 Hz, precision is 2**18
reference time is DEA65FC4.F02039BC (13:14:04.937 IST Wed May 16 2018)
clock offset is -0.0238 msec, root delay is 1.88 msec
root dispersion is 11.87 msec, peer dispersion is 0.02 msec
Chassis: FOC0835X50K
364028 SNMP packets input
0 Bad SNMP version errors
49 Unknown community name
0 Illegal operation for community name supplied
0 Encoding errors
368327 Number of requested variables
0 Number of altered variables
346055 Get-request PDUs
15450 Get-next PDUs
0 Set-request PDUs
370442 SNMP packets output
0 Too big errors (Maximum packet size 1500)
37 No such name errors
0 Bad values errors
0 General errors
363976 Response PDUs
6463 Trap PDUs
SNMP global trap: disabled
SNMP logging: enabled
Logging to 172.31.6.102.162, 0/10, 6463 sent, 0 dropped.
SNMP agent enabled
address ref clock st when poll reach delay offset disp
*~172.31.6.220 127.127.1.0 6 12 64 377 1.9 0.00 0.0
* master (synced), # master (unsynced), + selected, - candidate, ~ configured
[/inline]
Posts: 221
Threads: 71
Joined: Dec 2017
killerrex
its working now, now going for Multiple threading, as device count is high
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()
for line in Veroutput.split('\n'):
if 'Version' in line:
Version = (line.split(',')[1]).split(' ')[2]
elif 'image' in line:
Raw_Image = line.split(':')[1][:-1]
Image = Raw_Image[:-1]
elif 'Processor board ID' in line:
ProcessorID = (line.split(' ')[3]).split('\r')[0]
elif 'Model number' in line:
Model = (line.split(':')[1]).split('\r')[0]
elif 'MAC Address' in line:
MACAddress = (line.split(' ')[4]).split('\r')[0]
elif 'System serial number' in line:
SystemSerialNumber = (line.split(':')[1]).split('\r')[0]
elif 'Clock' in line:
NTPStatus = line.split(',')[0]
NTPsyncStatus = NTPStatus.split(' ')[2]
elif '*~' in line:
NTPAssociationDelay = re.sub('\s{2,}', ' ', line).split(' ')[5]
elif 'logging:' in line:
SNMPlogging = line.split(':')[1].strip()
elif 'agent' in line:
SNMPAgentStatus = (line.split(' ')[2]).split('\r')[0]
# Now that all the lines are read, report the result
print('{} {} {} {} {} {} {} {} {} {} {}'.format(
HOST, Version, Image, ProcessorID, MACAddress, Model,
SystemSerialNumber, NTPsyncStatus, NTPAssociationDelay,
SNMPlogging, SNMPAgentStatus)) Output: 10.10.1.10 12.1(22)EA12 c2950-i6q4l2-mz.121-22.EA12.bin FOC0835X50K 00:12:00:DD:AD:40 WS-C2950G-24-EI FOC0835X50K synchronized 377 enabled enabled
|