Python Forum
NameError: name 'target_id' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'target_id' is not defined
#1
Hii,

I am using python 2.7 for running the python code. When I run the code I get an error as "NameError: name 'target_id' is not defined". Please help me solving this error. The Code is-


import subprocess
import re
import time
import os
import shlex

#change this to the IP address or subnet you want to be scanned
ip_address = "127.0.0.1"


#Use the configuration for the scan , the prefered on is full and fast
#config_id = "8715c877-47a0-438d-98a3-27c7a6ab2196"   #Discovery
#config_id = "085569ce-73ed-11df-83c3-002264764cea"  #empty
config_id = "daba56c8-73ec-11df-a475-002264764cea"  #Full and fast
#config_id = "698f691e-7489-11df-9d8c-002264764cea"  #Full and fast ultimate
#config_id = "708f25c4-7489-11df-8094-002264764cea"  #Full and very deep
#config_id = "74db13d6-7489-11df-91b9-002264764cea"  #Full and very deep ultimate
#config_id = "2d3f051c-55ba-11e3-bf43-406186ea4fc5"  #Host Discovery
#config_id = "bbca7412-a950-11e3-9109-406186ea4fc5"  #System Discovery


# Html format for the report
format_id = "6c248850-1f62-11e1-b082-406186ea4fc5"
# xml format
#format_id = "a994b278-1f62-11e1-96ac-406186ea4fc5"

# connect to openvas  and change password to admin
p = subprocess.Popen(["openvasmd", "--user=admin", "--new-password=admin"],
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, err = p.communicate()


# creates target
cmd = ["omp", "--username", "admin", "--password", "admin",
 "--xml=<create_target><name>Subnet %s </name><hosts> %s </hosts></create_target>" %(ip_address, ip_address)]
p = subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
output, err = p.communicate()
if "OK" in output:
    print "target was created, target id:"
    target_id_tuple = re.findall(r'\w+(?:-\w+)+', output)
    target_id = target_id_tuple[0]
    print target_id


#creates task
cmd = ["omp", "--username", "admin", "--password", "admin", 
    "--xml=<create_task><name>My scan</name><comment>Deep scan on Subnet %s </comment><config id=\"%s\"/><target id=\"%s\"/></create_task>" %(ip_address, config_id, target_id)]
p = subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
output, err = p.communicate()
if "OK" in output:
    print "task was created, task id is:"
    task_id_tuple = re.findall(r'\w+(?:-\w+)+', output)
    task_id = task_id_tuple[0]
    print task_id


#Now that the task for the scan is ready start it
cmd = ["omp", "--username", "admin", "--password", "admin", 
    "--xml=<start_task task_id=\"%s\"/>" %task_id]
p = subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
output, err = p.communicate()
if "OK" in output:
    print "Task has started report id is:"
    report_id_tuple = re.findall(r'\w+(?:-\w+)+', output)
    report_id = report_id_tuple[0]
    print report_id


#wait for the scan to finish
while True:
    time.sleep(60)
    cmd = ["omp", "--username", "admin", "--password", "admin", "-G"]
    p = subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
    output, err = p.communicate()
    if "Done" in output:
        time.sleep(15)
        print "task finished"
        
        cmd = "omp --username admin --password admin -D %s  " % task_id 
        args = shlex.split(cmd)
        p = subprocess.Popen(args, stdout=subprocess.PIPE)

        cmd = "omp --username admin --password admin --xml='<delete_target target_id=\"%s\"/>'" % target_id
        args = shlex.split(cmd)
        p = subprocess.Popen(args, stdout=subprocess.PIPE)
        break
    else:
        pass


time.sleep(5)
#use HTML format, grab the report and rename it with the current date
timestr = time.strftime("%Y%m%d")
cmd = "omp --username admin --password admin -R %s -f %s " %(report_id, format_id) 
time.sleep(5)
args = shlex.split(cmd)
time.sleep(5)
out = open("Report.html", "w+")
time.sleep(5)
p = subprocess.Popen(args, stdout=out)
time.sleep(5)
newname = 'Report_'+timestr+'.html' 
os.rename('Report.html', newname)
The Output is as follows:
Error:
Traceback (most recent call last): File "openvas.py", line 47, in <module> "--xml=<create_task><name>My scan</name><comment>Deep scan on Subnet %s </comment><config id=\"%s\"/><target id=\"%s\"/></create_task>" %(ip_address, config_id, target_id)] NameError: name 'target_id' is not defined
Reply
#2
Output would be nice. I see a lot of print statements. And wrap you code in python tags please. The error message indicates target_id is not defined. I see where target_id is defined, but something must be preventing that code from being reached. Is it an indentation error? I can't tell because without python tags the indentation is lost.
Reply
#3
I have updated the code using python tag. Please check and guide me solving this error.
Reply
#4
target_id is only assigned on line 41. But that line will not be run if the conditional on line 38 fails. If that happens, then the variable is not defined if it is needed on line 83.
Reply
#5
(Nov-03-2020, 05:43 AM)deanhystad Wrote: Output would be nice. I see a lot of print statements. And wrap you code in python tags please. The error message indicates target_id is not defined. I see where target_id is defined, but something must be preventing that code from being reached. Is it an indentation error? I can't tell because without python tags the indentation is lost.

What should i make change in code so that it works
Reply
#6
Please someone help, its urgent for doing my project
Reply
#7
The problem comes from create_target.
If the condition is not fulfilled, the target_id is not assigned.
You should update Python to 3.6 or later.
Python 3.5 and 2.7 are End of Life: https://devguide.python.org/#status-of-python-branches

If you put your code into function, it's later easier to test.
Here your function:

def create_target(ip_address):
    xml_data = (
        f"--xml=<create_target><name>Subnet {ip_address} "
        f"</name><hosts> {ip_address} </hosts></create_target>"
    )
    cmd = ["omp", "--username", "admin", "--password", "admin", xml_data]
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False, encoding="utf8")
    stdout, stderr = proc.communicate()
    if "OK" in stdout:
        print("target was created, target id:")
        target_id_tuple = re.findall(r'\w+(?:-\w+)+', stdout)
        target_id = target_id_tuple[0]
        print(target_id)
        return True
    elif "Failed to acquire socket." in stderr:
        print("omp service is not running")
    else:
        print(stdout, stderr)
        return False
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Thanks for your reply but the python script which I am using is referred from github and it compulsory requires python 2.7
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 152 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,056 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,771 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,198 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,457 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,863 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,179 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,667 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,298 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,843 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020