Show current ip on Linux

Sibylle Koczian Sibylle.Koczian at Bibliothek.Uni-Augsburg.de
Tue Jun 14 15:21:20 CEST 2005


David Van Mosselbeen schrieb:
> 
> Thanks for support.
> I have read the refered page you show above. I try some piece of code that
> im have copy and paste it into a blank file that i give the name
> "ip_adress.py" to test it.
> 
> 
> THE SOURCE CODE :
> -----------------
> 
> import commands
> 
> ifconfig = '/sbin/ifconfig'
> # name of ethernet interface
> iface = 'eth0'
> # text just before inet address in ifconfig output
> telltale = 'inet addr:'
> 
> def my_addr():
>     cmd = '%s %s' % (ifconfig, iface)
>     output = commands.getoutput(cmd)
> 
>     inet = output.find(telltale)
>     if inet >= 0:
>         start = inet + len(telltale)
>         end = output.find(' ', start)
>         addr = output[start:end]
>     else:
>         addr = ''
> 
>     return addr
> # End python code
> 
> But now, it's fine to have some piece of code but this wil not work on my
> computer. I'm sure that y do somethings bad.
> To run the python script on a Linux machine. How to proceed it ?
> 
> 1) I have open a terminal
> 2) then i type "python ip_adress.py" (to run the script)
> 
> But nothings, i not view the current ip of my computer.
> What happend ?
> 

You have defined a function, but you never call this function. Running a 
script won't do anything, if this script consists only of function (or 
class) definitions.

You can either

- open the interactive python interpreter and type:
import ip_adress
ip_adress.my_addr()

or, probably simpler,

- add the following two lines to your script, after the definition of 
your function:

if __name__ == '__main__':
     print my_addr()

Then run your script just as you did before. Now you are calling the 
function and printing the value it returns.

-- 
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : Sibylle.Koczian at Bibliothek.Uni-Augsburg.DE



More information about the Python-list mailing list