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='')

gmp = Gmp(connection)

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

# Retrieve current GMP version

version = gmp.get_version()

# Prints the XML in beautiful form


When using you can also have a look at our GMP docs 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.


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’, ‘’, 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

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:

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.


Also there is a full api documentation at


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()

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 = ‘’
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.


Thanks bricks.

The create_task method does not seem to be documented -

Took me a while to realise it was there!

1 Like