Code Duplication    Length = 36-39 lines in 3 locations

scripts/start-alert-scan.gmp.py 1 location

@@ 147-185 (lines=39) @@
144
    alert_id = None
145
    alert = alert_object.xpath('alert')
146
147
    if len(alert) == 0:
148
        print("creating alert")
149
        gmp.create_alert(
150
            alert_name,
151
            event=gmp.types.AlertEvent.TASK_RUN_STATUS_CHANGED,
152
            event_data={"status": "Done"},
153
            condition=gmp.types.AlertCondition.ALWAYS,
154
            method=gmp.types.AlertMethod.EMAIL,
155
            method_data={
156
                """Task '$n': $e
157
158
After the event $e,
159
the following condition was met: $c
160
161
This email escalation is configured to attach report format '$r'.
162
Full details and other report formats are available on the scan engine.
163
164
$t
165
166
Note:
167
This email was sent to you as a configured security scan escalation.
168
Please contact your local system administrator if you think you
169
should not have received it.
170
""": "message",
171
                "2": "notice",
172
                sender_email: "from_address",
173
                "[OpenVAS-Manager] Task": "subject",
174
                "c402cc3e-b531-11e1-9163-406186ea4fc5": "notice_attach_format",
175
                recipient_email: "to_address",
176
            },
177
        )
178
179
        alert_object = gmp.get_alerts(filter='name=%s' % recipient_email)
180
        alert = alert_object.xpath('alert')
181
        alert_id = alert[0].get('id', 'no id found')
182
    else:
183
        alert_id = alert[0].get('id', 'no id found')
184
        if debug:
185
            print("alert_id: " + str(alert_id))
186
187
    return alert_id
188

scripts/start-multiple-alerts-scan.gmp.py 2 locations

@@ 147-183 (lines=37) @@
144
    alert_object = gmp.get_alerts(filter='name=%s' % alert_name)
145
    alert_id = None
146
    alert = alert_object.xpath('alert')
147
    if len(alert) == 0:
148
        gmp.create_alert(
149
            alert_name,
150
            event=gmp.types.AlertEvent.TASK_RUN_STATUS_CHANGED,
151
            event_data={'status': 'Done'},
152
            condition=gmp.types.AlertCondition.ALWAYS,
153
            method=gmp.types.AlertMethod.EMAIL,
154
            method_data={
155
                """Task '$n': $e
156
157
After the event $e,
158
the following condition was met: $c
159
160
This email escalation is configured to attach report format '$r'.
161
Full details and other report formats are available on the scan engine.
162
163
$t
164
165
Note:
166
This email was sent to you as a configured security scan escalation.
167
Please contact your local system administrator if you think you
168
should not have received it.
169
""": "message",
170
                "2": "notice",
171
                sender_email: "from_address",
172
                "[OpenVAS-Manager] Task": "subject",
173
                "c402cc3e-b531-11e1-9163-406186ea4fc5": "notice_attach_format",
174
                recipient_email: "to_address",
175
            },
176
        )
177
        alert_object = gmp.get_alerts(filter='name=%s' % recipient_email)
178
        alert = alert_object.xpath('alert')
179
        alert_id = alert[0].get('id', 'no id found')
180
    else:
181
        alert_id = alert[0].get('id', 'no id found')
182
        if debug:
183
            print("alert_id: %s" % str(alert_id))
184
185
    # second configurable alert name
186
    alert_name2 = "%s-2" % recipient_email
@@ 192-227 (lines=36) @@
189
    alert_object2 = gmp.get_alerts(filter='name=%s' % alert_name2)
190
    alert_id2 = None
191
    alert2 = alert_object2.xpath('alert')
192
    if len(alert2) == 0:
193
        gmp.create_alert(
194
            alert_name2,
195
            event=gmp.types.AlertEvent.TASK_RUN_STATUS_CHANGED,
196
            event_data={'status': 'Done'},
197
            condition=gmp.types.AlertCondition.ALWAYS,
198
            method=gmp.types.AlertMethod.EMAIL,
199
            method_data={
200
                """Task '$n': $e
201
202
After the event $e,
203
the following condition was met: $c
204
205
This email escalation is configured to attach report format '$r'.
206
Full details and other report formats are available on the scan engine.
207
208
$t
209
210
Note:
211
This email was sent to you as a configured security scan escalation.
212
Please contact your local system administrator if you think you
213
should not have received it.
214
""": "message",
215
                "2": "notice",
216
                sender_email: "from_address",
217
                "[OpenVAS-Manager] Task": "subject",
218
                recipient_email: "to_address",
219
            },
220
        )
221
        alert_object2 = gmp.get_alerts(filter='name=%s' % recipient_email)
222
        alert2 = alert_object2.xpath('alert')
223
        alert_id2 = alert2[0].get('id', 'no id found')
224
    else:
225
        alert_id2 = alert2[0].get('id', 'no id found')
226
        if debug:
227
            print("alert_id2: %s" % str(alert_id2))
228
229
    return (alert_id, alert_id2)
230