Python Forum
Help with parsing strings! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with parsing strings! (/thread-1634.html)



Help with parsing strings! - Cenzoooo - Jan-17-2017

Hello Python community!

I have problems with simple script which i created for gathering specific data from routers, so I would appreciate your help!

My script should do next:

1. SSH to router and type command "show system licence"
2. Output of that command is in few rows and columns, but I need to take specific part of string and print it out

I stucked at part 2, here is my code: 
import paramiko,string

JUNuser = 'xxx'
JUNpass = 'xxx'
JUNhost = 'xxx'

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(JUNhost, username=JUNuser, password=JUNpass)
stdin, stdout, stderr = ssh.exec_command("show system license")

output = stdout.readlines()
type(output)
final = output[3:4]
print '\n'.join(final) 
With this i managed to get specific row from original output.
Original output looks like:
Output:
License usage:                                   Licenses           Licenses           Licenses            Expiry   Feature name             used               installed           needed    scale-subscriber               0                 1000                  0                41 days
With part "final = output[3:4]" i managed to get:
Output:
scale-subscriber                      0         1000           0    41 days
What I want to achieve is to tell my script to get only this number of days when licence expire. So I want my script to show me only number 41!

Do you guys have idea how to do that? I am python beginner so be nice to me :)

Best Regards!


RE: Help with parsing strings! - snippsat - Jan-17-2017

Quote:So I want my script to show me only number 41!

>>> s = 'scale-subscriber                      0         1000           0    41 days'
>>> s
'scale-subscriber                      0         1000           0    41 days'
>>> s.split()
['scale-subscriber', '0', '1000', '0', '41', 'days']
>>> s.split()[-2]
'41'
>>> int(s.split()[-2])
41
Use code as i have added for you now Wink


RE: Help with parsing strings! - Cenzoooo - Jan-18-2017

(Jan-17-2017, 04:18 PM)snippsat Wrote:
Quote:So I want my script to show me only number 41!

>>> s = 'scale-subscriber                      0         1000           0    41 days'
>>> s
'scale-subscriber                      0         1000           0    41 days'
>>> s.split()
['scale-subscriber', '0', '1000', '0', '41', 'days']
>>> s.split()[-2]
'41'
>>> int(s.split()[-2])
41
Use code as i have added for you now Wink


Yeah, but if I put this that way it will show me number 41, what about next day when expiry date will be 40 days? It should dinamically read numbers and give me output how many days have left.



RE: Help with parsing strings! - wavic - Jan-18-2017

Learn the basics of Python! The script takes the position where '41' appears in the output. If this doesn't change it will print what you want


RE: Help with parsing strings! - Cenzoooo - Jan-18-2017

(Jan-18-2017, 07:27 AM)wavic Wrote: Learn the basics of Python! The script takes the position where '41' appears in the output. If this doesn't change it will print what you want

Yeah, I understand now... Thank you all for help, in the mid time I solved it myself in a different way also!

import paramiko,string

JUNuser = 'xxx'
JUNpass = 'xxx'
JUNhost = 'xxxhr'

ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(JUNhost, username=JUNuser, password=JUNpass)
stdin, stdout, stderr = ssh.exec_command("show system license")
final = stdout.readlines()
final2 = final[3:4]
final3 = [int(i.split()[-2]) for i in final2]
print final3[0]


RE: Help with parsing strings! - buran - Jan-18-2017

well, it's nice you try list comprehension
final3 = [int(i.split()[-2]) for i in final2]
however, note that this will produce list with 6 elements all of which will be 41 and after that you need to get an element from this list. i.e. it's not necessary to add extra step in the process when basically you do the same what snippsat suggested.