When an error occurs in a Python script on Unix , an email is sent.
I have been asked to add {Testing Environment} to the subject line of the email if the IP address is 192.168.100.37 which is the testing server. This way we can have one version of a script and a way to tell if the email is coming from messed up data on the testing server.
However, when I google I keep finding this code:
import socket
socket.gethostbyname(socket.gethostname())
However, that's giving me the IP address of 127.0.1.1. When I use ifconfig
I get this
eth0 Link encap:Ethernet HWaddr 00:1c:c4:2c:c8:3e
inet addr:192.168.100.37 Bcast:192.168.100.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:75760697 errors:0 dropped:411180 overruns:0 frame:0
TX packets:23166399 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:59525958247 (59.5 GB) TX bytes:10142130096 (10.1 GB)
Interrupt:19 Memory:f0500000-f0520000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:25573544 errors:0 dropped:0 overruns:0 frame:0
TX packets:25573544 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:44531490070 (44.5 GB) TX bytes:44531490070 (44.5 GB)
Firstly, I don't know where it got 127.0.1.1 from, but either way that's not what I want. When I google I keep coming to the same syntax, Bash scripts or netifaces and I'm trying to use standard libraries.
So how can I get the IP address of eth0 in Python?
Since most of the answers use
ifconfig
to extract the IPv4 from the eth0 interface, which is deprecated in favor ofip addr
, the following code could be used instead:UPDATE:
Alternatively, you can shift part of the parsing task to the python interpreter by using
split()
instead of grep and awk, as @serg points out in the comment:But in this case you have to check the bounds of the array returned by each
split()
call.UPDATE 2:
Another version using regex:
try below code, it works for me in Mac10.10.2:
It worked for me
Two methods:
Method #1 (use external package)
You need to ask for the IP address that is bound to your
eth0
interface. This is available from the netifaces packageYou can also get a list of all available interfaces via
Method #2 (no external package)
Here's a way to get the IP address without using a python package:
Note: detecting the IP address to determine what environment you are using is quite a hack. Almost all frameworks provide a very simple way to set/modify an environment variable to indicate the current environment. Try and take a look at your documentation for this. It should be as simple as doing
Building on the answer from @jeremyjjbrown, another version that cleans up after itself as mentioned in the comments to his answer. This version also allows providing a different server address for use on private internal networks, etc..
Alternatively, if you want to get the IP address of whichever interface is used to connect to the network without having to know its name, you can use this:
I know it's a little different than your question, but others may arrive here and find this one more useful. You do not have to have a route to 8.8.8.8 to use this. All it is doing is opening a socket, but not sending any data.