[Tutor] ipconfig on a MacOS X

Pijus Virketis pijus@virketis.com
Thu Mar 20 23:18:02 2003


Dear all,

I need to check my computer's IP address in my script. In the terminal, 
I can do this just fine with "ipconfig getifaddr en0". When I try to 
make this call from Python, though, things get tricky:

1. If I use MacPython (the GUI version), then the function system() is 
neither in module "os" nor "mac". How can I execute the system call 
without system()?

2. If I use the terminal Python, then strangely enough system() is 
available in "os" module. This is what I get:

 >>> os.system("ipconfig getifaddr en0")
127.0.0.1	#my ip address here
0
 >>> ip = os.system("ipconfig getifaddr en0")
 >>> print(a)
0

Oops ... So I get the right output, but I cannot seem to be able to 
capture it in a variable, instead grabbing only what I presume is the 
exit code.

So, I would like to ask two questions: how do I grab the output in 
terminal Python? And, how do I accomplish the whole task in MacPython? 
How come there is this schizophrenic split between the two Pythons? (I 
guess that's a third question, we can all fondly remember the Spanish 
Inquisition at this point :)).

Thank you!

Pijus