Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with parsing strings!
#1
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!
Reply
#2
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
Reply
#3
(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.
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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]
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 770 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,777 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Finding multiple strings between the two same strings Slither 1 2,524 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Python file parsing, can't catch strings in new line Philia 5 3,970 Apr-29-2018, 01:09 PM
Last Post: snippsat
  lists, strings, and byte strings Skaperen 2 4,233 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020