0

I am new to Python and having issues calling a variable from one func to use in another.

First function gets localhost IP, second function I would like to grab that IP and use it to port scan itself.

My open port function comes up with error 'AttributeError: 'function' object has no attribute 'ipv4''

Any help is greatly appreciated.

def get_IP():
ip = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip.connect(("8.8.8.8", 80))
print(ip.getsockname()[0])
ipv4 = ip.getsockname()[0]
ip.close()
return ipv4

def get_open_ports():
for port in range(1,65535):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket.setdefaulttimeout(1)
    result = s.connect_ex((get_IP.ipv4, port))
    if result ==0:
        print(f"Port {port} is open")
    s.close()
Brett
  • 3
  • 1
  • `result = s.connect_ex((get_IP(), port))`, you might want to have a look at the Python tutorial: https://docs.python.org/3/tutorial/controlflow.html#defining-functions – Maurice Meyer Dec 22 '21 at 12:29
  • Does this answer your question? [Calling variable defined inside one function from another function](https://stackoverflow.com/questions/10139866/calling-variable-defined-inside-one-function-from-another-function) – Tomerikoo Dec 22 '21 at 12:33
  • The function `get_IP` already returns the value of `ipv4`. So you just need to change `get_IP.ipv4` to `get_IP()`. i.e. call the function and get its returned value – Tomerikoo Dec 22 '21 at 12:34

1 Answers1

0

You can pass it as an argument to the second function using the syntax:

def get_open_ports(my_ip):

But you need to call the functions:

ip = get_IP()
open_ports = get_open_ports(ip)

Or even

open_ports = get_open_ports(get_IP())
Mat
  • 191
  • 7