Getting local IP address...

Tony Johnson gjohnson at gs.verio.net
Mon Jul 17 15:17:02 EDT 2000


Yes I understand that os.system is to be used with extreme caution.  The 
ifconfig eth0 command does nothing but query information already stored and 
saved.  If someone hacked your unix box, they could just do an ifconfig 
eth0 instead of using your python sctipt.  There is also no need for keys 
or passwords considering nslookup/dig does this already...

I don't see any probs...

On Monday, July 17, 2000 1:12 PM, Aaron Rhodes [SMTP:arhodes at psionic.com] 
wrote:
>
> Howdy...
>
> That works too.  There are some purists
> who may take offense to reliance on external system binaries
> (ifconfig and grep)...not a big deal in my opinion.
> ifconfig on Linux probably does something similar
> to the fcntl/ioctl stuff I was mentioning...perhaps there
> should be a platform independent python-ifconfig library?
>
> The API to such a library could be constructed to provide
> a standard way to do "ifconfig-ish" stuff on
> Windows/NT/Unices. (i.e. up and down network interfaces,
> get the ip address off the net interface, set the ip on the
> net interface, change firewall rules perhaps, etc.)
>
> Aaron
> arhodes at psionic.com
>
>
> Tony Johnson wrote:
> >
> > Why not just do an addr = os.system(ifconfig eth0 | grep inet)
> > This should return the ip addy of the nic...
> >
> > Since the way OS's store IP addys is so implementaion dependant, I 
don't
> > see how it could be portable unless people start making a posix way of
> > storing an ip addy on top of the os dependant way
> >
> > On Monday, July 17, 2000 11:33 AM, Aaron Rhodes 
[SMTP:arhodes at psionic.com]
> > wrote:
> > > Here's a tremendously non-portable way to do it for
> > > Linux by reading it straight off the ethernet card...
> > >
> > > import string,socket,fcntl
> > > SIOCGIFADDR = 0x8915
> > > ifname = 'eth0'
> > > s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
> > > ifr = ifname+'\0'*(16-len(ifname))+chr(socket.AF_INET)+15*'\0'
> > > r= fcntl.ioctl(s.fileno(),SIOCGIFADDR,ifr)
> > > addr = string.join(map(str,map(ord,r[20:24])),'.')
> > > print addr
> > >
> > >






More information about the Python-list mailing list