0

I receive error while trying return value from a function say's dg isn't defined can you tell what's wrong ?

ipl = socket.gethostbyname(socket.gethostname())
seg1,seg2,seg3,seg4=ipl.split(".")
ip2 = seg1+"."+seg2+"."+seg3+"."
ip3 = seg1+"."+seg2+"."+seg3+"."


def getDGW(ip3):
    cmd = 'ipconfig'
    p = subprocess.Popen(cmd , stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    for line in p.stdout:
        x = re.findall('Default Gateway',str(line))
        if x:
            regex = ip3 + r'[0-9]+'
            line = re.search(regex, str(line))
            if line:
                print(line.group(0))
                dg = line.group(0)
                return (dg)

getDGW(ip3)

print(dg)

1 Answer 1

1

Your issue is not in returning dg, but in the statement print(dg). You want to have print(getDGW(ip3)) or

dg = getDGW(ip3)
print(dg)

dg is an undefined variable outside of the scope of the function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.