Wednesday 31 December 2014

Python Code Snippet to get the hostname and IP address of a machine

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

How to check the memory utilization of cluster nodes in a Kubernetes Cluster ?

 The memory and CPU utilization of a Kubernetes cluster can be checked by using the following command. kubectl top nodes The above command...