Skip to content

Commit 50ed659

Browse files
committed
docs: add simple USAGE.md guide for getting started with python-gvm
Covers installation, all three connection types, GMP/OSP usage, transforms, error handling, and debugging with short code examples. https://claude.ai/code/session_01D8fVeN4YVfAbzr3cXG4ZVA
1 parent 6145f65 commit 50ed659

1 file changed

Lines changed: 205 additions & 0 deletions

File tree

USAGE.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# How to Use python-gvm
2+
3+
**python-gvm** lets you control a Greenbone Vulnerability Management (GVM) server from Python using the GMP or OSP protocols.
4+
5+
---
6+
7+
## Installation
8+
9+
```bash
10+
python3 -m pip install python-gvm
11+
```
12+
13+
---
14+
15+
## Connection Types
16+
17+
Pick the connection type that matches your setup:
18+
19+
| Type | When to use |
20+
|------|-------------|
21+
| `UnixSocketConnection` | gvmd runs on the same machine (fastest) |
22+
| `TLSConnection` | Remote server over a secure TCP connection |
23+
| `SSHConnection` | Remote server through an SSH tunnel |
24+
25+
---
26+
27+
## GMP – Greenbone Management Protocol
28+
29+
### 1. Simple request (no authentication)
30+
31+
```python
32+
from gvm.connections import UnixSocketConnection
33+
from gvm.protocols.gmp import GMP
34+
35+
connection = UnixSocketConnection(path='/run/gvmd/gvmd.sock')
36+
37+
with GMP(connection=connection) as gmp:
38+
print(gmp.get_version())
39+
```
40+
41+
Expected output:
42+
```xml
43+
<get_version_response status="200" status_text="OK"><version>22.4</version></get_version_response>
44+
```
45+
46+
---
47+
48+
### 2. Authenticated request
49+
50+
Most GMP commands require authentication. Use `EtreeCheckCommandTransform` to get
51+
Python-parseable XML objects and automatic error raising on bad responses.
52+
53+
```python
54+
from gvm.connections import UnixSocketConnection
55+
from gvm.errors import GvmError
56+
from gvm.protocols.gmp import GMP
57+
from gvm.transforms import EtreeCheckCommandTransform
58+
59+
connection = UnixSocketConnection(path='/run/gvmd/gvmd.sock')
60+
transform = EtreeCheckCommandTransform()
61+
62+
try:
63+
with GMP(connection=connection, transform=transform) as gmp:
64+
gmp.authenticate('admin', 'password')
65+
66+
# Get all tasks whose name contains "weekly"
67+
tasks = gmp.get_tasks(filter_string='name~weekly')
68+
69+
for task in tasks.xpath('task'):
70+
print(task.find('name').text)
71+
72+
except GvmError as e:
73+
print(f'Error: {e}')
74+
```
75+
76+
---
77+
78+
### 3. Remote connections
79+
80+
**TLS (TCP + certificate)**:
81+
82+
```python
83+
from gvm.connections import TLSConnection
84+
85+
connection = TLSConnection(
86+
hostname='192.168.1.100',
87+
port=9390,
88+
certfile='/path/to/client.crt',
89+
keyfile='/path/to/client.key',
90+
cafile='/path/to/ca.crt',
91+
)
92+
```
93+
94+
**SSH tunnel**:
95+
96+
```python
97+
from gvm.connections import SSHConnection
98+
99+
connection = SSHConnection(
100+
hostname='192.168.1.100',
101+
port=22,
102+
username='gmp',
103+
password='secret',
104+
)
105+
```
106+
107+
Both can be used as a drop-in replacement for `UnixSocketConnection` in any of the examples above.
108+
109+
---
110+
111+
## OSP – Open Scanner Protocol
112+
113+
```python
114+
from gvm.connections import UnixSocketConnection
115+
from gvm.protocols.latest import Osp
116+
117+
connection = UnixSocketConnection(path='/var/run/ospd-wrapper.sock')
118+
osp = Osp(connection=connection)
119+
120+
with osp:
121+
print(osp.get_version())
122+
print(osp.get_scans())
123+
```
124+
125+
---
126+
127+
## Response transforms
128+
129+
By default every method returns a raw UTF-8 string. Two transforms are available:
130+
131+
| Transform | Returns |
132+
|-----------|---------|
133+
| `EtreeTransform` | `lxml.etree` Element (parse freely, no error check) |
134+
| `EtreeCheckCommandTransform` | `lxml.etree` Element + raises `GvmError` on failure |
135+
136+
```python
137+
from gvm.transforms import EtreeTransform
138+
from gvm.xml import pretty_print
139+
140+
transform = EtreeTransform()
141+
142+
with GMP(connection=connection, transform=transform) as gmp:
143+
version = gmp.get_version()
144+
pretty_print(version) # nicely formatted XML output
145+
```
146+
147+
---
148+
149+
## Error handling
150+
151+
All exceptions inherit from `gvm.errors.GvmError`:
152+
153+
| Exception | Cause |
154+
|-----------|-------|
155+
| `GvmClientError` | Bad request sent by the client |
156+
| `GvmServerError` | Server returned an error status |
157+
| `InvalidArgument` | A parameter has an invalid value |
158+
| `RequiredArgument` | A required parameter is missing |
159+
160+
```python
161+
from gvm.errors import GvmError, InvalidArgument
162+
163+
try:
164+
with GMP(connection=connection, transform=transform) as gmp:
165+
gmp.authenticate('admin', 'password')
166+
gmp.get_task(task_id='invalid-id')
167+
except InvalidArgument as e:
168+
print(f'Bad argument: {e}')
169+
except GvmError as e:
170+
print(f'GVM error: {e}')
171+
```
172+
173+
---
174+
175+
## Debugging
176+
177+
Log all sent/received data to a file:
178+
179+
```python
180+
import logging
181+
from gvm.connections import UnixSocketConnection, DebugConnection
182+
from gvm.protocols.gmp import GMP
183+
184+
logging.basicConfig(filename='gvm_debug.log', level=logging.DEBUG)
185+
186+
connection = DebugConnection(UnixSocketConnection(path='/run/gvmd/gvmd.sock'))
187+
188+
with GMP(connection=connection) as gmp:
189+
gmp.get_version()
190+
```
191+
192+
The log file will contain each raw command and response:
193+
194+
```
195+
DEBUG:gvm.connections:Sending 14 characters. Data <get_version/>
196+
DEBUG:gvm.connections:Read 97 characters. Data <get_version_response status="200" ...>
197+
```
198+
199+
---
200+
201+
## More information
202+
203+
- Full API reference: <https://greenbone.github.io/python-gvm/>
204+
- Community forum: <https://forum.greenbone.net/>
205+
- Issue tracker: <https://github.com/greenbone/python-gvm/issues>

0 commit comments

Comments
 (0)