Creating a report ... cli works but python doesn't

the gvm-cli creation of a report.pdf works very well …

gvm-cli tls --hostname=scanner --xml ‘<get_reports report_id=“9129012f-b7f3-4f87-ba4e-135f0dfc7310” format_id=“c402cc3e-b531-11e1-9163-406186ea4fc5” details=“True”/>’ | grep -oP ‘(?<=</report_format>)[^<]+’ | base64 -d > report.pdf
file report.pdf
report.pdf: PDF document, version 1.5

but the python tools creating a shit

gvm-script tls --hostname scanner scripts/pdf-report.gmp.py 9129012f-b7f3-4f87-ba4e-135f0dfc7310 pdf-report

file pdf-report
pdf-report: data

any ideas? :slight_smile:

Could be possible that https://github.com/greenbone/python-gvm/pull/290 caused the breakage. Could you try to add the details=True parameter to https://github.com/greenbone/gvm-tools/blob/master/scripts/pdf-report.gmp.py#L56 ?

1 Like

It works now … The problem was only the wrong magic file number … thanks … @bricks

I have the problem again …

the original example give me a corrupt pdf

import sys
from base64 import b64decode
from pathlib import Path
import datetime
#from datetime import timedelta

def create_report(tname,reportid):
time1 = datetime.datetime.now()
timestamp=time1.strftime(’%Y-%m-%d’)
pdf_filename = tname + “-” + timestamp + “.pdf”
pdf_report_format_id = “c402cc3e-b531-11e1-9163-406186ea4fc5”
response = gmp.get_report(
report_id=reportid, report_format_id=pdf_report_format_id
)
report_element = response[0]
content = “”.join(report_element.itertext())
binary_base64_encoded_pdf = content.encode(‘ascii’)
binary_pdf = b64decode(binary_base64_encoded_pdf)
pdf_path = Path(pdf_filename).expanduser()
pdf_path.write_bytes(binary_pdf)
print('Done. PDF created: ’ + str(pdf_path))

task_name=sys.argv[4]
report_id=sys.argv[5]
create_report(task_name,report_id)

and with

gvm-cli tls --hostname=scanner --xml ‘<get_reports report_id=“9129012f-b7f3-4f87-ba4e-135f0dfc7310” format_id=“c402cc3e-b531-11e1-9163-406186ea4fc5” details=“True”/>’ | grep -oP ‘(?<=</report_format>)[^<]+’ | base64 -d > report.pdf

it works …

the problem happens not with all reports …

I am relatively sure that this line causes the issue. See https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.tail
But at the moment I don’t have a correct solution to get all text from the xml element.

1 Like

My workaround is an embended gvm-cli command in my python script - not nice, but it works :wink:

What is the eqavalent command in python to this? When I get this in python I can do the rest with regex …

gvm-cli tls --hostname=scanner --xml “<get_reports report_id=”$REPORTID" format_id="$FORMATID" details=“True”/>"

I have already replied to your comment in the other post.

The error in the script is now fixed from ‘creating a shit’.

2 Likes

Thank you very much … It works perfectly :slight_smile:

This is the adapted function that I use

import sys
from base64 import b64decode
from pathlib import Path
import datetime
import time

def create_report(tname,reportid):
time1 = datetime.datetime.now()
timestamp=time1.strftime(’%Y-%m-%d_%H_%M’)
pdf_filename = “/tmp/” + tname + “-” + timestamp + “.pdf”
pdf_report_format_id = “c402cc3e-b531-11e1-9163-406186ea4fc5”
response = gmp.get_report(report_id=report_id, report_format_id=pdf_report_format_id)
report_element = response.find(“report”)
content = report_element.find(“report_format”).tail
binary_base64_encoded_pdf = content.encode(‘ascii’)
binary_pdf = b64decode(binary_base64_encoded_pdf)
pdf_path = Path(pdf_filename).expanduser()
pdf_path.write_bytes(binary_pdf)
print(“PDF: " + pdf_filename + " created”)
return pdf_filename

Closing as a duplicate of PDF report with gvm-script error mentioned in: