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
My code examples are always for Python >=3.6.0
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
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 153 Jul-26-2021, 04:36 PM
Last Post: snippsat
  I am getting a NameError that is not defined and not sure why it happen rick0922 5 402 Jun-14-2021, 03:41 PM
Last Post: deanhystad
  [split] NameError: name 'start_with_first_selected' is not defined knoxvilles_joker 4 604 Apr-18-2021, 07:59 PM
Last Post: knoxvilles_joker
  “NameError” “is not defined” script that I know works on other computers iamwithstupid 0 587 Nov-24-2020, 04:23 PM
Last Post: iamwithstupid
  NameError: name 'L2' is not defined soshelby 5 996 Nov-05-2020, 08:50 PM
Last Post: soshelby
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 1,039 Oct-05-2020, 07:03 PM
Last Post: buran
  Going thru tutorials..."NameError: name 'raw_input' is not defined" hmonnier 4 1,165 Jul-14-2020, 02:19 PM
Last Post: BitPythoner
  NameError: name 'pins' is not defined glennford49 2 1,623 Jun-15-2020, 12:20 PM
Last Post: glennford49
  python library not defined in user defined function johnEmScott 2 867 May-30-2020, 04:14 AM
Last Post: DT2000
  [split] NameError: name 'formats' is not defined shobhu123 1 1,097 May-24-2020, 06:29 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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