944,005 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2394
  • Python RSS
Oct 28th, 2009
0

Getting the connected user's ip in python

Expand Post »
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 ^_^
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster
johnroach1985 is offline Offline
135 posts
since Apr 2004
Oct 28th, 2009
0
Re: Getting the connected user's ip in python
to get your computers ip, try

python Syntax (Toggle Plain Text)
  1. >>> import socket
  2. >>> socket.gethostbyname(socket.gethostname())
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
fallopiano is offline Offline
68 posts
since Jun 2009
Oct 28th, 2009
0
Re: Getting the connected user's ip in python
Click to Expand / Collapse  Quote originally posted by fallopiano ...
to get your computers ip, try

python Syntax (Toggle Plain Text)
  1. >>> import socket
  2. >>> 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?
Reputation Points: 11
Solved Threads: 0
Junior Poster
johnroach1985 is offline Offline
135 posts
since Apr 2004
Oct 28th, 2009
0
Re: Getting the connected user's ip in python
Click to Expand / Collapse  Quote originally posted by fallopiano ...
to get your computers ip, try

python Syntax (Toggle Plain Text)
  1. >>> import socket
  2. >>> 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 ?
Reputation Points: 11
Solved Threads: 0
Junior Poster
johnroach1985 is offline Offline
135 posts
since Apr 2004
Oct 29th, 2009
0
Re: Getting the connected user's ip in python
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.
Python Syntax (Toggle Plain Text)
  1. def getIPAddress(self):
  2. """
  3. This function gets your external ip
  4. from whatismyop.com
  5. this program must be called every
  6. 5 minutes not more or else you must
  7. have your own server.
  8. """
  9. site = urllib.urlopen("http://www.whatismyip.com/automation/n09230945.asp").read()
  10. grab = re.findall('\d{2,3}.\d{2,3}.\d{2,3}.\d{2,3}',site)
  11. address = grab[0]
  12. return address

In the end the above code was used. Thanks for all your help anyway.
Reputation Points: 11
Solved Threads: 0
Junior Poster
johnroach1985 is offline Offline
135 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Binding DNA
Next Thread in Python Forum Timeline: noob question about strings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC