Simple sample code for Python

I am a beginner and I am wondering if there is any sample python code to create and run the scan and get the result. In the below code, I just get the version, How I can make and run a task?

import gvm

#from gvm.protocols.latest import Gmp

from gvm.transforms import EtreeTransform

from gvm.xml import pretty_print

from gvm.protocols.gmpv9 import Gmp

connection = gvm.connections.TLSConnection(hostname='127.0.0.1')

gmp = Gmp(connection)

gmp.authenticate('root', 'admin')

# Retrieve current GMP version

version = gmp.get_version()

# Prints the XML in beautiful form

pretty_print(version)

When using https://github.com/greenbone/python-gvm you can also have a look at our GMP docs https://docs.greenbone.net/API/GMP/gmp-9.0.html to get an idea about what commands are available in GMP and how to use them. The commands you are looking for are create_task and start_task, I guess.

2 Likes

Thank you for your fast response, to create the task, I need to create a scanner, and scanner demands scanner_type. I tried a lot but I couldn’t make that. Unfortunately, there is no simple sample in Python to figure out how a scanner and task can be defined.

gmp.create_scanner( ‘test’, ‘127.0.0.1’, 9390, scanner_type=2 , credential_id=‘up’)

error: In create_scanner the argument scanner_type must be of type ScannerType.

You can use the default scanner that comes with GVM. try get_scanners to see it and a CVE scanner, respectively. What is needed to create a task is listed in https://docs.greenbone.net/API/GMP/gmp-9.0.html#command_create_task

I’m not sure, what you are trying to achieve, but if you just want to scan your local network and/or want to learn more about our vulnerability management, I would recommend to get started with the GCE (Greenbone Community Edition), a virtual machine that let’s you do scans quite easily and you’ll get to know how scanners, tasks, targets and so on are connected.
You can download the VM for free here: https://www.greenbone.net/en/community-edition/

Once you got to know the “inner workings” of GVM a bit better, you can switch to writing your own scripts and it will be a lot easier to understand what needs to be done.

2 Likes

Also there is a full api documentation at https://python-gvm.readthedocs.io/en/latest/

2 Likes

Sometimes it is hard to understand without seeing any sample, for example in the below code I want to make a new task and run it based on Steffen’s suggestion. I don’t know how to get task_id and pass it to start_task()

#Code
from gvm.connections import UnixSocketConnection
from gvm.protocols.gmp import Gmp
from gvm.transforms import EtreeTransform
from gvm.xml import pretty_print
import gvm

ip = ‘121.0.0.1’
connection =gvm.connections.TLSConnection(hostname=ip)
transform = EtreeTransform()
with Gmp(connection, transform=transform) as gmp:
# Login
gmp.authenticate(‘admin’, ‘admin’)
# Retrieve all tasks
tasks = gmp.get_tasks()
# Get names of tasks
task_names = tasks.xpath(‘task/name/text()’)
print(“task_ names \n”,task_names)
scanners = gmp.get_scanners()
print(“scanners \n”,scanners)
scanners_names = scanners.xpath(‘scan/name/text()’)
print(“scanners_names \n”,scanners_names)
task = gmp.create_task(“task1”,‘daba56c8-73ec-11df-a475-002264764cea’,ip,‘6acd0832-df90-11e4-b9d5-28d24461215b’)

need to get task id and start the task

For code examples you can take a look at the gvm-tools scripts. They use the same API

To get the id of a task you have to look at the GMP protocol doc and the response xml data. E.g. for create_task the new task id is return as the id attribute of the response.

2 Likes

Thanks bricks.

The create_task method does not seem to be documented - https://python-gvm.readthedocs.io/en/latest/search.html?q=create_task

Took me a while to realise it was there!

1 Like