The following python code snippet will return the IP address
of a machine
import socket
ip_address = socket.gethostbyname(socket.gethostname())
print ip_address
host_name = socket.getfqdn()
print host_name
The IP address may not be proper in all the cases. If the
/etc/hosts file of the unix machine contains entries with hostname mapped to
127.0.0.1, will return 127.0.0.1 as IP address. So for ensuring proper working,
it will be better to use the fully qualified hostname method.
No comments:
Post a Comment