简体   繁体   中英

How to extract a specific line from the python output

I am trying to execute this curl command using python . It retrieves an output like below.

import subprocess

try:
    output=subprocess.check_output(["curl", "-v", "https://escortpersonaladz.com"], stderr=subprocess.STDOUT)
    print(output)
    if "expire date:" in str(output):
        print("Found") 
        print("Expiry date is: ") # I want to extract and print ONLY expiry date from the output
    else:
        print("Not found")
except:
    print("Error Occured")

this produces below output

* Rebuilt URL to: <dns>
*   Trying <ip>...
* TCP_NODELAY set
* Connected to escortpersonaladz.com (<ip>) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS Unknown, Certificate Status (22):
* TLSv1.3 (IN), TLS handshake, Unknown (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Client hello (1):
* TLSv1.3 (OUT), TLS Unknown, Certificate Status (22):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=webdisk.escortpersonaladz.com
*  start date: May  3 02:00:53 2020 GMT
*  expire date: Aug  1 02:00:53 2020 GMT
*  subjectAltName: host "escortpersonaladz.com" matched cert's "escortpersonaladz.com"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
.
.
.
Found
Expiry date is: 

How can I extract that particular line * expire date: Aug 1 02:00:53 2020 GMT from the above output and write it to a file, so that file would look like:

1         *  expire date: Aug  1 02:00:53 2020 GMT
2         *  expire date: Aug  1 02:00:53 2022 GMT
3         *  expire date: Aug  1 02:00:53 2020 GMT
.
.
.

Extract the lines using something this:

string = '''
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=webdisk.escortpersonaladz.com
*  start date: May  3 02:00:53 2020 GMT
*  expire date: Aug  1 02:00:53 2020 GMT
*  subjectAltName: host "escortpersonaladz.com" matched cert's "escortpersonaladz.com"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
'''
for line in string.split('\n'):
    if line[0:15] == '*  expire date:':
        print(line)
>>> *  expire date: Aug  1 02:00:53 2020 GMT

You can save these lines in a file to a variable and create a text file like:

text = ''
for line in string.split('\n'):
    if line[0:15] == '*  expire date:':
        text += line + '\n'

file = open('filename.txt', 'r+')
file.truncate(0)
file.seek(0)
file.write(text)
file.close()
print(text)

You can use regex to do that:

https://www.w3schools.com/python/python_regex.asp

x = re.sub(<Matching regex>, <The part you want>, output)
print(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM