Getting the connected user's ip in python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2004
Posts: 129
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster

Getting the connected user's ip in python

 
0
  #1
Oct 28th, 2009
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 ^_^
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 48
Reputation: fallopiano is an unknown quantity at this point 
Solved Threads: 4
fallopiano's Avatar
fallopiano fallopiano is offline Offline
Light Poster
 
0
  #2
Oct 28th, 2009
to get your computers ip, try

  1. >>> import socket
  2. >>> socket.gethostbyname(socket.gethostname())
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 129
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster
 
0
  #3
Oct 28th, 2009
Originally Posted by fallopiano View Post
to get your computers ip, try

  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?
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 129
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster
 
0
  #4
Oct 28th, 2009
Originally Posted by fallopiano View Post
to get your computers ip, try

  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 ?
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 129
Reputation: johnroach1985 is an unknown quantity at this point 
Solved Threads: 0
johnroach1985's Avatar
johnroach1985 johnroach1985 is offline Offline
Junior Poster
 
0
  #5
Oct 29th, 2009
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.
  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.
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
Reply With Quote Quick reply to this message  
Reply

Tags
ip, python, remote, ssh

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC