Hi there,

Trying to write a small script in python. What it will basically do is this;


1- A SSH user initiates the python script (from SSH remotely)
2- The script gets the connected users IP (the user is connected through SSH)
3- The connected IP is sent back to the user


I got the part where the user initiates script (the easiest part so far)
I also got to write to a file and send the same file using SSH and Python

The part I need help is in the step 2

I am a first time Python programmer. Been using it for three days so be kind to me please ^_^

to get your computers ip, try

>>> import socket
>>> socket.gethostbyname(socket.gethostname())

to get your computers ip, try

>>> import socket
>>> socket.gethostbyname(socket.gethostname())

Doesn't this give the python-running machine's IP ?

Or does it give the IP of the machine that calls it?

to get your computers ip, try

>>> import socket
>>> socket.gethostbyname(socket.gethostname())

It just gave me 127.0.0.2 ?? which isn't really the ip i tried to run the python from

I know that 127.0.0.1 is localhost but what is 127.0.0.2 ?

Found a better way to solve my find external IP problem with Python. It was foolish of me trying to find it through SSH while there were so many other tools out there.

def getIPAddress(self):
        """
           This function gets your external ip
           from whatismyop.com
           this program must be called every
           5 minutes not more or else you must
           have your own server.
        """
        site = urllib.urlopen("http://www.whatismyip.com/automation/n09230945.asp").read()
        grab = re.findall('\d{2,3}.\d{2,3}.\d{2,3}.\d{2,3}',site)
        address = grab[0]
        return address

In the end the above code was used. Thanks for all your help anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.