By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,539 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,539 IT Pros & Developers. It's quick & easy.

How to get the ip addresses of a nic

P: n/a
Is there a simple way to get all the ip addresses of a nic, beyound parsing
/etc/sysconfig/.....

-------------------------------------------------
Bo Jacobsen

Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Bo Jacobsen wrote:
Is there a simple way to get all the ip addresses of a nic, beyound parsing
/etc/sysconfig/.....

-------------------------------------------------
Bo Jacobsen


I just had to do deal with this BS a couple days ago. I couldn't get
the socket module to give it to me. The only ways I found were parsing
the output of ifconfig (or equivalent on other platforms) or connecting
to server and then looking at your socket. I don't like either one.

Rob

# Method 1

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('somewebserver', 80))
return s.getsockname()[0]

# Method 2

if sys.platform == 'linux2':
out = commands.getoutput('/sbin/ifconfig')
return re.search('inet addr:(.+?)\s', out).group(1)
elif sys.platform == 'win32':
si, so = os.popen4('route print')
out = so.read()
ipPatt = r'\d+\.\d+\.\d+.\d+'
return re.search(r'0\.0\.0\.0\s+0\.0\.0\.0\s+' + ipPatt + r'\s+(' +
ipPatt + ')', out).group(1)
elif sys.platform == 'freebsd5':
out = commands.getoutput('/sbin/ifconfig')
return re.search('inet (\S+)\s', out).group(1)
else:
raise Exception('unsupported platform: %s' % sys.platform)

Jul 18 '05 #2

P: n/a
> > Is there a simple way to get all the ip addresses of a nic, beyound
parsing
/etc/sysconfig/.....

-------------------------------------------------
Bo Jacobsen


I just had to do deal with this BS a couple days ago. I couldn't get
the socket module to give it to me. The only ways I found were parsing
the output of ifconfig (or equivalent on other platforms) or connecting
to server and then looking at your socket. I don't like either one.

Rob

# Method 1

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('somewebserver', 80))
return s.getsockname()[0]

# Method 2

if sys.platform == 'linux2':
out = commands.getoutput('/sbin/ifconfig')
return re.search('inet addr:(.+?)\s', out).group(1)
elif sys.platform == 'win32':
si, so = os.popen4('route print')
out = so.read()
ipPatt = r'\d+\.\d+\.\d+.\d+'
return re.search(r'0\.0\.0\.0\s+0\.0\.0\.0\s+' + ipPatt + r'\s+(' +
ipPatt + ')', out).group(1)
elif sys.platform == 'freebsd5':
out = commands.getoutput('/sbin/ifconfig')
return re.search('inet (\S+)\s', out).group(1)
else:
raise Exception('unsupported platform: %s' % sys.platform)


And the parsing gets much more difficult with multiple nics and aliases.

Bo
Jul 18 '05 #3

P: n/a
"Bo Jacobsen" <bo@systemhouse.dk> wrote in message news:<LE**********************@news000.worldonline .dk>...
Is there a simple way to get all the ip addresses of a nic, beyound parsing
/etc/sysconfig/.....


Depending on your needs:
socket.gethostbyname(socket.gethostname())

'192.168.0.18'

Justin Dubs
Jul 18 '05 #4

P: n/a
Justin Dubs wrote:
socket.gethostbyname(socket.gethostname())
'192.168.0.18'

Justin Dubs

That always gives me this:
import socket
socket.gethostbyname(socket.gethostname())

'127.0.0.1'

Jul 18 '05 #5

P: n/a
Quoth Rob Nikander <rn*************@adelphia.net>:
| Justin Dubs wrote:
| >>>>socket.gethostbyname(socket.gethostname())
|>
|> '192.168.0.18'
|
|
| That always gives me this:
|
| >>> import socket
| >>> socket.gethostbyname(socket.gethostname())
| '127.0.0.1'

That isn't your address?

Try something like this -
t = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
t.connect(('my.dns.server', 53))
print t.getsockname()

Donn Cave, do**@drizzle.com
Jul 18 '05 #6

P: n/a
jt****@eos.ncsu.edu (Justin Dubs) wrote in message news:<2e*************************@posting.google.c om>...
"Bo Jacobsen" <bo@systemhouse.dk> wrote in message news:<LE**********************@news000.worldonline .dk>...
Is there a simple way to get all the ip addresses of a nic, beyound parsing
/etc/sysconfig/.....


Depending on your needs:
socket.gethostbyname(socket.gethostname())

'192.168.0.18'

Justin Dubs


Tearing apart socket.gethostbyname_ex(socket.gethostname()) might help.
Jul 18 '05 #7

P: n/a
> > Is there a simple way to get all the ip addresses of a nic, beyound
parsing
/etc/sysconfig/.....


Depending on your needs:
socket.gethostbyname(socket.gethostname())

'192.168.0.18'

Justin Dubs


I need to get addresses on hosts with mulitiple nics, and with multiple
aliases.

Trying to parse output from "$ip addr show" on those hosts are not easy.

Bo
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.