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)
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.
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.
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’)
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.