How to get the ip addresses of a nic

B

Bo Jacobsen

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

Rob Nikander

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

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)
 
B

Bo Jacobsen

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

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
 
J

Justin Dubs

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

Depending on your needs:
'192.168.0.18'

Justin Dubs
 
D

Donn Cave

Quoth Rob Nikander <[email protected]>:
| 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, (e-mail address removed)
 
B

Bo Jacobsen

Is there a simple way to get all the ip addresses of a nic, beyound
parsing
Depending on your needs:

'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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top